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.
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.