This gist applies the theory from Ilya Grigorik's Script-injected "async scripts" considered harmful on the default Universal Analytics snippet. TLDR place this above the CSS in the <head>
of your document
<!-- Google Analytics Part 1: Creates window.ga, sets account, and queues pageview-->
<script>
!function(n,t){n.GoogleAnalyticsObject=t,n[t]=n[t]||function(){(n[t].q=n[t].q||[]).push(arguments)},n[t].l=1*new Date}(window,"ga");
ga('create', 'UA-XXXX-Y', 'auto'); // REPLACE UA-XXXX-Y w/ YOUR ACCOUNT
ga('send', 'pageview');
</script>
<!-- End Google Analytics Part 2 -->
And then place this below your CSS in the <head>
:
<!-- Google Analytics Part 2: load analytics.js-->
<script async src='//www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics Part 2 -->
- 1.default.html - Pulled from Introduction to Analytics.js / Tracking Code Quick Start
- 2.jsniced.html - Pulled from Optimizing the Google Analytics Snippet
- 3.refactored.html - Removes async injected parts + dead variables.