Get Real time Users on site and Session Details in Php - Google Analytics API Integration

Get Real time Users on site and Session Details in Php - Google Analytics API Integration

To get started using Google Analytics API in your application, do the following steps:

  1. Creating a project in the Google API Console
  2. Enable the Google Analytics API
  3. Setup OAuth consent screen
  4. Create service account keys
  5. Add service account email id in google analytics user

Creating a project in the Google API Console

If you want to integrate the Google Analytics API with your application, you must have a Google API console project,  you should visit Google API console Before sign in to your Gmail account, you need to associate with Google Analytics account.

Here you can create new Google Console Project or access your existing projects. To add new project click + icon marked in red square.

Once selecting the + icon, it will ask you to enter project name to create new Google console project as shown window below.

To Enable Google Analytics API

If your application needs to use Google APIs, you should enable the services.

To enable an API for your project, follow the below steps:

  1.  Open the API Library in the Google API Console. And click Library menu or click Enable APIs Button in Dashboard page.
  2. In Library page select Analytics API services or search to find it.

Library screen to select Analytics API marked in red color

Next, we want enable analytics API, click on enable button to activate the Google analytics API.  Next step will see how to access the analytics API process.  We can access user data with OAuth 2.0 or server to server interaction

Once Google analytics API enabled. Now you can access Analytics configuration and report data in your application.

Setup OAuth Consent Screen

Setup OAuth consent screen with credentials

Provide “Email Address” & “Product name show to users” others optional you can save the credentials.

create service account keys

To access analytics configurations and report data with your application. Establish the identity of the service account with Public/private key pairs.

To create a new service account key, follow the below steps:

Step 1: Select credentials -> then Service account key.

Step 2: Select Service Account Key option

Step 3:

  1. Select New Service account or exists service account from drop down
  2. For New service we need to provide service account name and its role.

Note: The newly created service account will have and Service account email address similar to below mail id:

Next, choose whether to download the service account's public/private key as a standard P12 file.

Here service account email id:


Now public/private key pair is generated and download in your computer.  Copy & paste generated public/private key pair in your application directory.

To check your service account email id, click manage service account link

Now, you can get your service account email Id on IAM & Admin window.

Sample service account email id:

Add Service Account Email ID in Google Analytics User

Now, we will add service account email id in Google Analytics users to access the analytics data and checking the client information.

To visit Google Analytics data select admin menu

Next, click user management & now add and set service account email id to read & analyze permission to view analytics data in your application.

Sample service account email id:

Once above steps has completed. You can access analytics data in your application.

Example code in PHP

Here implement Google Analytics API integration and getting data on real time active users on site and session details in Google analytics



include 'CustomAnalytcs_class.php';
$service_account_email = '';
 // Service account Email ID
$key_file_location = 'GoogleAnalytics-fa32e1a557ad.p12'; // P12 Format Private Key File
 $analytics = new CustomAnalytcs_class($service_account_email, $key_file_location);
$realtimedata = $analytics->getRealtimeData(); // Get Real Time Data
$sessiondata = $analytics->getSessonData();// Get Real Time Session Data


We can check real time active users with below screen



SI NO Filename Download
1 Source Code Download
Attched File Password: /

About Author

API Notes

We are a professional tutorial site on the programming languages like C, C++, Java, .Net, Php and the web development languages like Html, Css, and JavaScript. We train people on the database programming like MySQL, SQL, and Oracle.

Leave a comment

Leave A Reply