Tweetegy On the edge of chaos with Ruby, Rails, JavaScript and AngularJS.

| About | Search | Archive | Github | RSS |

Using Google Analytics API in a Ruby on Rails project



I assume you know about Rails already. Garb can be installed as follows:

gem install garb

Go to your Google Analytics account and fetch the Account Id and Profile Id that your interested in. You will find your Account Id on your account home page under the link Edit account settings, it is the number after the UA-. You will find your Profile Id, by going back to your account home page, finding the profile in the table, then click the Edit link in that row.the Profile Id is at the top of this page.phew!

If you are not familiar with the Google Analytics API, then you will find the Data Feed Query Explorer tool useful.

The Task: Record and display Unique Pageviews from a set list of pages

Now for the fun part! Lets build something that records Unique Page Views from a list of pages in a local database. We need to create a model to store these pages and the unique pageviews. Change into your rails project directory and run the following scaffold script:

script/generate scaffold page url:string unique_pageviews:integer

Open up the pages controller that was just generated and add the following method, which sets the class instance variable @profile to an instance of your Google Analytics profile based on the Account Id and Profile Id.

 1 def get_analytics_profile
 2   Garb::Session.login(your_google_analytics_username,your_google_analytics_password)
 3   accounts = Garb::Account.all
 4   #Loop all Accounts for account_id
 5   accounts.each do |account|
 6     if == your_google_analytics_account_id
 7       #Loop all Profiles for profile_id
 8       account.profiles.each do |profile|
 9        if == your_google_analytics_profile_id
10          @profile = profile
11          break
12        end
13      end
14     end
15   end
16 end

Basically, Garb::Session.login is called passing your username and password to authenticate the session. Next we fetch all accounts and iterate the array until we find the account that matches your Account Id. Then we need to iterate all the profiles within that account until we find the profile that matches your Profile Id.

Now add this method which will update all the unique pageviews in your list of pages:

 1 def update_all
 2   @pages = Page.all
 3   @pages.each do |page|
 4       get_analytics_profile if !@profile
 5       report =
 6       report.metrics :unique_pageviews
 7       report.dimensions :page_path
 8       report.filters :page_path.contains => page.url
 9       page.unique_pageviews = report.results.first.unique_pageviews
11     end
12   end
13 end

Since you have used a scaffold to create this controller, there should already be an index method present. Add a call to the update_all method at the top of the index method so that everytime the index method is called the update_all method is called. Here is how your index method should look:

1 def index
2   update_all
3   @pages = Page.all
5   respond_to do |format|
6     format.html # index.html.erb
7     format.xml  { render : xml => @pages }
8   end
9 end

Final Touches

Now all that remains is a little configuration, add some fixtures and we’re done! You should already have fixtures for your page model. Note that the url does not need to include the domain name, so /pricing/ or /sign-up/ are the sorts of values you should put here (obviously ones that exist in your Analytics profile!). The configuration is simply to add:

1 config.gem "garb"

in the appropriate place of your environment.rb file.

Get those metrics!

Now fire up your rails app and navigate to the index for your pages http://localhost:3000/pages/. The call to update_all will be made which will update your local database with the metrics and then render the data in the browser in the usual Rails table format.


Using the Garb gem makes it extremely easy to work with the Google Analytics API in a Rails project. Obviously this example application can be improved to include caching, error handling (it will bomb out if the Id’s or urls are not found in your Analytics account) as well as other improvements that you might want to make.