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 :(