Skip to content

Instantly share code, notes, and snippets.

@mppatterson
Created April 2, 2014 14:08
Show Gist options
  • Save mppatterson/9934917 to your computer and use it in GitHub Desktop.
Save mppatterson/9934917 to your computer and use it in GitHub Desktop.
// Sample from: https://developers.google.com/analytics/solutions/articles/hello-analytics-api
// Download the API Client on https://github.com/google/google-api-php-client/tree/master/src
// Require 'Google/Client.php' and set your include path to include the src dir, i.e.:
set_include_path(join(":", array(get_include_path(),
YOUR_APP_PATH.'/google-api-php-client/src',
)));
// Setup the service object
$client_id = 'xxx.apps.googleusercontent.com';
$service_account_name = '[email protected]';
$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);
runMainDemo($analytics);
function runMainDemo(&$analytics) {
try {
$profileId = getFirstProfileId($analytics);
if (isset($profileId)) {
$results = getResults($analytics, $profileId);
printResults($results);
}
} catch (apiServiceException $e) {
// Error from the API.
print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();
} catch (Exception $e) {
print 'There wan a general error : ' . $e->getMessage();
}
}
function getFirstprofileId(&$analytics) {
$accounts = $analytics->management_accounts->listManagementAccounts();
if (count($accounts->getItems()) > 0) {
$items = $accounts->getItems();
$firstAccountId = $items[0]->getId();
$webproperties = $analytics->management_webproperties
->listManagementWebproperties($firstAccountId);
if (count($webproperties->getItems()) > 0) {
$items = $webproperties->getItems();
$firstWebpropertyId = $items[0]->getId();
$profiles = $analytics->management_profiles
->listManagementProfiles($firstAccountId, $firstWebpropertyId);
if (count($profiles->getItems()) > 0) {
$items = $profiles->getItems();
return $items[0]->getId();
} else {
throw new Exception('No views (profiles) found for this user.');
}
} else {
throw new Exception('No webproperties found for this user.');
}
} else {
throw new Exception('No accounts found for this user.');
}
}
function getResults(&$analytics, $profileId) {
return $analytics->data_ga->get(
'ga:' . $profileId,
'2012-03-03',
'2012-03-03',
'ga:visits');
}
function printResults(&$results) {
if (count($results->getRows()) > 0) {
$profileName = $results->getProfileInfo()->getProfileName();
$rows = $results->getRows();
$visits = $rows[0][0];
print "<p>First view (profile) found: $profileName</p>";
print "<p>Total visits: $visits</p>";
} else {
print '<p>No results found.</p>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment