Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Empty Rows in Google Analytics Data API

Having difficulty getting empty rows from the Analytics Data API (GA4) BETA. I'm pulling simple traffic by date array but any dates that have zero traffic are not returned - I would prefer it to give me the date and zero.

I believe there should be a KEEPEMPTYROWS parameter but this doesn't seem to work - it took me so long to get the ORDERBY working that I thought I'd check here first to see if anyone else has got this working lol. I know I can fill in the missing dates using PHP code but I'd prefer to get it from the query. Here's the PHP for my JSON query:

$response = $client->runReport([
'property' => 'properties/' . $_SESSION["gaid"],
'dateRanges' => [
    new DateRange([
        'start_date' => $mstartdate,
        'end_date' => $menddate,
    ]),
],
'dimensions' => [new Dimension(
    [
        'name' => 'date',
    ]
),
],
'metrics' => [new Metric(
    [
        'name' => 'activeUsers',
        'name' => 'sessions',
    ]
)
],
'orderBys' => [
    new OrderBy([
        'dimension' => new OrderBy\DimensionOrderBy([
            'dimension_name' => 'date',
            'order_type' => OrderBy\DimensionOrderBy\OrderType::ALPHANUMERIC
        ]),
        'desc' => false,
    ])],
    'keepEmptyRows'=>"1"

]);

Thanks in advance :)

like image 874
Danik10 Avatar asked Aug 22 '26 12:08

Danik10


1 Answers

For anyone else looking for an answer, the option is called "keepEmptyRows":

$client = new BetaAnalyticsDataClient(['credentials' => ...]);
$response = $client->runReport([
    ...
    'keepEmptyRows' => true,
]);

Source: https://github.com/googleapis/php-analytics-data/blob/master/src/V1beta/RunReportRequest.php#L642

Unfortunately, it does not work as expected.

I was asking for a 7 day report, expecting each 7 rows, each containing 0s (previously UA was working like that). Instead I got single row with 0s.

This problem is also mentioned here: https://github.com/google/site-kit-wp/issues/6623

In the end, I ended up pre-filling the array with the default data before applying changes that come from GA.

like image 50
Jacek Kobus Avatar answered Aug 25 '26 02:08

Jacek Kobus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!