One of websites uses full page caching and it has very good performance. But I want to make it faster by putting Turbolinks to my website. With Turbolinks, the browser will not recompile my Javascript and CSS each page change and speed up my website.

But the other problem occur. The problem is Google Analytics always send pageview for same URL because of Turbolinks does not recompile javascript from Google Analytics

Turbolinks basically uses XHR request each page change and always evaluate script tag inside inside HTML body. Google Analytics need to send pageview on every page change.

So to make Google Analytics with Turbolinks, I should make Turbolinks evaluates send pageview function from Google Analitycs every page change.


<html>
<head>
<% if Rails.env.production? %>
<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', 'YOUR_GOOGLE_ANALYTICS_ID', 'auto');
</script>
<% end %>
</head>
<body>
<% if Rails.env.production? %>
<script>
ga('send', 'pageview', '<%= request.fullpath %>');
</script>
<% end %>
</body>
</html>