Google Analytics API: stacked pageviews
Recently, Rand Fiskin from SEOmoz wrote a great article on how to create a custom Google Analytics report with Excel/Openoffice Calc. If you did not read it yet, please do it now.
I think that it's an excellent idea, however I was quite turned down by the amount of work for the manual data gathering. I knew that Google Analytics has an API for such things, but I never had a reason to use it until today. So I just wrote a few lines of PHP to query the API and spit out the raw data in CSV format.
The ZIP file above contains two files: analytics_api.php curtesy of Chris Hope and runme.php which contains the following config section:
/* config starts here */
$login = '';
$password = '';
$id = '';
$filters['Firefox'] = 'ga:browser=@Firefox';
$filters['Safari'] = 'ga:browser=@Safari';
$metric = 'ga:pageviews';
$startdate = strtotime('-1 day -13 weeks');
$enddate = strtotime('-1 day');
/* config ends here */
The interesting part is the $filters array: Here you can put all your segmentations. In the example above, we get Firefox users vs. Safari users. If you want to get something similar to Rand Fiskin's post, use this:
$filters['Blog'] = 'ga:pagePath=@/blog';
$filters['YOUmoz'] = 'ga:pagePath=@/ugc';
When you run the script for the first time, you should probably restrict it to one single week and use only a limited number of segments, because the execution time can get quite high. As I said, this is the first time I am using the Google Analytics API and maybe there is a more efficient way to do this. Please feel free to leave your remarks in the comments!