I posted how to say hello to Google Analytics API.
Continuing the post, I would like to get Real-time user number using Google Analytics API.
1. Initialize google analytics API.
require '../composer/vendor/autoload.php'; function initializeAnalytics(){ // Creates and returns the Analytics Reporting service object. // Use the developers console and download your service account // credentials in JSON format. Place them in this directory or // change the key file location if necessary. $KEY_FILE_LOCATION = __DIR__ . '/credentials.json'; // Create and configure a new client object. $client = new Google_Client(); $client->setApplicationName("Hello Analytics Reporting"); $client->setAuthConfig($KEY_FILE_LOCATION); $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); $analytics = new Google_Service_Analytics($client); return $analytics; } $analytics = initializeAnalytics();
2. Function for Real-time user number.
function get_realtime_active_user($analytics, $ga_internal_id){ $optParams = array( 'dimensions' => 'rt:medium'); try { $results = $analytics->data_realtime->get( 'ga:'.$ga_internal_id, 'rt:activeUsers', $optParams); // Success. $return = $results->totalsForAllResults['rt:activeUsers']; return $return; } catch (apiServiceException $e) { // Handle API service exceptions. $error = $e->getMessage(); } }
3. Get multiple site’s data, change $ga_id_array variable according to your GA account number.
You can get GA account number just like the below.

$data = array(); $ga_id_array = array('site1'=>'1234567','site2'=>'12345678','site3'=>'123456789'); foreach($ga_id_array as $name => $ga_id){ $each_data = array(); $each_data['name'] = $name; $each_data['num'] = get_realtime_active_user($analytics, $ga_id); $data[] = $each_data; } echo json_encode($data);
4. All together.
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); require '../composer/vendor/autoload.php'; function initializeAnalytics(){ // Creates and returns the Analytics Reporting service object. // Use the developers console and download your service account // credentials in JSON format. Place them in this directory or // change the key file location if necessary. $KEY_FILE_LOCATION = __DIR__ . '/credentials.json'; // Create and configure a new client object. $client = new Google_Client(); $client->setApplicationName("Hello Analytics Reporting"); $client->setAuthConfig($KEY_FILE_LOCATION); $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); $analytics = new Google_Service_Analytics($client); return $analytics; } $analytics = initializeAnalytics(); function get_realtime_active_user($analytics, $ga_internal_id){ $optParams = array( 'dimensions' => 'rt:medium'); try { $results = $analytics->data_realtime->get( 'ga:'.$ga_internal_id, 'rt:activeUsers', $optParams); // Success. $return = $results->totalsForAllResults['rt:activeUsers']; return $return; } catch (apiServiceException $e) { // Handle API service exceptions. $error = $e->getMessage(); } } $data = array(); $ga_id_array = array('site1'=>'1234567','site2'=>'12345678','site3'=>'123456789'); foreach($ga_id_array as $name => $ga_id){ $each_data = array(); $each_data['name'] = $name; $each_data['num'] = get_realtime_active_user($analytics, $ga_id); $data[] = $each_data; } echo json_encode($data);
That’s all, this will return with JSON format like this.
[{"name":"site1","num":"722"},{"name":"site2","num":"100"},{"name":"site3","num":"20"}]