Adding Google Analytics to Rails 4.2 app
<p>I've got a Rails 4.2 app that I've deployed with Heroku and I've tried to add Google Analytics to it. However, Google Analytics isn't picking up any sessions.</p>

<p>Any suggestions why and how to resolve this?</p>

<p><strong>CODE</strong></p>

<p>/app/layouts/_footer.html.erb:</p>

<pre><code>&lt;% if Rails.env.production? %&gt;
  &lt;script&gt;
    (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-XXXXXXXX-X', 'herokuapp.com');
    ga('send', 'pageview');

  &lt;/script&gt;
&lt;% end %&gt;
</code></pre>

<p>/app/layouts/application.html.erb:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Title&lt;/title&gt;
&lt;%= Gon::Base.render_data({}) %&gt;
  &lt;%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' =&gt; true %&gt;
  &lt;%= javascript_include_tag 'application', 'data-turbolinks-track' =&gt; true %&gt;
  &lt;%= csrf_meta_tags %&gt;
  &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;div class="container"&gt;
    &lt;%= yield %&gt;
&lt;/div&gt;

&lt;%= render 'layouts/footer' %&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>/app/assets/javascripts/analytics.js.coffee:</p>

<pre><code>$(document).on 'page:change', -&gt;
 if window._gaq?
  _gaq.push ['_trackPageview']
 else if window.pageTracker?
  pageTracker._trackPageview()
</code></pre>

<p>/app/assets/javascripts/application.js:</p>

<pre><code>//= require jquery
//= require analytics
//= require bootstrap
//= require jquery_ujs
//= require jquery.ui.all
//= require Chart
//= require jquery.turbolinks
//= require lodash
//= require_tree .
</code></pre>

<p>Gemfile:</p>

<pre><code>source 'https://rubygems.org'

gem 'rails',                '4.2.2'
gem 'bootstrap-sass',       '3.2.0.0'
gem 'sass-rails',           '5.0.2'
gem 'uglifier',             '2.5.3'
gem 'coffee-rails',         '4.1.0'
gem 'jquery-rails',         '4.0.3'
gem 'jquery-ui-rails', '~&gt; 4.2.1'
gem 'turbolinks',           '2.3.0'
gem 'jquery-turbolinks'
gem 'jbuilder',             '2.2.3'
gem 'sdoc',                 '0.4.0', group: :doc
gem 'chart-js-rails'
gem 'gon'
gem 'lodash-rails'

group :development, :test do
  gem 'sqlite3',     '1.3.9'
  gem 'byebug',      '3.4.0'
  gem 'web-console', '2.0.0.beta3'
  gem 'spring',      '1.1.3'
end

group :production do
  gem 'pg',             '0.17.1'
  gem 'rails_12factor', '0.0.2'
end
</code></pre>