Preload Google Analytics

Posted at



Adding a small addition to the head of the page will speed-up the loading of the Google Analytics code you most-probably have on your page.



And it is super easy too!
for the most parts you only need to add a single line:


<link rel="preload" as="script" href="https://www.google-analytics.com/analytics.js"/>
to the HEAD part of the document, ideally just before the <HEAD> part.




If you are using some of the more advance features of Google Analytics,
such as linkid for Enhanced Link Attribution or ec for Ecommerce Tracking
you may even provide an additional overall speed-up by using:
<link rel="preload" as="script" href="//www.google-analytics.com/analytics.js"/> 
<link rel="preload" as="script" href="//www.google-analytics.com/plugins/ua/linkid.js"/> 
<link rel="preload" as="script" href="//www.google-analytics.com/plugins/ua/ec.js"/>





Lastly, if you are still using the older Google Analytics which uses ga.js instead of analytics.js
(and you don't want to upgrade..) - just use:
<link rel="preload" as="script" href="//www.google-analytics.com/ga.js" />





Notes:
- Consider using https:// instead of // to always load Google Analytics from it's secure-location.
- For the older (ga.js) Google Analytics with linkid use:
<link rel="preload" as="script" href="//www.google-analytics.com/ga.js"/>
<link rel="preload" as="script" href="//www.google-analytics.com/plugins/ga/inpage_linkid.js"/>