diff --git a/examples/example_service_account.php b/examples/example_service_account.php new file mode 100644 index 0000000..b7d93bd --- /dev/null +++ b/examples/example_service_account.php @@ -0,0 +1,110 @@ +auth->setClientId($client_id); // From the APIs console +$ga->auth->setEmail($email); // From the APIs console +$ga->auth->setPrivateKey($privatekey); // Path to the .p12 file + + /* + * Step 3: Do real stuff! + * If we're here, we sure we've got an access token + */ + $auth = $ga->auth->getAccessToken(); + +// Try to get the AccessToken +if ($auth['http_code'] == 200) { + $accessToken = $auth['access_token']; + $tokenExpires = $auth['expires_in']; + $tokenCreated = time(); + + print_r($auth); +} else { + echo "somthing went wrong"; +} + + +$ga->setAccessToken($accessToken); +$ga->setAccountId('ga:xxxxx'); // GA ID + +// Load profiles +$profiles = $ga->getProfiles(); +$accounts = array(); +foreach ($profiles['items'] as $item) { + $id = "ga:{$item['id']}"; + $name = $item['name']; + $accounts[$id] = $name; +} +// Print out the Accounts with Id => Name. Save the Id (array-key) of the account you want to query data. +// See next chapter how to set the account-id. +print_r($accounts); + + // Set the default params. For example the start/end dates and max-results + // Set the accessToken and Account-Id +$ga->setAccessToken($accessToken); +$ga->setAccountId('ga:xxxxxx'); // GA ID + +// Set the default params. For example the start/end dates and max-results +$defaults = array( + 'start-date' => date('Y-m-d', strtotime('-3 month')), // get data of 3 months back + 'end-date' => date('Y-m-d'), +); +$ga->setDefaultQueryParams($defaults); + +echo "


Example1: Get visits by date


"; +$params = array( + 'metrics' => 'ga:visits', + 'dimensions' => 'ga:date', +); +$visits = $ga->query($params); +print_r($visits); + + +echo "


Example2: Get visits by country


"; +$params = array( + 'metrics' => 'ga:visits', + 'dimensions' => 'ga:country', + 'sort' => '-ga:visits', + 'max-results' => 30, + 'start-date' => '2014-12-10' //Overwrite this from the defaultQueryParams +); +$visitsByCountry = $ga->query($params); +print_r($visitsByCountry); + + +echo "


Example3: Same data as Example1 but with the built in method:


"; +$visits = $ga->getVisitsByDate(); +print_r($visits); + + +echo "


Example4: Get visits by Operating Systems and return max. 100 results


"; +$visitsByOs = $ga->getVisitsBySystemOs(array('max-results' => 100)); +print_r($visitsByOs); + +echo "


Example5: Get referral traffic


"; +$referralTraffic = $ga->getReferralTraffic(); +print_r($referralTraffic); + + +echo "


Example6: Get visits by languages


"; +$visitsByLanguages = $ga->getVisitsByLanguages(); + +print_r($visitsByLanguages);