Created
May 29, 2016 16:09
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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