Skip to content

Instantly share code, notes, and snippets.

@ianbarber
Created March 3, 2015 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianbarber/d39c296f39a09f857db8 to your computer and use it in GitHub Desktop.
Save ianbarber/d39c296f39a09f857db8 to your computer and use it in GitHub Desktop.
Query Analytics with Service Account
<?php
require_once "../vendor/autoload.php";
$client = new Google_Client();
$client->setApplicationName("Service Acccount Test");
$client_id = '985145966381-is5nq94smihmoa2k0567jab6u6vj278c.apps.googleusercontent.com';
$service_account_name = '985145966381-is5nq94smihmoa2k0567jab6u6vj278c@developer.gserviceaccount.com';
$key_file_location = 'foostarter-a27f84b2dec4.p12';
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array(Google_Service_Analytics::ANALYTICS_READONLY),
$key
);
// Remember to cache the access token locally if making multiple calls
// and don't just use this function for each request!
$client->getAuth()->refreshTokenWithAssertion($cred);
$service = new Google_Service_Analytics($client);
$result = $service->data_ga->get("ga:62716028", "yesterday", "today", "ga:pageviews");
var_dump($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment