如何使用 PHP 客户端库在 GA4 中 return 衡量总计?
How to return metric totals in GA4 using the PHP client library?
使用 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
被 return 编辑为以下对象:
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 客户端库 return 我的每个指标的总计?根据 the official documentation,这应该 return 一个 Row 对象?
RepeatedField result returned from calling Google\Analytics\Data\V1beta\RunReportResponse::getTotals()
can 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;
}
}
有一个GA4 Dimensions & Metrics Explorer,可以用来构建请求。
当有一个产生结果的请求时,将它移植到 PHP 就不是那么困难了。
维度名称 nthDay
实际上可能是 ga:nthDay
(至少对于 UA)。
使用 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
被 return 编辑为以下对象:
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 客户端库 return 我的每个指标的总计?根据 the official documentation,这应该 return 一个 Row 对象?
RepeatedField result returned from calling Google\Analytics\Data\V1beta\RunReportResponse::getTotals()
can 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;
}
}
有一个GA4 Dimensions & Metrics Explorer,可以用来构建请求。
当有一个产生结果的请求时,将它移植到 PHP 就不是那么困难了。
维度名称 nthDay
实际上可能是 ga:nthDay
(至少对于 UA)。