How to Install Google Analytics on WordPress

How to Install Google Analytics on WordPress

When it comes to running a website, WordPress is my no.1 option and so far the only one, except for running a forum, in which case I recommend XenForo.

As soon as you are starting your website, you will need to add the Google Analytics tracking code and this tutorial should show you how to install google analytics on wordpress

What does Google Analytics do?

Together with the data from Google Console, the Analytics intel allows you to see in time how your website does. Here is what you can learn from it:

  • how many people visited your website
  • how much time they spent on your website
  • how many actions they performed (the bounce rate)
  • if they achieved any traffic goals (newsletter subscriptions, contact us form sending, product/service purchasing)
  • the countries and cities the traffic comes from
  • who sent you the traffic: the search engines, referring websites, social media etc.
  • and information about the search queries performed, page position in the SERPs and so on.

For instance, when I perform a SEO audit for my clients, I always request access to these 2 sections: Google Analytics and Google Search Console, so that I know exactly what my client is currently ranking for, if they have any traffic issues, a huge bounce rate or incomplete goals.

By knowing where they stand, from a SEO point of view, I can then make predictions for their next search engine optimization strategies and rank them higher in the next weeks.

Why do I care about the stats from Google?

There are other search engines and, clearly, other traffic tracking platforms. You can look into your cPanel account, at the stats from awstats / webalizer, you can use Yandex Metrica etc.

Yet, as Google is right now the dominant search engine which most people use on a daily basis, I’m interested in knowing EXACTLY how Google sees a website and how the traffic flows towards it.

Of course, if you want to use additional data, it’s OK, but these 2 should suffice.

How to Add Your Website to Analytics / Console

First of all you’ll add your website to Analytics, as the Console dashboard uses Analytics to verify your website. There are other ways to do it, but I’m lazy and always like to take a shortcut, as much as possible.

I highly recommend you also use Gmail to handle all your email workflow. This can be easily tied to many accounts and, instead of having to handle 10-20 or more accounts you just log into your gmail.

For instance, I have on my Gmail account all the mailing from multiple websites: themedically.com, creditcardpaymentsnow.com, orthodontic-braces.com and so on.

So, for this go to google.com/gmail and create an account. Do use REAL data, as this will be your main email address to handle your website communication and it shouldn’t give you any surprises in the future.

As soon as your Gmail account is set up, you can now go to google.com/analytics and set your website on it. Choose the HTTPS version, as the HTTP one should be already redirected on the secured setting. Don’t let both of them, as they are seen as 2 separate entities.

You will be taken to a screen that shows you your tracking code. Here is an example:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-638292-12"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-338062-48');
</script>

Don’t use this exact code, as its ID is different from your page’s ID (the UA-638292-12) is the actual ID. If you are using a plugin that shows your Analytics data inside WordPress, you’ll need to specify this ID: the number similar to 638292-12.

How to Install Google Analytics on WordPress with a Plugin

One of the most used plugins for Analytics tracking is MonsterInsights. There are 2 plans: free and paid.

I personally do not recommend you use anything but the normal Analytics Dashboard, as any plugin will add more files and eventually slow down the database/website.

So my recommendation is to use a plugin to add the code into the website’s pages and then see the data in Analytics.

Insert Headers and Footers Plugin

Find the plugin in the WordPress Repository: Insert Headers and Footers. To do this go into your wp-admin installation, PLUGINS > ADD NEW. Look for “insert headers and footers“, INSTALL and ACTIVATE the plugin.

You will find the settings for this plugin in your dashboard, SETTINGS > INSERT HEADERS AND FOOTERS.

As the Google Analytics code needs to be inserted in the header, just take the code (similar to the one I pasted above and add it there).

Hit SAVE and, if you are using a cache plugin, empty the cache. Go to your Analytics account and hit the REALTIME button and OVERVIEW. You should see yourself and check that the code is working.

How to Add Google Analytics on WordPress Without a Plugin

As a WordPress geek, the way I recommend to install Google Analytics on WordPress is to copy-paste the code directly into the header.php file.

In order to do this, simply navigate to APPEARANCE > THEME EDITOR. In the right file list look at header.php and then add the code ABOVE the ending head tag.

HERE PASTE YOUR ANALYTICS CODE
</head>