How to set up Google Analytics in WordPress

How to set up Google Analytics in WordPress

John Pawson's Layout avatar

Analytics… a word that many creative types shy away from. But understanding how your website performs is not only interesting, it’s imperative to knowing if your site is being visited, who’s visiting it, what’s working, and what’s not. It can help you identify areas – either in terms of design or content – that need work, and ensure that you’re speaking to your target audience.

There are a number of tools that allow you to analyze your site’s traffic; Google Analytics is, perhaps unsurprisingly, the most well known and many would argue the best available. It offers a wealth of options to dissect information about the people  visiting your site; where they found you, how long they stayed, and even demographics such as age, gender, location, and browser preference. There is a fairly steep learning curve for some of the more in-depth analysis options, but top level data is easy to access, filter, and consume.

Thankfully, setting up Google Analytics is a straightforward process for those using WordPress sites. Widely speaking, you have three main options for implementing Google Analytics in WordPress, depending on the theme and plugins you have installed. But before we look at those options, the first thing to do is set yourself up with an analytics account at www.google.com/analytics/.

 

google-analytics-setup

The step by step process will take you through setting up your site, and by the end of it, you’ll be presented with some nifty code that needs to find its way into your site. It should 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');

&nbsp;

ga('create', 'UA-XXXXXXXX-1', 'auto');

ga('send', 'pageview');

&nbsp;

</script>

Got it? Ok, here are the three choices for what to do next:

The theme route

Many recent WordPress themes (both premium and free) include an area to paste in your Google Analytics tracking code, usually found in “theme options.” Others simply offer a place to insert code into each page of your site. They may offer you the opportunity to “insert code into the HEAD section of your site,” or something similar.

Simply take the tracking code created during the Google Analytics setup process and paste it in the designated box. Hit save, and that’s that – you’re all set up. Within Google Analytics you should see that the site is now “receiving data.” If it doesn’t show this immediately, don’t worry; it can take a little time to update.

google-analytics-wordpress-theme

The plugin route

If your site’s theme doesn’t have this option, or if you want to keep your Google Analytics separate from your theme (for example, if you think you may change your theme in the future), then a plugin could be the easiest option for you. There are many available that will make adding Google Analytics to your site a breeze – both free and premium. From experience, there’s probably not much need to go down the premium track; from the multitude of free options available there is bound to be one that suits your requirements, as these two examples show:

  • Google Analytics by Yoast: Yoast is one of the most well-known WordPress plugin companies out there; the Google Analytics plugin is just one of a suite of plugins that are available and is used by a large section of the WordPress community. As such, this plugin is well documented and is likely to be regularly updated. The plugin provides straightforward setup options and the bonus of being able to analyze your stats directly within the WordPress admin area. This is great if you want to be able to access your visitor info without having to login to a separate site. There are a few other options that may or may not be useful depending on how far you are planning to analyze your site.
  • Simple Analytics: In contrast to the Yoast plugin, simple analytics is just that… simple. There are no fancy options – just a simple, lightweight plugin that provides a space to paste in your tracking code. This is potentially a better option for those worried about plugins increasing load times. Since this plugin is so simple, it shouldn’t add any extra bloat to your install. You’ll have to analyze your stats from the Google Analytics interface, but even with Yoast’s plugin you may want to visit Google Analytics to really sink your teeth into the data.

google-analytics-wordpress-settings

If you’re considering the plugin route, have a look through WordPress’s library of free plugins and pick a couple to test. Look for those that have been recently updated, make sure they work with your installed version of WordPress (which as far as I’m concerned should always be the latest version!), and have a look both at the number of downloads and the topics in the support section. Those that are actively supported or have a large number of downloads and an almost empty support section might be the best ones to start with. Try a few out (one at a time to avoid any conflicts), and once you’ve settled on one, delete the rest from your plugins installs.

The code route

If you are a WordPress purist and hate the use of plugins (or for whatever reason are restricted on plugin installs) then the code route is a completely bloat-free way of integrating your analytics code. It does, however, require you to manually edit PHP files, so if you’d rather avoid touching the code that makes your website work, perhaps consider the alternate options above… Still with me? Ok. Let’s take a look at how to add your tracking code straight into the core files.

  • Log in to your site’s admin panel and head to the appearance tab on the left hand menu. Under that you’ll see “Editor” as the last option.
  • Click this and you’ll see a whole list of the behind-the-scenes PHP files that make your site work. Find “Header.php” and open it up.
  • Once you’re editing this file, copy your Analytics tracking code and paste it in right before the closing </head> tag. Hit save and you’re done.
  • This will mean that the tracking code will appear in the header section on every page on your site. Quick and simple (as long as you don’t mess up the underlying code!)

 

 

 

google-analytics-wordpress-edit-themes

Any one of these three easy options makes Google Analytics accessible for everyone. It’ll cost you nothing but a little of your time, and before you know it you’ll have a thorough understanding of your site’s visitors. It can help you build a real picture of what’s working for your target audience, and empower you to make informed decisions about your digital presence!

Comments ( 3 )

  1. Emmanuel chill

    November 2, 2017

    Many find it difficult getting to set up google analytics on their PC including me, lol........
    But after going through this post, I found out it's so easy to get it running.

    Thanks for giving this simple and easy steps.

    Cheers!

  2. Prosperity Kenneth

    August 4, 2017

    Honestly, this is an awesome piece, I never knew getting started with Google analytics is this easy. Thanks :)

  3. Andrew McCauley

    July 12, 2015

    Another great option is Google Tag Manager. I use that along with DuracellTomi's Google Tag Manager for WordPress (https://wordpress.org/plugins/duracelltomi-google-tag-manager/). It's a powerful combination that offers a flexibility that including the Analytics tag in the theme, through a Google Analytics-specific plugin or hardcoding it.

Join the discussion