PHP BetaAnalyticsDataClient runRealtimeReport空

eyh26e7m  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(75)

如果我尝试以下操作,结果是空的--即使当我登录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,
        ]),
    ]
]);


我做错了什么,试图收集“活跃用户最后30分钟”从谷歌的AnalyticsData API?这是一个问题,释放本身FOUND HERE.我只是需要一个有效的aproach检索实时数据.有任何人通过PHP API客户端做这件事的经验?

9nvpjoqh

9nvpjoqh1#

这是工作
我从你的代码中得到的错误是它找不到维度对象。所以我添加了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);
}

字符串

相关问题