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 example-news-site.com“. 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:

<script>
    (function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,‘script’,’//www.google-analytics.com/analytics.js','ga');

    ga(‘create’, ‘UA-99999999-1’, ‘auto’, ‘projectPresenterTracker’);
</script>

Then to record an event:

<script>
    ga(‘projectPresenterTracker.send’, {
        hitType: ‘event’,
        eventCategory: “example-news-site”,
        eventAction: ‘presentation_preview’,
        eventLabel: “12312",
        eventValue: 1
    });
</script>

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 =  [’https://www.googleapis.com/auth/analytics']
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 = Google::APIClient.new
client.authorization = authorization
api_method = client.discovered_api(‘analytics’,‘v3’).data.ga.get
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’ => Date.today.to_s, ‘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.

Comments

  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 https://essaysworld.net/ 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

  4. gdavegil@yahoo.com
    Dan
    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

    Test!

  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

  7. kzseo19@gmail.com
    Kaii
    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

  9. concretecolumbia@gmail.com
    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.