小编FBR*_*BRC的帖子

通过 oauth2 从 Google Analytics Data API (Ga4) 提取数据(同意屏幕)

是否可以不通过服务帐户从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()
        . …
Run Code Online (Sandbox Code Playgroud)

php google-analytics-api google-cloud-php-client

2
推荐指数
1
解决办法
3126
查看次数