Google Analytics API V4不适用于GA4帐户



是否可以通过API V4从新的Google Analytics(GA4(帐户获取数据?它总是返回以下错误消息:

{ "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(:

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 );

}

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

表示已使用其对应用程序进行身份验证的用户。没有访问您试图从中提取数据的谷歌分析视图的权限。

如果您试图将谷歌分析报告api与谷歌分析GA4帐户一起使用,也可能会导致此问题。由于GA4属性id与UA视图id不同。系统会感到困惑,并认为你根本无法访问。

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

UA与GA4

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

谷歌分析数据api快速入门

require 'vendor/autoload.php';
use GoogleAnalyticsDataV1betaBetaAnalyticsDataClient;
use GoogleAnalyticsDataV1betaDateRange;
use GoogleAnalyticsDataV1betaDimension;
use GoogleAnalyticsDataV1betaMetric;
/**
* 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;
}

直接加载credentials文件

// Authenticate using a keyfile path
$client = new BetaAnalyticsDataClient([
'credentials' => $service_account_key_file_path
]);

相关内容

最新更新