Last active
May 14, 2016 12:12
-
-
Save christophrumpel/ef8af3ce2d006defeab375672f8da55e to your computer and use it in GitHub Desktop.
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 | |
| // require_once('pubnub-lib/autoloader.php'); | |
| // use Pubnub\Pubnub; | |
| // $pubnub = new Pubnub(array( | |
| // 'subscribe_key' => '', | |
| // 'publish_key' => 'f' | |
| // )); | |
| function getService() | |
| { | |
| // Creates and returns the Analytics service object. | |
| // Load the Google API PHP Client Library. | |
| require_once 'google-api-php-client/src/Google/autoload.php'; | |
| // Use the developers console and replace the values with your | |
| // service account email, and relative location of your key file. | |
| $service_account_email = ''; | |
| $key_file_location = ''; | |
| // Create and configure a new client object. | |
| $client = new Google_Client(); | |
| $client->setApplicationName("HelloAnalytics"); | |
| $analytics = new Google_Service_Analytics($client); | |
| // Read the generated client_secrets.p12 key. | |
| $key = file_get_contents($key_file_location); | |
| $cred = new Google_Auth_AssertionCredentials( | |
| $service_account_email, | |
| array(Google_Service_Analytics::ANALYTICS_READONLY), | |
| $key | |
| ); | |
| $client->setAssertionCredentials($cred); | |
| if($client->getAuth()->isAccessTokenExpired()) { | |
| $client->getAuth()->refreshTokenWithAssertion($cred); | |
| } | |
| return $analytics; | |
| } | |
| $analytics = getService(); | |
| /** | |
| * 1.Create and Execute a Real Time Report | |
| * An application can request real-time data by calling the get method on the Analytics service object. | |
| * The method requires an ids parameter which specifies from which view (profile) to retrieve data. | |
| * For example, the following code requests real-time data for view (profile) ID 56789. | |
| */ | |
| $optParams = array( | |
| 'dimensions' => 'rt:medium,rt:city,rt:country'); | |
| try { | |
| $results = $analytics->data_realtime->get( | |
| 'ga:53344051', | |
| 'rt:activeUsers', | |
| $optParams); | |
| print_r($results->getRows()); | |
| // $activeUserCount = $results->totalsForAllResults['rt:activeUsers']; | |
| // $country = $results->getRows()[0][2]; | |
| // $city = $results->getRows()[0][1]; | |
| // //var_dump($country = $results->getRows()[0][2]); | |
| // if($activeUserCount > 0) { | |
| // $info = $pubnub->publish('analytics-channel', ['led' => 1, 'city' => $city, 'country' => $country]); | |
| // } else { | |
| // $info = $pubnub->publish('analytics-channel', ['led' => 0]); | |
| // } | |
| // var_dump($info); | |
| // Success. | |
| } catch (apiServiceException $e) { | |
| // Handle API service exceptions. | |
| $error = $e->getMessage(); | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment