首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >V4不适用于GA4帐户

V4不适用于GA4帐户
EN

Stack Overflow用户
提问于 2022-03-18 13:47:02
回答 1查看 1.2K关注 0票数 0

是否有可能从新的(GA4)帐户中通过API V4获得数据?它总是返回以下错误消息:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
{ "error": { "code": 403, "message": "User does not have sufficient permissions for this profile.", "errors": [ { "message": "User does not have sufficient permissions for this profile.", "domain": "global", "reason": "forbidden" } ], "status": "PERMISSION_DENIED" } }

在UA账户上我可以做得很好。

是否有特定于这种新帐户类型的API (web服务器请求- OAuth)?

属性id

下面是使用的代码(PHP):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
require_once __DIR__ . '/vendor/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfig(__DIR__ . '/FILE.json');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);

$client->setAccessToken($_SESSION['access_token']);
$analytics = new Google_Service_AnalyticsReporting($client);
$response = getReport($analytics);
printResults($response);

function getReport($analytics){
    
    $dateRange = new Google_Service_AnalyticsReporting_DateRange();
    $dateRange->setStartDate("7daysAgo");
    $dateRange->setEndDate("today");
    
    $sessions = new Google_Service_AnalyticsReporting_Metric();
    $sessions->setExpression("name");
    $sessions->setAlias("sessions");
    
    $request = new Google_Service_AnalyticsReporting_ReportRequest();
    $request->setViewId('307566943');
    $request->setDateRanges($dateRange);
    $request->setMetrics(array($sessions));
    
    $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
    $body->setReportRequests( array( $request) );
    
    return $analytics->reports->batchGet( $body );
    
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-19 04:05:35

用户对此配置文件没有足够的权限。

意味着已与您的应用程序进行身份验证的用户。没有权限访问您正试图从中提取数据的Google分析视图。

如果您试图将与GA4帐户一起使用,也可能会引发此问题。因为GA4属性id与UA视图id不相同。系统会变得混乱,并假设您只是无法访问。

解决方案是使用访问该视图的用户对应用程序进行身份验证或授予用户访问权限。并检查您是否使用了正确的api类型的谷歌分析,您正试图访问。

UA对GA4

还请记住,要从GA4帐户中提取日期,您需要使用Google分析数据api。如果您已从UA帐户中提取数据,则一直在使用Google分析报告api。这是两个具有不同方法的完全不同的API。

Google分析数据api快速启动

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
require 'vendor/autoload.php';

use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\Metric;

/**
 * TODO(developer): Replace this variable with your Google Analytics 4
 *   property ID before running the sample.
 */
$property_id = 'YOUR-GA4-PROPERTY-ID';

// Using a default constructor instructs the client to use the credentials
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
$client = new BetaAnalyticsDataClient();

// Make an API call.
$response = $client->runReport([
    'property' => 'properties/' . $property_id,
    'dateRanges' => [
        new DateRange([
            'start_date' => '2020-03-31',
            'end_date' => 'today',
        ]),
    ],
    'dimensions' => [new Dimension(
        [
            'name' => 'city',
        ]
    ),
    ],
    'metrics' => [new Metric(
        [
            'name' => 'activeUsers',
        ]
    )
    ]
]);

// Print results of an API call.
print 'Report result: ' . PHP_EOL;

foreach ($response->getRows() as $row) {
    print $row->getDimensionValues()[0]->getValue()
        . ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71533496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文