Updated Garb: Even Easier Access to the Google Analytics API

Tony Pitale, Former Viget

Article Category: #Code

Posted on

In my introductory post, I explained what Garb was, and how it could be useful to those wishing to get access to their Google Analytics data. In this post, I would like to introduce the latest version of Garb (0.2.1 as of this writing) and explain the many changes and new ways to use Garb to get at your data.

Getting sessions and profiles is still exactly the same.

 Garb::Session.login('username', 'password') profile = Garb::Profile.all.first 

What's new in 0.2.1 is the way in which reports are built and results are retrieved. Check it out:

As a Report Class

 class Exits include Garb::Resource metrics :exits, :exit_rate dimensions :request_uri end 

Getting the Results with a Class

 Exits.results(profile, :limit => 10, :offset => 20, :start_date => (Date.today - 30), :end_date => Date.today) # With Filtering and Sorting Exits.results(profile) do filter :request_uri.contains => 'fun', :exits.gte => 1000 sort :exit_rate end 

One-off Report

 report = Garb::Report.new(profile) report.metrics :exits, :exit_rate report.dimensions :request_uri # With Filtering and Sort report.filter :request_uri.contains => 'fun' report.filter :exits.gte => 1000 report.sort :exit_rate.desc # Getting Results report.results(:limit => 10, :offset => 20, :start_date => (Date.today - 30), :end_date => Date.today) 

The results returned from Garb will be OpenStructs with methods for each of the metrics and dimensions in an array.

 results.exits #=> 1234 results.exit_rate #=> 0.20423810234 results.request_uri #=> '/some/fun/url/to/a/page' 

Overall, we feel that the improvements are solid and make more sense. Be warned: if you've used a previous version of Garb, then updating to the latest version will very likely break most of what was done previously. I hope everyone can find a use for this, and I encourage all to check out the project on Github and to read the documentation in the Wiki.

Related Articles