Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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')

Thanks for this! This was really helpful :)

santicarmo31 commented Mar 28, 2017 edited

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


Thank you Esbee, It works absolutely beautifully!

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