Thursday, April 30, 2009

How to export report from google analytics (PHP)

You need:
GA account (email + password)
GA profile (usually the website you are tracking)
php

Why do we do this?
Access your data without logging into GA
Can download massive number of reports all at once
Twist it into which ever format you want

Template code:
http://manfred.dschini.org/2008/09/26/google-analytics-export/
click "available here " to download

How to use it:
You will see some $_GET[....] .... That is when the user need to write an interface, and set variables through the UI. Actually, you can set those variables inside the php code. I will explain the things you need to set:
$cmp (I don't know. the default is average. So, you can set it as 'average')
$fmt (This correspond to the GA's four export formats. 0-pdf, 1-xml, 2-..... It is highly recommended to use '1' if you want to twist the data further).
$rpt (report typle. export.php page lists all available reports)
$pdr (report duration. for example: $pdr='20080601-20080829')
$email (your GA email)
$password (your GA password)
$id (your GA ID. This is tricky. How do you know your id?
Go to this page:
http://code.google.com/apis/analytics/docs/gdata/gdataLibraries.html
Let's assume this url is not stable. You can get to this page by searching: "Google Analytics data report api Client Libraries & Sample Code". The first result ...
Okay, now you are on that page, click "interactive samples" under JavaScript. These are a few samples. Use the "Retrieve Your Accounts" demo, "Run". Follow the steps. You will get your profile. The profile Id is the value for your $id variable.
By default, the php template will return data on daily basis. If you select a very large time window, you will get too much numbers. There is another variable you can set:
$gdfmt. This allows you to get report by hour/day/week/month
$gdfmt='nth_month' means it is by month. I like by month -:p
This variable is not in the template. But once you know how it works, it is easy to set another variable.
As for the url's format, it is:
https://www.google.com/analytics/reporting/export?
with a number of paramters attached. You can go to your GA account, and click "export", then check out the url of one of those downloadable formats. That gives you a better idea of how to twist your template.

Other API
Bart mentions one:
http://www.cari.net/applescript-and-google-analytics-automatic-exporting.html
It seems it is much easier. But it is for Mac machine. I don't know Mac :(

Google provides its own data report api. The Google Analytics Data Export API is currently in public beta. While in public beta, our API will remain subject to change. I spend a few hours to learn it. It doesn't provide a good tutorial. Plus their Java codes have error! And I don't know where to download the complete js library. I give up...

No comments:

Post a Comment