是否可以通过 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)?
这是使用的代码(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 );
}
用户对此配置文件没有足够的权限
表示您已对其应用程序进行身份验证的用户。无权访问您尝试从中提取数据的 Google 分析视图。
如果您尝试通过 Google Analytics GA4 帐户使用 Google Analytics Reporting API,也可能会导致此问题。由于 GA4 媒体资源 ID 与 UA 视图 ID 不同,系统会感到困惑并假设您没有访问权限。
解决方案是向有权访问该视图的用户验证应用程序或授予用户访问权限。并检查您是否使用了适合您尝试访问的 Google Analytics(分析)类型的正确 API。
另请记住,要从 GA4 帐户中提取日期,您需要使用 Google Analytics Data API。如果您已从 UA 帐户提取数据,则您一直在使用 Google Analytics Reporting API。这是两个完全不同的 API,具有不同的方法。
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;
}
// Authenticate using a keyfile path
$client = new BetaAnalyticsDataClient([
'credentials' => $service_account_key_file_path
]);