Add the following code (known as the "JavaScript tracking snippet") to your site's templates.
The code should be added before the closing tag, and the string 'UA-XXXXX-Y' should be replaced with the property ID (also called the "tracking ID") of the Google Analytics property you wish to track.
<!-- Google Analytics -->
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
The above code does four main things:
<script>
element that starts asynchronously downloading the analytics.js JavaScript library from https://www.google-analytics.com/analytics.jsga()
command queue) that allows you to schedule commands to be run once the analytics.js library is loaded and ready to go.ga()
command queue to create a new tracker object for the property specified via the 'UA-XXXXX-Y'
parameter.ga()
command queue to send a pageview to Google Analytics for the current page.