Posted on 19/6/06 by Felix Geisendörfer
The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.
Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.
IMPORTANT: The stuff found in this article is outdated and not working anymore. Please see: New Google Analytics API / DataSource! for a newer version!
Vote for this story on digg.com: Digg It!
I already talked about my CakePHP implementation of a Google Analytics API in some of my previous posts, and I also asked Google to clarify the legal status of such code. Almost a month has passed, and besides the initial we'll contact you about it again e-mail, I didn't hear anything from Google.
About 2 weeks ago, I recieved an e-mail in which one of my readers pointed out the existance of a couple wordpress plugins, that offer Google Analytics statistics. One of them also seems to be pretty popular and so far Google hasn't stopped any of the creators from providing them.
For this reason I decided to release my Api as well, and let you decide about the ways to use it. I'm sure you'll find some interestings things to do with it. Since I already started my new Web Model package over at cakeforge I just added the Google Analytics Model to it.
Want to see it working before you waste your time on it? Check out the demo I setup earlier!
How to Install
- In order to install the Api you need to get the latest version of CakePHP, and unpack it.
- Then download the Web Model and save it to app/web_model.php.
- After you've done that, download the Google Analytics Model and save it as app/models/google_analytics.php.
How to use it?
Here is a simple example how to do the authentication with Google Analytics and how to retrieve the executive overview report for the last 7 days from your first profile:
class AnalyticsController extends AppController
var $name = 'Analytics';
var $uses = array('GoogleAnalytics');
var $loginCookie = null;
// Let's make sure we don't get php max execution timeouts
$email = 'email@example.com';
$pass = 'my-password';
$this->loginCookie = $this->GoogleAnalytics->login($email, $pass);
trigger_error('Acces Denied', E_USER_WARNING);
trigger_error('Connection Timeout', E_USER_WARNING);
// Let's clean up the cache folder every 2 hours
$profiles = $this->GoogleAnalytics->listProfiles($this->loginCookie);
$firstProfile = $profiles['id'];
$report = $this->GoogleAnalytics->getReport($this->loginCookie, $firstProfile, GA_REPORT_EXECUTIVE_OVERVIEW, '-7 days', 'yesterday', 10, GA_FORMAT_CUSTOM_ARRAY);
// Let's see how such a report array looks like:
If you want to find out about all functions and parameters that are supported, simply open up app/models/google_analytics.php and have a look at the source. Everything is documented fairly well and it should be easy to figure things out.
What you should know about the API
This Api essentially is a simulated browser that requests all data like a normal user would request them with a browser. That isn't neccessarly a problem, but if Google decided to change the interface for Google Analytics there could easily be propblems popping up.
In order to keep bandwidth usage low, most data (reports, logins, etc.) is cached for 2 hours by default, but only requests with the exact same parameters are beeing cached. The last parameter of most functions inside the GoogleAnalytics class is called $cacheExpires and can either be a timestamp for some point in the future, or a strtotime() compatible statement like "+30 minutes", this way you can specify the amount of time certain requests are beeing cached. Since Google only flows data in like every 6-24 hours you can easily set this value pretty high.
So I hope you enjoy playing around with this code, everything is licensed under MIT, so you don't have to worry about IP too much. I'd appriciate to get some feedback and will try my best to support those who struggle with the implemenation.
--Felix Geisendörfer aka the_undefined
You can skip to the end and add a comment.
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.