gpt4 book ai didi

php - 如何使用 PHP 客户端库在 GA4 中返回指标总计?

转载 作者:行者123 更新时间:2023-12-02 15:52:50 38 4
gpt4 key购买 nike

使用 php-data-analytics 时,如果我在 runReport() 响应上调用 getTotals(),我会得到一个“RepeatedField”对象:

$params = [ 
"property" => "properties/{$property_id}",
"dateRanges" => [
new DateRange([
'start_date' => '7daysAgo',
'end_date' => 'yesterday',
]),
new DateRange([
'start_date' => '14daysAgo',
'end_date' => '8daysAgo',
])
],
"dimensions" => [
new Dimension([ 'name' => 'nthDay' ])
],
"metrics" => [
new Metric([ 'name' => 'activeUsers' ])
],
"orderBys" => [
new OrderBy([
'desc' => false,
'dimension' => new OrderBy\DimensionOrderBy([
"dimension_name" => 'nthDay',
"order_type" => OrderBy\DimensionOrderBy\OrderType::NUMERIC
])
])
],
"keepEmptyRows" => true
];

$report = $client->runReport($params);
$totals = $report->getTotals();

$totals 作为以下对象返回:

Google\Protobuf\Internal\RepeatedField Object
(
[container:Google\Protobuf\Internal\RepeatedField:private] => Array
(
)

[type:Google\Protobuf\Internal\RepeatedField:private] => 11
[klass:Google\Protobuf\Internal\RepeatedField:private] => Google\Analytics\Data\V1beta\Row
[legacy_klass:Google\Protobuf\Internal\RepeatedField:private] => Google\Analytics\Data\V1beta\Row
)

如何使用 GA4 PHP 客户端库返回每个指标的总计?根据the official documentation , 这应该返回一个 Row 对象?

最佳答案

RepeatedField result从调用 Google\Analytics\Data\V1beta\RunReportResponse::getTotals() 返回的结果可以 be iterated .

You must request a metric aggregation when you run the report to retrieve totals.

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\MetricAggregation;
use Google\Analytics\Data\V1beta\OrderBy;
use Google\Analytics\Data\V1beta\OrderBy\DimensionOrderBy;
use Google\Analytics\Data\V1beta\OrderBy\DimensionOrderBy\OrderType;

$property_id = '314116996';

$client = new BetaAnalyticsDataClient();

$params = [
'property' => "properties/{$property_id}",
'dateRanges' => [
new DateRange([
'start_date' => '7daysAgo',
'end_date' => 'yesterday',
]),
new DateRange([
'start_date' => '14daysAgo',
'end_date' => '8daysAgo',
]),
],
'dimensions' => [
new Dimension(['name' => 'nthDay']),
],
'metrics' => [
new Metric(['name' => 'activeUsers']),
],
'orderBys' => [
new OrderBy([
'desc' => false,
'dimension' => new DimensionOrderBy([
'dimension_name' => 'nthDay',
'order_type' => OrderType::NUMERIC,
]),
]),
],
'keepEmptyRows' => true,
'metricAggregations' => [
MetricAggregation::TOTAL,
],
];

$response = $client->runReport($params);

$totals = $response->getTotals();

foreach ($totals as $row) {
foreach ($row->getMetricValues() as $metricValue) {
echo 'Metric Value: '.$metricValue->getValue().PHP_EOL;
}
}

关于php - 如何使用 PHP 客户端库在 GA4 中返回指标总计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72082811/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com