PHP BetaAnalyticsDataClient runRealtimeReport 为空

问题描述 投票:0回答:1

如果我尝试以下操作,它最终会变成空——即使当我登录 GA Analytics 时,它会在过去 30 分钟内显示 2 或 3 个当前活跃用户。 (这是我试图取回的数据)

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

$credentialsPath = 'service-account-credentials.json';
$property_id = 'XXX';
putenv("GOOGLE_APPLICATION_CREDENTIALS=" . __DIR__ . '/service-account-credentials.json');

$client = new BetaAnalyticsDataClient();

$response =  $client->runRealtimeReport ([
    'property' => $property_id,
    '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;
}

相反,如果我使用

runReport()
且 dateRage 为
today
->
today
,它也会返回空,但我觉得这是预期的,因为它不是实时 API,并且
today
可能还为时过早报告。但只需将
start_date
更改为
7daysAgo
,我就可以获得可靠、准确的数据返回。

$response = $client->runReport([
    'property' => $property_id,
    'dateRanges' => [
        new DateRange([
            'start_date' => 'today',
            'end_date' => 'today',
        ]),
    ],
    'dimensions' => [
        new Dimension(['name' => 'city']),
    ],
    'metrics' => [new Metric(
        [
            'name' => 'activeUsers',
        ]
    )
    ],
    'orderBys' => [
        new OrderBy([
            'dimension' => new OrderBy\DimensionOrderBy([
                'dimension_name' => 'date', // your dimension here
                'order_type' => OrderBy\DimensionOrderBy\OrderType::ALPHANUMERIC
            ]),
            'desc' => false,
        ]),
    ]
]);

我在尝试从 Google 的 AnalyticsData API 收集“最近 30 分钟的活跃用户”时做错了什么?这是版本本身的问题吗在这里找到。我只需要一种有效的方法来检索实时数据。有谁有通过 PHP API 客户端执行此操作的经验吗?

php google-analytics-api
1个回答
0
投票

这正在工作

我从您的代码中得到的错误是它找不到维度对象。所以我添加了

new \Google\Analytics\Data\V1beta\Dimension

<?php
require 'vendor/autoload.php';


$service_account_key_file_path = "C:\Development\FreeLance\GoogleSamples\Credentials/ServiceAccountCred.json";

use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;

try {

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

    // CHANGE THIS
    $property_id = '250796939';
    $dimensions = [ new \Google\Analytics\Data\V1beta\Dimension(['name' => 'city']), ];
    $metrics = [new \Google\Analytics\Data\V1beta\Metric(['name' => 'activeUsers'])];

    $response =  $client->runRealtimeReport ([
        'property' => 'properties/' . $property_id,
        'dimensions' => $dimensions,
        'metrics' => $metrics,
    ]);

    foreach ($response->getRows() as $row) {
        foreach ($row->getDimensionValues() as $dimensionValue) {
            print 'Dimension Value: ' . $dimensionValue->getValue() . PHP_EOL;
        }
        foreach ($row->getMetricValues() as $metricValue) {
            print 'Metric Value: ' . $metricValue->getValue() . PHP_EOL;
        }
    }

} catch (\Google\ApiCore\ValidationException $e) {
    printf($e);
}
© www.soinside.com 2019 - 2024. All rights reserved.