'How to return metric totals in GA4 using the PHP client library?

When using php-data-analytics, if I call getTotals() on the runReport() response, I get a "RepeatedField" object:

$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 is returned as the following object:

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
        )

How do I use the GA4 PHP client library to return the totals for each of my metrics? According to the official documentation, this should return a Row object?



Solution 1:[1]

The 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;
    }
}

Solution 2:[2]

There's a GA4 Dimensions & Metrics Explorer, which can be used to build requests.
When having a request which yields results, it's not so difficult to port it to PHP then.
Dimension name nthDay may actually be ga:nthDay (at least for UA).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2