Parsing Google Analytics data with Ruby
The google analytics api come out a few days ago, meaning we can do some cool things with integrating stats data into web apps.
It should be pretty easy to show the number of page views or the most popular browsers in your templates, or even change the amount of advertising depending on the popularity of the page.
A couple of ruby gems have been created gattaca and garb to make it really easy to interface with the api, so I’ve put together a couple of examples to get you started.
Top 10 pages in the past day
The first one is nice and simple, setting up a few options then looping through the results to display the url and number of page views for each.
Graph of hits on the homepage for past month
This one uses the sparklines gem to draw a line graph of the hits on a particular url (’/’ in this example) over the past 30 days.
Neither of these scripts are particularly fast so if you’re planning on using them in a web page be sure to fragment cache them.
It will be really interesting to see how this data is used in the future, maybe we’ll even see self-tuning web sites!


Leave a Comment