通过 oauth2(同意屏幕)从 Google Analytics Data API (Ga4) 中提取数据
Extract data from Google Analytics Data API (Ga4) via oauth2 (consent screen)
是否可以不通过服务帐户从 Google Analytics Data API(GA4 帐户)中提取数据?我可以使用服务帐户(下面的示例)正常提取,但我需要通过 oauth(同意屏幕)进行授权,但我发现没有任何相关内容。
<?php
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;
$client = new BetaAnalyticsDataClient(['credentials' => 'MY-CREDENTIALS.json']);
$response = $client->runReport([
'property' => 'properties/MY-ID',
'dateRanges' => [
new DateRange([
'start_date' => '2020-03-31',
'end_date' => 'today',
]),
],
'dimensions' => [new Dimension(
[
'name' => 'city',
]
),
],
'metrics' => [new Metric(
[
'name' => 'activeUsers',
]
)
]
]);
print 'Report result: ' . PHP_EOL;
foreach ($response->getRows() as $row) {
print $row->getDimensionValues()[0]->getValue()
. ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
}
我得到的是这样的:
$client = new BetaAnalyticsDataClient( [
'credentials' => Google\ApiCore\CredentialsWrapper::build( [
'scopes' => [
'https://www.googleapis.com/auth/analytics',
'openid',
'https://www.googleapis.com/auth/analytics.readonly',
],
'keyFile' => [
'type' => 'authorized_user',
'client_id' => 'MY-CLIENT-ID',
'client_secret' => 'MY-CLIENT-SECRET',
'refresh_token' => 'REFRESH-TOKEN' //
],
] ),
] );
是否可以不通过服务帐户从 Google Analytics Data API(GA4 帐户)中提取数据?我可以使用服务帐户(下面的示例)正常提取,但我需要通过 oauth(同意屏幕)进行授权,但我发现没有任何相关内容。
<?php
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;
$client = new BetaAnalyticsDataClient(['credentials' => 'MY-CREDENTIALS.json']);
$response = $client->runReport([
'property' => 'properties/MY-ID',
'dateRanges' => [
new DateRange([
'start_date' => '2020-03-31',
'end_date' => 'today',
]),
],
'dimensions' => [new Dimension(
[
'name' => 'city',
]
),
],
'metrics' => [new Metric(
[
'name' => 'activeUsers',
]
)
]
]);
print 'Report result: ' . PHP_EOL;
foreach ($response->getRows() as $row) {
print $row->getDimensionValues()[0]->getValue()
. ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
}
我得到的是这样的:
$client = new BetaAnalyticsDataClient( [
'credentials' => Google\ApiCore\CredentialsWrapper::build( [
'scopes' => [
'https://www.googleapis.com/auth/analytics',
'openid',
'https://www.googleapis.com/auth/analytics.readonly',
],
'keyFile' => [
'type' => 'authorized_user',
'client_id' => 'MY-CLIENT-ID',
'client_secret' => 'MY-CLIENT-SECRET',
'refresh_token' => 'REFRESH-TOKEN' //
],
] ),
] );