Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics 4 API - runReport() give empty result : "source" Dimension not working

I'm working with the new Google Analytics API (GA4) on a PHP script. My API requests were working well since yesterday, one of the runReport() query suddenly start returning empty result :

        $globalTrendData = $client->runReport([
        'property' => 'properties/' . $property_id,
        'dateRanges' => [
            new DateRange([
                'start_date' => '20daysAgo',
                'end_date' => 'yesterday',
            ]),
        ],
        'dimensions' => [
            new Dimension(['name' => 'pagePath',]),
            new Dimension(['name' => 'pageTitle',]),
            new Dimension(['name' => 'city',]),
            new Dimension(['name' => 'source',]),
            new Dimension(['name' => 'date',])
        ],
        'metrics' => [
            new Metric(['name' => 'screenPageViews',]),
            new Metric(['name' => 'userEngagementDuration',]),
            new Metric(['name' => 'activeUsers',]),

        ]
    ]);

Why this query stopped working ? Is Google making some change on the API ?

like image 548
Camille Avatar asked Jun 05 '26 18:06

Camille


1 Answers

Few days ago, Google started using new names for dimensions and metrics... Remplacing some used by the old Universal Analytics API.

A migration documentation just appeared here : https://developers.google.com/analytics/devguides/migration/api/reporting-ua-to-ga4-dims-mets

So for my problem, I had to change :

 new Dimension(['name' => 'source',]),

By :

new Dimension(['name' => 'sessionSource',]),

As "sessionSource" is the new official GA4 name to get source dimension. Solved the problem and got the correct return.

Hope it can help some people wondering why suddenly Analytics calls don't work, check metrics and dimensions names on the link above !

like image 116
Camille Avatar answered Jun 08 '26 07:06

Camille



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!