Skip to content

Instantly share code, notes, and snippets.

@mppatterson
Created April 2, 2014 14:12
Show Gist options
  • Save mppatterson/9934981 to your computer and use it in GitHub Desktop.
Save mppatterson/9934981 to your computer and use it in GitHub Desktop.
Connecting to Google Analytics via a service object (server-to-server)
// Setup the service object
$client_id = 'xxx.apps.googleusercontent.com';
$service_account_name = 'xxx@developer.gserviceaccount.com';
$client_secret = 'notasecret';
$keyfile = 'path_to_your_key.p12';
$client->setClientSecret($client_secret);
$client->setAssertionCredentials(
new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/analytics'),
file_get_contents($keyfile)
)
);
$client->setClientId($client_id);
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
$analytics = new apiAnalyticsService($client);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment