The Google Analytics API and PHP: A seriesPosted in PHP -
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.
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.
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.
GA API Introductory Posts
The following posts cover a couple of things about the Google Analytics API:
The following code examples have been posted for using the Analytics class:
- Basic data example getting the most popular pages
- Get data without a dimension
- Using filters
- Paging through result data
- Calculating a page's bounce rate
- Get data for a single page
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.