Daniel Hartmann

healthy coding

Google Analytics and Zend Framework

Google provides a lot of APIs to export some of your data used in their services. In the following example I’d like to explain how to use the Google Analytics API to create own reports using the Zend Framework and the Zend_Gdata_Analytics component.

To use this component, which is not included in the Zend Framework source itself, you have to download the Zend_Gdata_Anayltics classes at github.com.

After updating the Zend library, you are able to query account or data feeds. Account feeds provide information about your existing accounts and profiles. Data feeds allow you to fetch some statistic data assigned to one of your profiles.

To get details on your account, you just have to call the getAccountFeed method of the service object. Which data of profile is provided in the response of such a request, can be found in the API documentation of Google Analytics.

require_once 'Zend/Gdata/ClientLogin.php';
require_once 'Zend/Gdata/Analytics.php';

$client = Zend_Gdata_ClientLogin::getHttpClient($email, $password, 
  Zend_Gdata_Analytics::AUTH_SERVICE_NAME);
$service = new Zend_Gdata_Analytics($client);

$accounts = $service->getAccountFeed();
$properties = array(
  'webPropertyId', 'accountName', 'accountId',
  'profileId', 'currency', 'timezone',
  'title', 'tableId', 'id', 'updated', 'link'
);

foreach($accounts as $account) {
  echo "\n{$account->title}\n";
  foreach($properties as $property) {
    echo "\t{$property} = {$account->$property}\n";
  }
}

The statistic data of a profile can also be fetched using a simple call of getDataFeed. This method accepts a query parameter which must provide information about dimensions, metrics or filter of the data query.

$dimensions = array(
  Zend_Gdata_Analytics_DataQuery::DIMENSION_MEDIUM,
  Zend_Gdata_Analytics_DataQuery::DIMENSION_SOURCE,
  Zend_Gdata_Analytics_DataQuery::DIMENSION_BROWSER_VERSION,
  Zend_Gdata_Analytics_DataQuery::DIMENSION_MONTH,
);

$query = $service->newDataQuery()->setProfileId($yourProfileId)
  ->addMetric(Zend_Gdata_Analytics_DataQuery::METRIC_BOUNCES) 
  ->addMetric(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS) 
  ->addFilter("ga:browser==Firefox")
  ->setStartDate('2011-05-01') 
  ->setEndDate('2011-05-31') 
  ->addSort(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS, true)
  ->addSort(Zend_Gdata_Analytics_DataQuery::METRIC_BOUNCES, false)
  ->setMaxResults(50);

foreach($dimensions as $dim){
  $query->addDimension($dim);
}

$result = $service->getDataFeed($query); 
foreach($result as $row){
  echo $row->getMetric('ga:visits')."\t";
  echo $row->getValue('ga:bounces')."\n";
}

There are some restrictions in the Google Analytics API, which don’t let you fetch data for queries with more than seven dimensions. The maximum number of entries returned from a request is also limited to 10,000. But common queries usually don’t run into such issues.

If you want some more details on the Zend_Gdata_Analytics class, you can take a look at the Zend Framework proposal page or just download the source from github.com.

  

13 Responses

  1. Hi Daniel,

    I have been using your component for a while now. I was wondering, how can I iterate over all results? Lets say my websites has 14000 keywords which bring in visits.

    Thanks!
    Gerron

    • This will depend on your query. So if you just add the keyword dimension to your query, you should be able to iterate it easily.

      /*...*/
      $query->addDimension('ga:keyword')
        ->setMaxResults(10000);
      
      $result = $service->getDataFeed($query);   
      foreach($result as $row){ /* ... */}
      
  2. Wouldn’t that query stop after 10000 results? While you can paginate using offset?

  3. I have tried to use count() on $result, which did not work due to the fact it is an object which you can access via magic methods. I might be doing something wrong, but haven’t been able to iterate over all results since I don’t know how to determine the result count and use an offset.

    $totalResults = $results->getTotalResults();
    $totalResults = $totalResults->getText();

    The above also returns something I cannot use. For example: 1534 results while the interface would list 2562.

    Thanks!
    Gerron

    • I just tried to understand your issue but I can’t reproduce the behaviour you’re describing:

      $query = $service->newDataQuery()
        ->addMetric(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS)
        ->addDimension(Zend_Gdata_Analytics_DataQuery::DIMENSION_KEYWORD) 
        ->setStartDate('2006-01-01') 
        ->setEndDate('2011-07-13') 
        ->addSort(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS, true)
        ->setMaxResults(10000); 
      
      $result = $service->getDataFeed($query); 
      $totalResults = $result->getTotalResults()->getText();
      var_dump($totalResults);
      var_dump(count($result));
      

      This snipplet in my case results in:

      string(4) "2074"
      int(2074)
      

      which matches the count I’m getting displayed in Google Analytics itself.

      The Zend_Gdata_Analytics_DataFeed class extends the Zend_Gdata_Feed which should implement the Countable interface, so the results should be the same as in other Zend_Gdata components. Do you use some filter or some sorting definitions?

      - daniel

  4. Thank you for the explanation.

    It turned out it was the Google Analytics API itself. As soon as I combined (multiple) dimensions with ga:visitors, a lot of the rows disappear.

    - Gerron

  5. Markus says:

    That’s a great library. As good or better than many other API wrappers in the Zend Framework. Why hasn’t this gone into the official release yet?

  6. Hey Daniel,

    Your Zend_Gdata_Analytics component is brilliant, I’ve been using it for a while now and can’t understand why Zend haven’t brought this into their framework yet.

    I was wondering if you’ve read about the upcoming API changes announced by Google and whether this will affect your component? Details can be seen at http://analytics.blogspot.com/2011/08/introducing-two-new-versions-of.html “If you are still using the Account Feed in the Data Export API, we highly recommend you test out the new Management API and start planning your migration.”

    Cheers,

    Jonathan

  7. Thank you so much, man. It’s a great lib and perfectly serves my needs.

  8. Brian Vanderbusch says:

    I’m also curious if this is compatible with the new Google API v3. I see it hasn’t changed on git.

  9. Jan Kopp says:

    It will be great, to find this constant

    {code}
    const ANALYTICS_FEED_URI_SSL = ‘https://www.google.com/analytics/feeds/data’;
    {code}

    in Zend_Gdata_Analytics_DataQuery.

  10. soomal says:

    Is that still working?
    I am trying to use example query and I got “Internal error: query validation failed “. However fetching accounts works.

Leave a Reply