Documentation

Using Google Analytics - Zend_Gdata

Using Google Analytics

The Google Analytics API allows client applications to request data, saved in the analytics accounts.

See » http://code.google.com/apis/analytics/docs/gdata/v2/gdataOverview.html for more information about the Google Analytics API.

Retrieving account data

Using the account feed, you are able to retrieve a list of all the accounts available to a specified user.

  1. $service = Zend_Gdata_Analytics::AUTH_SERVICE_NAME;
  2. $client = Zend_Gdata_ClientLogin::getHttpClient($email, $password, $service);
  3. $analytics = new Zend_Gdata_Analytics($client)
  4. $accounts = $analytics->getAccountFeed();
  5.  
  6. foreach ($accounts as $account) { 
  7.   echo "\n{$account->title}\n";
  8. }

The $analytics->getAccountFeed() call, results in a Zend_Gdata_Analytics_AccountFeed object that contains Zend_Gdata_Analytics_AccountEntry objects. Each of this objects represent a google analytics account.

Retrieving report data

Besides the account feed, google offers a data feed, to retrieve report data using the Google Analytics API. To easily request for these reports, Zend_Gdata_Analytics offers a simple query construction interface. You can use all the » metrics and dimensions specified by the API. Additionaly you can apply some » filters to retrieve some » common data or even complex results.

  1. $query = $service->newDataQuery()->setProfileId($profileId) 
  2.   ->addMetric(Zend_Gdata_Analytics_DataQuery::METRIC_BOUNCES)   
  3.   ->addMetric(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS)
  4.   ->addDimension(Zend_Gdata_Analytics_DataQuery::DIMENSION_MEDIUM) 
  5.   ->addDimension(Zend_Gdata_Analytics_DataQuery::DIMENSION_SOURCE) 
  6.   ->addFilter("ga:browser==Firefox") 
  7.   ->setStartDate('2011-05-01')   
  8.   ->setEndDate('2011-05-31')   
  9.   ->addSort(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS, true) 
  10.   ->addSort(Zend_Gdata_Analytics_DataQuery::METRIC_BOUNCES, false) 
  11.   ->setMaxResults(50);
  12.  
  13. $result = $analytics->getDataFeed($query);
  14. foreach($result as $row){ 
  15.   echo $row->getMetric('ga:visits')."\t"
  16.   echo $row->getValue('ga:bounces')."\n"
  17. }

Copyright

© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts