phpgoogle-analyticsuniversal-analytics

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 ?


Solution

  • 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 !