如果我尝试以下操作,结果是空的--即使当我登录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客户端做这件事的经验?
1条答案
按热度按时间9nvpjoqh1#
这是工作
我从你的代码中得到的错误是它找不到维度对象。所以我添加了
new \Google\Analytics\Data\V1beta\Dimension
字符串