Adding Google Analytics to Your Site

| March 29, 2020

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 couple of ways to do this:

  1. 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,
  2. 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 *