Adding Google Analytics to Your Site

By | November 24, 2015

Adding Google Analytics to your site is very easy. You first need to get your scripting code from Google Analytics. Your tracking code is going to look something like this:

<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', 'UA-12345678-9', 'auto');
  ga('send', 'pageview');

</script>

The important part is the UA-12345678-9; that’s the string which uniquely identifies your site.

Once you have your Analytics script code, you just need to add it in a way where the script gets included on all of your pages & posts. There are a few ways to do this:

  1. If you are using the Montezuma theme, go to Appearance → Montezuma Options → Head → Insert Code and copy the code to either the Top or Bottom fields (not both). Or, the theme that you are using may also have an option to add script code (somewhat uncommon).
  2. Create a child theme and add the Analytics code to a copy of the header.php file. Do not add the code directly to the theme files, otherwise your Analytics code will be wiped out the next time you update the theme. Or,
  3. Use a plugin like Header and Footer which can add any sort of content (CSS, Javascript, etc) to the head section of every page. A bit easier than creating a child theme, but some of you may already have a child theme.

Once you’ve added your script code, do a view source on a few of your pages to make sure you can see it. Then wait for a few days for Google Analytics to gather some stats for your site.

Leave a Reply

Your email address will not be published. Required fields are marked *