Google Analytics API
Unfortunately Google Analytics does not have an API yet... However I will show you a quick and dirty way to get the data you need. The idea is basically to setup an automated report from within Google Analytics, which will email a CSV file attachment, that you can download and parse with a script. For this example I will use the Zend Mail class because I love Zend Mail and it is free.
So the first step is to schedule the report. For this simple example we will use the default Visitors Report. So login to your Google Analytics account, click Visitors on the main left nav, then click the Email icon up at the top of the report, see screenshot below:
Now let's get to the code. First step is to connect to your POP mail server and get the new messages. Don't forget to download the latest version of Zend at http://framework.zend.com.
$params = array('host' => "mail.domain.com", 'user' => "info@domain.com", 'password' => "secret");
$mail = new Zend_Mail_Storage_Pop3($params);
$num = $mail->countMessages();
In a production environment, you would want some logic to verify that the number of messages is greater than 0, loop through the messages, etc but for this tutorial I have omitted these items for brevity. So next we need to download the first message and get its parts.
$parts = $message->countParts();
Next we spin through the parts, looking for the attachment.
Then we parse the CSV file to an array for simple looping:
$lines = explode("\n",$csv);
$data = array();
foreach ($lines as $line){
$data[] = explode(',',$line);
}
The previous code checks the email account, downloads the CSV attachment and parses it to an array. That is all going to stay the same but the matching below will change depending on your needs. For this simplistic example, I am going to get the number of visits from yesterday.
So that's basically it. Of course in production you would probably want to write the Google Analytics data to a database or whatever, but you get the point! Hopefully Google Analytics will get an API soon, but until then, this works like a champ!

























Nice workaround for getting the GA data. thanks for the tutorial. I have a question about the zend mail function, do I need to use this script then save it with some name(like filename.php) and then run it from the browser to get the data stored (ex: mysqldb)? or Can I include this script in my other files then show the required value like “number of visits, page views etc”?
Or can I just set the cron job for every hour or two for this mail script so that it checks the data ans store the data in our mysql db to store the values and then retrieve to show in the browser?
Can you please let me know if we can use the normal php to get the mail instead of using zend framework?
The great thing about the Zend Framework is that you can use just the classes that you need. I often use just the mail class by itself. The Zend framework is PHP. http://framework.zend.com
Good post.
I did the same but with XML, nice to see how we got to the same solution from different angles/geo locations.
gmail runs on ssl port 995 , you have add ssl to the conf.
$params = array(’host’ => “mail.domain.com”, ‘user’ => “info@domain.com”, ‘password’ => “secret”,’ssl’ => ‘SSL’);
A way to access the google analytics api using php.
http://www.ceritosoftware.com/articles/index/page/php-access-google-analytics-api
I found great interface GAPI http://code.google.com/p/gapi-google-analytics-php-interface to do this.
Can get any data from Google Analytics even ecommere.
Regards Steven