Batch downloading analytic events from Google Analytics

Recording 'events' on the fly can be an unnecessary heavy load on the server

Analytics Chart by StockSnap is licensed under CC0 Creative Commons

The Ruby on Rails web application, Project Presenter, is a tool that allows companies to distribute and post Project News, case studies, and project stories to news sites, trade journals, and other sites relating to that project.

To prove the value of Project Presenter, the application needs to be able to send analytics information back to the end user. For example: “Project X, was click 27 times on“. However, the clicks, combined with all of the impressions, soon became a significant load on the server and database. What we’ve done to remedy that though is to move live clicks and impressions directly to the Project Presenter Google Analytics account. They’re sent as ‘events’ and are then downloaded as a batch and analyzed. This is how we did it:

On the remote site, you need to namespace your Google Analytics code like this:

        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

    ga(‘create’, ‘UA-99999999-1’, ‘auto’, ‘projectPresenterTracker’);

Then to record an event:

    ga(‘projectPresenterTracker.send’, {
        hitType: ‘event’,
        eventCategory: “example-news-site”,
        eventAction: ‘presentation_preview’,
        eventLabel: “12312",
        eventValue: 1

Then to get a weekly job, we pull down the data as and put it in the Project Presenter database. We’ll then put it in a format that we need. To do that, we need a couple gems to interact with Google Analytics:

gem ‘google-api-client’, ‘0.8.2’, require: ‘google/api_client’
gem ‘googleauth’

This is setup in a rake task that gets triggered with Heroku Scheduler (and monitored by Dead Man’s Snitch). For full disclosure here, part of the reason I have did not make this post 18 months ago when I did the work is because I don’t fully understand everything for this. For example, the access_token of ‘123’ just simply had to be “something” and not blank. And, the cert_path is was another tricky area that my understanding is limited.

scopes =  [’']
cert_path = Gem.loaded_specs[‘google-api-client’].full_gem_path+‘/lib/cacerts.pem’
ENV[‘SSL_CERT_FILE’] = cert_path
authorization = Google::Auth.get_application_default(scopes)
client =
client.authorization = authorization
api_method = client.discovered_api(‘analytics’,‘v3’)
client.authorization.access_token = ‘123’

So now the Google client is setup and can be chatted with.

result = client.execute(:api_method => api_method, :parameters => { ‘ids’ => “ga:999999999”, ‘start-date’ => 7.days.ago.to_date.to_s, ‘end-date’ =>, ‘dimensions’ => ‘ga:eventCategory, ga:eventLabel, ga:eventValue’, ‘metrics’ => ‘ga:totalEvents’})

array_of_data = eval(result.response.env.body)[:rows]

This gives you the event data in an array to do whatever you need. In our case, we are bundling the data into week long segments to display in charts.

# [[“building-great-homes”, “2532", “presentation_preview”, “1"], [“building-great-homes”, “2633", 
“presentation_preview”, “1"],...

Collective Idea - Google Analytics Chart.png

And that’s it! Now you have your data without loading down your servers!

Photo of Jason Carpenter

Jason has an entrepreneurial spirit which has led him to start four companies in his young career. Couple that with his robust knowledge of programming, he’s a double threat.


Add a Comment

Hmm...that didn't work.

Something went wrong while adding your comment. If you don't mind, please try submitting it again.

Comment Added!

Your comment has been added to this post. Please refresh this page to view it.

Optional. If added, we will display a link to the website in your comment.
Optional. Never shared or displayed in your comment.