3 items

Google Analytics is a complicated and powerful tool suitable for the vast majority of internet businesses. It tracks web activity and helps marketers develop the ground strategy for maintaining users' interest. To be able to analyze any action on web pages Click Admin at the top of any Analytics page to implement generated JavaScript into your web project.

Our company works with Ruby on Rails technology and that is why I would like talk with you about settings for Rails 4 application with Turbolinks.
As a rule, you can implement GA code fast and easy, following these 4 steps. But pay attention to the points. First of all, make sure that the Analytics code only loads up in a production environment. It’s the best way to add the file
_google_analytics.html.slim at app/views/layouts folder with this script.

  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

  ga('create', 'XX-XXXXXXXX-X', 'auto');
  ga('send', 'pageview');

Okay, guys, let's move it along. The second necessary step is creating a google_analytics.coffee file in app/assets/javascripts/analytics.js.

jQuery ->
  $(document).on 'page:change', ->
    if window.ga?
      ga('set',  'location', location.href.split('#')[0])
      ga('send', 'pageview', { "title": document.title })

As you can see, this file will track the page whenever loads a new page. And yeah, in application.html.slim, render the layouts before closing the head tag.

title My Rails App

/ Some code...

== stylesheet_link_tag 'application', params[:controller], media: 'all'
== render 'layouts/google_analytics'


At the end, to be work done, don’t forget in your application.js to require the coffee file previously created.

//= require jquery
//= require jquery_ujs
//= require google_analytics

var someJavascriptStuff = {};  

Now, Google Analytics Tracking script is added and you’re good to go.