Created
April 2, 2014 14:08
-
-
Save mppatterson/9934917 to your computer and use it in GitHub Desktop.
This file contains 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
// 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