The Google Analytics API and PHP: A seriesThe Google Analytics API and PHP: A series

Posted April 28th, 2009 in PHP (Updated December 16th, 2009)

The Google Analytics API is a powerful tool for accessing analytics data without having to log into the web interface and means you can easily have automated processes for collating data and saving it to a database, emailing information on a daily basis etc.

Google Analytics API PHP Class

I have created a PHP class for accessing the Google Analytics API with CURL and using DOMDocument to parse the XML. The resulting class and example script can be downloaded here and there are a number of posts on this website showing how to use it.

Last Updated

The analytics class was last updated on May 28nd 2009 at 10:51am NZST.
The example script was last updated on May 28th 2009 at 10:55am NZST.

Both files are in the .tar.gz file available for download.

Please note: This API is no longer under development and is not supported.

Try this page and this page over at Google code for other PHP APIs. For some reason Stig Manning's is the only one listed on the second page so check the first page as well because there are a couple more there as well.

Code Reuse

Feel free to use the class as you wish but if re-posting it on other websites or using it in your own projects or for your customers, please retain the notice in the analytics class file attributing the original work to me.

If you are using my class and would like to be added to my Who's using my Google Analytics API PHP Class? page please send me an email and I will add you.

GA API Introductory Posts

The following posts cover a couple of things about the Google Analytics API:

Code examples

The following code examples have been posted for using the Analytics class:

Code updates

Whenever I make changes to the class I upload and add a post about what was changed.

The April 30th 2009 update included adding error triggers on request failures, and some changes to solve connection issues from Windows.

The May 22nd 2009 update included making the auth property public; adding start and end parameters to the ->data() method for "today", "yesterday" and "week"; adding the filters and debug parameter to the data method; and adding the get_summary, get_summaries and sec2hms methods.

The May 28th 2009 update included a new analytics_filters class for helping with setting up filters when calling the ->data() method.

Questions and answers

I've had various questions from people by email and answered them both in emails and by posting answers up in new blog posts.

The first Q&A post looked at possible reasons for not being able to connect to the Google Analytics API.

The second Q&A post looked at how to get the account name with a profile_ID, how to format timeOnSite, and how to get "absolute unique visitors".

The third Q&A post looked at the badly formatted request to the Google Analytics API error, default dates and that a second instance of the API is unauthorized.

Comments

blog comments powered by Disqus