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.
  1. January 14, 2019 at 7:31 AM

    Thank you for any other wonderful post. The place else may anybody get that type of information in such an ideal way of writing? I’ve a presentation next week, and I am at the look for such info.

  2. February 01, 2019 at 11:55 AM

    Custom dimensions will let you collect and analyze records that Analytics does no longer routinely tune. You could for example use custom dimensions to outline person types like member and non member. Analytics routinely tracks page and display screen load instances however you may also installation customized monitoring to degree how lengthy it takes for Ajax based totally operations and resources to load.

  3. Unless you’re setting up an event to only fire when a form is successfully submitted, it’s much easier to get inaccurate metrics than by setting up a destination goal and then just noindexing that page.

  4. April 10, 2019 at 6:47 AM

    Google Analytics (GA) is a free service accessible by Google that generate detailed figures about the visitors to a website.

  5. April 23, 2019 at 12:34 PM

    Thanks for such a great content on this website.

  6. April 23, 2019 at 12:34 PM

    Thanks for sharing this blog its very informative and useful for us.

  7. April 23, 2019 at 19:58 PM

    The administration argues that more capable regulators and safer mortgage products alleviate the concern.

  8. April 30, 2019 at 8:07 AM

    i was looking for this type of analytics guide but could not found anywhere, thanks for the guide Jason.

    natalia johnson
    May 03, 2019 at 7:34 AM

    well information

  10. May 03, 2019 at 8:27 AM

    This is really nice and informative post.