Skip to content

Instantly share code, notes, and snippets.

Last active April 15, 2020 15:01
Show Gist options
  • Save the-bass/545653241530f8f2c2e16371bec56f20 to your computer and use it in GitHub Desktop.
Save the-bass/545653241530f8f2c2e16371bec56f20 to your computer and use it in GitHub Desktop.
Using Google Analytics with Rails 5 and Turbolinks 5. This code is taken from the conversation between @preetpalS and @packagethief on
<%# Put this code snippet between the <head></head>-tags in your application layout and %>
<%# replace 'UA-XXXXXXXX-X' with your own unique Google Analytics Tracking ID %>
<%# ... %>
<%# ... %>
<% if Rails.env.production? %>
<script type="text/javascript">
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
ga('create', 'UA-XXXXXXXX-X', 'auto');
<% end %>
<%# ... %>
# Put this file into your assets/javascripts/ folder and assure
# it gets loaded by checking or editing your assets/javascripts/application.js
# file as appropriate
document.addEventListener 'turbolinks:load', (event) ->
if typeof ga is 'function'
ga('set', 'location',
ga('send', 'pageview')
Copy link

Thanks for this! This was really helpful :)

Copy link

santicarmo31 commented Mar 28, 2017

This works for me, I've forgotten to execute: rake assets:precompile RAILS_ENV=production after update the code to the server. Now it is working, thanks

Copy link

Thank you Esbee, It works absolutely beautifully!

Copy link

zedtux commented Jun 3, 2017

@esBeee can you please add a JavaScript version of the Coffee file?

Copy link


document.addEventListener('turbolinks:load', function(event) {
  if (typeof ga === 'function') {
    ga('set', 'location',;
    ga('send', 'pageview');

Copy link

Kattyi commented Jun 17, 2017

Thank you!

Copy link

bragboy commented Jul 29, 2017

Thank you

Copy link

Great! Thanks.
In case this is helpful for someone... I had some trouble making it work because of my google analytics code (had to create a different one) - but this code definitely works.

Copy link

Exactly what I was looking for, thank you !

Copy link

seebq commented Oct 12, 2017

Or if you're using the new gtag manager:

$(document).on('turbolinks:load', function () {
gtag('config', 'UA-XXXXXXXX-X', {'page_path': window.location.pathname});

Per their instructions here:

under Tracking virtual pageviews

Copy link

Thanks @seebq. Newest site I was working on required the new gtag version and I couldn't figure out what method to call. Your solution worked.

Copy link

shrmnk commented Jan 4, 2018

This is what I use with the gtag manager, using slim and CoffeeScript:


- if Rails.env.production?
  = javascript_include_tag ''
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

$(document).on 'turbolinks:load', ->
  if typeof gtag is 'function'
    gtag('config', UA-XXXX-X', {'page_path': window.location.pathname})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment