1

Before GA4 i use a custom php dashboard with dimensions and metrics for reporting stats. I use VIEW_ID for having my stats. Here my code :

function initializeAnalytics(){

$KEY_FILE_LOCATION = '/google-api-php-client/service-account-credentials.json';

$client = new Google_Client();

$client->setApplicationName("Reporting");

$client->setAuthConfig($KEY_FILE_LOCATION);

$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);

$analytics = new Google_Service_Analytics($client);

return $analytics;

}

function getResults($analytics) {

return $analytics->data_ga->get(

'ga:VIEW_ID',

'30daysAgo',

'yesterday',

'ga:users',

array('dimensions'=>'ga:date')

);

}

function printResults($results) {

if (!empty($results->getRows()) && count($results->getRows()) > 0) {

$rows = $results->getRows();

return $rows[0][0];

} else {

return 0;

}

}

$analytics = initializeAnalytics();

$results = getResults($analytics);

echo printResults($results);

It's workinig for all UA-XXXXXX-X properties with VIEW_ID.

But now with GA4 Properties, there is no VIEW_ID and i don't know how having this report using the new G_MEASUREMENT_ID. There is no guide for PHP and it still in alpha version...

Please help, my custom dashboard doesn't work with GA4 :(

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
bat
  • 11
  • 1
  • 1
  • 2

3 Answers3

7

The Google analytics Reporting API doesn't support GA4 accounts. You will need to use the new analytics data api There is currently very little documentation for this api but im sure it will arrive in the coming months.

You will need to rewrite your custom dashboard to use this new api, It should be available to you as part of the PHP client library.

  "name": string,
  "dimensions": [
    {
      object (DimensionMetadata)
    }
  ],
  "metrics": [
    {
      object (MetricMetadata)
    }
  ]
}
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
0

For GA4 Reporting API , new APIs are available this link can be referred for the set up steps Step For migrating api of UA to GA4

and using this query explorer tool you can build the query parameter Query Explorer tool

aliya kazi
  • 19
  • 2
-2

To show reports in My admin dashboard, i ended up using google data studioGoogle Data studio

Importing GA4 data in it and made reports.

There's share button which gives a link to view the reports,

Used IFRAME tag in my admin dashboard with (google data studio report) view link

Sahil Kashyap
  • 329
  • 2
  • 10