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.


  1. June 25, 2019 at 13:03 PM

    If you need help with writing papers I recommend you to apply to the writing service that is named Works perfect!

  2. July 15, 2019 at 9:25 AM

    Ruby on Rails web application with many interesting and attractive features. I appreciate this app. I enjoyed this app.
    Word Game

  3. August 22, 2019 at 14:40 PM

    If you need help with writing papers I recommend you to apply to the writing

    September 20, 2019 at 4:31 AM

    Great information! I always use this google analytics in my job.
    | R22 Replacement

  5. XXX
    September 20, 2019 at 4:32 AM


  6. September 26, 2019 at 4:26 AM

    It is wonderful to be here with everyone, I have a lot of knowledge from what you share, to say thank you, the information and knowledge here helps me a lot

    September 26, 2019 at 18:27 PM

    Well, this is very beneficial most especially to all analytics user. Thanks!
    | Merchant Account Companies

  8. Gail Stacey
    October 01, 2019 at 11:10 AM

    I havent heard of Project presenter before but just recently it amazed me because of its help.
    Gail | kitchen remodelers charleston

    Lily Clemen
    October 01, 2019 at 11:20 AM

    This app is just amazing and it works perfectly with the project im trying to finish right now.
    Lily | click here

  10. October 02, 2019 at 3:33 AM

    The information you share is great and useful to me and many others. It is closely related to my work and has helped me grow.