Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gondo
Last active September 21, 2020 13:06
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save gondo/10717698 to your computer and use it in GitHub Desktop.
Save gondo/10717698 to your computer and use it in GitHub Desktop.
<?php
/**
* 1. create project at https://console.developers.google.com/project
* 2. enable 'Analytics API' under 'APIs & auth' / APIs
* 3. create 'NEW CLIENT ID' (OAuth client) under 'APIs & auth' / Credentials
* i. select 'Service account'
* ii. save generated key file to 'key.p12'
* iii. remember CLIENT ID
* 4. under GA account add 'Read & Analyze' access to newly generated email (access to GA Account not Property nor View)
* 5. get View ID. go to GA Admin section, select proper Account, than proper Property, than proper View.
Under View click on 'View settings' and copy the number below 'View ID' (that is your GA_VIEW_ID number)
* 5. download google php library https://github.com/google/google-api-php-client
* 6. use the code below, use info from google API console (1.)
* doc here: https://developers.google.com/analytics/devguides/reporting/realtime/v3/reference/data/realtime/get
* real time metrics doc: https://developers.google.com/analytics/devguides/reporting/realtime/dimsmets/
*/
set_include_path('src/' . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
$CLIENT_ID = 'XXX.apps.googleusercontent.com';
$CLIENT_EMAIL = 'YYY@developer.gserviceaccount.com';
$SCOPE = 'https://www.googleapis.com/auth/analytics.readonly';
$KEY_FILE = 'key.p12';
$GA_VIEW_ID = 'ga:ZZZ';
$client = new Google_Client();
$client->setClientId($CLIENT_ID);
$client->setAssertionCredentials(
new Google_Auth_AssertionCredentials(
$CLIENT_EMAIL,
array($SCOPE),
file_get_contents($KEY_FILE)
)
);
$service = new Google_Service_Analytics($client);
try {
$result = $service->data_realtime->get(
$GA_VIEW_ID,
'rt:activeVisitors'
);
var_dump($result->totalsForAllResults['rt:activeVisitors']);
} catch(Exception $e) {
var_dump($e);
}
@gubertu
Copy link

gubertu commented Jun 12, 2014

it's works after and changed the timezone in php.ini!!

thanks! :)

@christianstrang
Copy link

Great job, thank you so much!

For those getting the following error: "Fatal error: Class 'Google_Service' not found"
use the autoload.php instead:

require_once 'Google/autoload.php';

@walksalot
Copy link

THANK YOU! Would have saved me 5 hours if I found this earlier.

@miky00008
Copy link

it's give white page

@danielset
Copy link

I needed to figure this out the other day and ended up writing a blog-post with a detailed description on how to get to the rt:activeVisitors with PHP. Hope that helps someone out there.

@palermo33
Copy link

Warning: require_once(Google/Service/Analytics.php): failed to open stream:

help me

Copy link

ghost commented Apr 7, 2017

use this:
<?php //Google RealTime Connect require_once 'path_to/vendor/autoload.php'; $client = new Google_Client(); $client->setApplicationName("XXXX"); // your project name $client->setAuthConfig('path_to_Auth_jsno_file'); $client->addScope('https://www.googleapis.com/auth/analytics.readonly'); $GA_VIEW_ID = 'ga:XXXX'; // your View_ID $service = new Google_Service_Analytics($client); try { $result = $service->data_realtime->get( $GA_VIEW_ID, 'rt:activeVisitors' ); $count = $result->totalsForAllResults['rt:activeVisitors']; echo $count; } catch(Exception $e) { var_dump($e);

@umh
Copy link

umh commented Jul 24, 2017

Hi, i wanna know whether we can?:

  1. Display total page views (lifetime data) for a specific page
  2. Display current active users on a page.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment