Skip to content

Instantly share code, notes, and snippets.

@danielset
Created May 29, 2016 16:09
Show Gist options
  • Save danielset/e21b3a59dbec3db2449abc29229bb003 to your computer and use it in GitHub Desktop.
Save danielset/e21b3a59dbec3db2449abc29229bb003 to your computer and use it in GitHub Desktop.
PHP Snippet PHP Connect to Google Real Time API and receive the activeVisitors on the site
<?php
//Google RealTime Connect
require_once '/ABSOLUTE/PATH/TO/GOOGLE-API-PHP/vendor/autoload.php';
$client = new Google_Client();
$client->setApplicationName("A_DESCRIPTIVE_NAME");
$client->setAuthConfig('/ABSOLUTE/PATH/TO/GOOGLE-API-PHP/AUTH-FILE.json');
$client->addScope('https://www.googleapis.com/auth/analytics.readonly');
$GA_VIEW_ID = 'ga: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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment