1

I have followed the following Stack Overflow page to produce a work around to the common issue of Turbolinks and Google Analytics not playing nicely.

Google analytics with rails 4

The code is in place but Google Analytics has alerted me there is missing tracking code for the web application and no data has been captured.

The code and setup I have used:

Created a new file under Assets > Javascripts (analytics.js.coffee)

// Coffee
    $(document).on 'page:change', ->
     if window._gaq?
      _gaq.push ['_trackPageview']
     else if window.pageTracker?
      pageTracker._trackPageview()

    // Javascript
    $(document).on('page:change', function() {
     if (window._gaq != null) {
      return _gaq.push(['_trackPageview']);
     } else if (window.pageTracker != null) {
      return pageTracker._trackPageview();
     }
    });

Create a partial in Views > Layouts (_footer.html.erb) I use my domain and tracking ID in my application's code, just replaced for this example.

<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-55647322-1', 'www.mysite.com');
  ga('send', 'pageview');

</script>

Added the render code to the bottom of the application layout page (application.html.erb)

</script>


<%= render :partial => 'layouts/footer' %>


</body>
</html>

Could the problem be with the Javascript file? Does it have to be a js.coffee file, or can I use simply .js?

Hopefully someone can work out what I'm doing wrong.

Thanks for looking.

Community
  • 1
  • 1
RubyMax
  • 341
  • 4
  • 16
  • Are you able to see the analytics JS in the source code of the live page? Also, you should change www.mysite.com to reflect your actual site unless you haven't already. – NM Pennypacker Feb 25 '15 at 16:15
  • @NickM thanks for the input Nick, mysite.com is replaced with the actual domain name in the code. After taking a look, the home page does not display the _footer.html.erb partial, but the rest of the app does. Any ideas? – RubyMax Feb 25 '15 at 16:30
  • Are you using a custom template/layout for your home page? Can you post your root route and the corresponding controller? – NM Pennypacker Feb 25 '15 at 16:34
  • I'm guessing the homepage does not display the partial because the render code was only added to the application.html.erb layout and not the home.html.erb layout. @NickM Do you have any ideas why it doesn't work at all though? Thanks for your help. – RubyMax Feb 25 '15 at 17:17
  • If it isn't working with the application layout I couldn't tell you why, but you might try including the footer render in the home layout. – NM Pennypacker Feb 25 '15 at 18:04
  • A side note: in your `analytics.js.coffee` file it is suggested to chose one version of code - either javascript or coffeescript. – R Milushev Apr 16 '15 at 17:47

0 Answers0