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(e,t){e.GoogleAnalyticsObject=t;e[t]=e[t]||function(){
(e[t].q=e[t].q||[]).push(arguments)};e[t].l=1*new Date})(t,"ga");
ga('create', 'UA-XXXX-Y'); // 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.