Retrieving Data from Google Analytics API using PHP
Analytics Parameters
The data format for analytics is fairly comprehensive and easy to understand – see their data feed reference document for a clear outline of the parameters that can be passed and what format they can take. I am using the ids parameter (which is the tableId I showed retrieval of in the earlier blog post), the date parameters, metrics, dimensions and sort – so my code looks something like this, with the parameters set in $query_data.
$oauth = new OAuth($oauth_consumer_key, $oauth_consumer_secret);
$oauth->setToken($token, $token_secret);
$oauth->fetch('https://www.google.com/analytics/feeds/data', $query_data);
$result = $oauth->getLastResponse();
Combining Parameters
The biggest problem I’m encountering with the analytics is understanding how to combine the dimension and metrics parameters to get useful data. It is pretty easy to report on visitors by day/week/month, over a given date range, but getting more meaningful and useful statistics is still a work in progress! There is some documentation, which is exhaustive, but doesn’t link the available data to the sorts of questions users actually want to ask. If anyone has any suggestions on good combinations to use, I’d absolutely love to hear them – leave me a comment and I’ll post again if/when have some better data patterns!
Cool! thanks for the link.
I used to work for a SEO/Web company so i was up to date with everything but lately i have no time. Its great to know the link to api and that it is so simple.
If i needed to make a dashboard now i am sure i would pull GA key metrics onto it : -)
Cheers for the link
You can always use Zend Framework’s Zend_Gdata library. You can find example at my blog – http://blog.wilgucki.pl/2010/03/top-10-w-oparciu-o-google-analytics-i.html
Unfortunately its in polish language, so look at source code only.
I find Google’s interactive Data Feed Query Explorer very helpful in sorting out the dimensions, metrics, etc I want to use with my Analytics queries.
http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html
I hadn’t really played much with the data explorer but it’s helping me understand the dimensions/metrics thing. Thanks!
Any ideas on how to do tis without using oauth? not sure on oauth yet!
OAuth would be my recommendation, it can seem complicated because there are multiple steps but I found that once I got started it was very approachable. The code examples here and in my previous post are all you need … give it a shot and good luck!
I would recommend of going with GAPI PHP class made by Google. :)
I think the advantage of going with Lorna Jane’s set-up is that you can use OAuth rather than hard-coding usernames and passwords in your code as is required in the GAPI PHP class.
I haven’t looked at the supplied PHP classes lately, but when I was working on this project, there were new API features in google not supported by any of the class libraries. And I have an interest in digital identity, so as Martin says the OAuth option was important and interesting to me.