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.

  

31 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.

  11. Hey there! Do you knmow if thy make any plugins to protect
    against hackers? I’m kinda paranoid about losing everything I’ve worked hard on.
    Any suggestions?

  12. Tremendous issues here. I am very satisfied to see your post.
    Thanks so much and I’m taking a look forward to contact you.
    Will you kindly drop me a e-mail?

  13. Hi there! Do you know if they make any plugins to safeguard against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on.

    Any tips?

  14. My brother suggested I might like this web site.
    He was entirely right. This post truly made my day. You can not imagine just how much
    time I had spent for this info! Thanks!

  15. mutari firme says:

    I am not sure where you’re getting your info, but great topic.
    I needs to spend some time learning more or understanding more.
    Thanks for wonderful information I was looking for this information for my mission.

  16. Hello there, just became alert to your blog through Google, and found that it is truly
    informative. I am gonna watch out for brussels. I will be grateful if you continue this in future.
    Many people will be benefited from your writing. Cheers!

  17. Miriam says:

    whoah this blog is excellent i really like studying your posts.
    Stay up the great work! You recognize, lots of persons are hunting
    round for this info, you can aid them greatly.

    my site … steam wallet codes (Miriam)

  18. You actually make it seem so easy with your presentation but I find this matter to be actually something which I think I
    would never understand. It seems too complex and very broad for me.
    I am looking forward for your next post, I will try to get the hang of it!

  19. Google says:

    Observers maintain the item displays a new coherent approach,
    one thing thus low in your culture, that it’s not necessarily realised by simply all.
    The website speed test at Secret Search Engine Labs will
    analyze how fast a page on your site is loading and give you tips on how to improve
    it. But it seems Memorial Day wasn’t important enough to Google.

  20. Wonderful web site. A lot of useful information here. I am sending
    it to some friends ans also sharing in delicious. And naturally, thanks
    in your effort!

  21. Google says:

    is updated frequently with free advice about Google Ad – Words
    strategy, tactics, tips tricks and techniques for success in Ad – Words advertising.
    The website speed test at Secret Search Engine Labs will analyze how fast a page on your site is loading and give you tips on how to improve it.
    There are other ways to improve your ranking in Googlemaps, the purpose
    of this blog post is not to tell you EVERYTHING Frederick Web Promotions can do to improve
    your ranking, the purpose of this particular blog post is
    to:.

  22. Wow that was unusual. I just wrote an really long comment
    but after I clicked submit my comment didn’t show up.
    Grrrr… well I’m not writing all that over again. Anyhow, just wanted to say excellent blog!

  23. Hello!
    Really does any person have the very same troubles when i complete?
    I can not appear to any further meet my companion within the
    room. Immediately after obtaining about three
    youngsters I’ve dropped the baby-belly excess fat nevertheless I am unable to manage to re-tighten our vagina.
    I have to consistently do other pursuits to produce your pet satisfied
    within the bedroom because vaginal sexual intercourse no more excites your ex.
    Guide! I have began considering goods along with solutions to repair
    this particular, can easily any person advocate a
    good 1?
    Appreciate it!

  24. Hi there, I would like to subscribe for this web site to get newest updates, so where can i do it please help out.

  25. That is a very good tip particularly to those new to the blogosphere.

    Short but very precise information… Thank you for sharing this one.
    A must read post!

  26. Felipa says:

    I read a lot of interesting posts here. Probably you spend a lot of
    time writing, i know how to save you a lot of work, there is an online tool that creates readable, SEO friendly articles in seconds, just type in google –
    laranitas free content source

  27. Hello our beloved! I would like to claim that this post is awesome, excellent authored and can include somewhere around valuable infos. I’m going to discover far more articles like that grow taller for idiots pdf.

  28. Your style is really unique in comparison to other people I have read
    stuff from. Thank you for posting when you’ve got the opportunity, Guess I’ll just bookmark this
    site.

Leave a Reply