skip to Main Content

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 🙁

3

Answers


  1. 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)
        }
      ]
    }
    
    Login or Signup to reply.
  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

    Login or Signup to reply.
  3. To activate the analysis by Google (GA4) of the traffic on a website, you must configure in Google Analytics a property with a web data stream. To draw a chart of the number of visits per page directly in let’ say the administrative part of a website, you must activate the Google Analytics Data API, create a service account in the Google Console and authorize in Google Analytics this account to access the data collected by the property, install with Composer the PHP component google/analytics-data.

    Explaining everything takes a while. Here is the article with all the details – just follow the steps:

    https://www.izend.org/en/traffic-analytics

    The same article in French:

    https://www.izend.org/fr/trafic-analytics

    To draw a chart of the number of visits on a page of your website over a given period of time, download iZend:

    https://www.izend.org/en/installation

    Look in blocks/analytics.php for the code in PHP which collects the data from Google Analytics 4.

    Look in views/en/analytics.phtml for the code in HTML and JavaScript which calls Google corechart to draw the line chart.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search