How to Add Google Analytics to WordPress

. No Comments

Learning how to add Google Analytics to WordPress is a very important part of running your own website. The software can give you valuable information about what is and isn’t working on your site and lets you make informed decisions about its future.

In this tutorial, we’re going to answer several questions about Google Analytics, including:

  • What is Google Analytics and Why Do You Need It?
  • How to Sign Up to Google Analytics?
  • How to Add Google Analytics to WordPress?
  • How to Install Google Analytics with a WordPress Plugin?

Let’s get started.

What is Google Analytics and Why Do You Need It?

Before getting into the technical stuff, let’s first talk about the subject matter of this post – Google Analytics. If you’re a complete newbie, you might not even be aware of what it is and why it’s a good idea to connect it to your WordPress website. So, let’s cover that first.

1. Google Analytics Helps You Get to Know Your Audience

Google Analytics is a web analytics suite that helps you understand and analyze your web traffic. It’s present on over 50 million websites around the world.

The way it works is that you add a piece of JavaScript code to your page (more on that below) that allows Google to track the activities of visitors coming to your site. It will then collect these activities and present them in the form of statistics in your Google Analytics account.

Google Analytics example

Here’s what you can learn from the statistics:

  • Who is visiting your site? — Geographical and technical information about where your audience is from and the devices they use to access your site.
  • What do your visitors do? — Find out which pages they look at, your bounce rate (meaning the share of visitors who leave after just one page), how long they stay on your site and more.
  • When are they coming by? — Google Analytics also shows you when exactly people come to your site by the hour, day, week or month.
  • How do they find your site? — You also get information about whether your audience finds your site in search engines (and which of them), social media, links on other websites or if they enter your site address directly.
  • How do they interact with your content? — From Google Analytics you are also able to understand how people interact with your WordPress site, for example, the links they click on. If you define certain goals (like converting visitors to email subscribers) you can also track that.

2. Use The Information To Optimize Your Website

What can you do with this information? How is that useful to you? Knowing the above allows you to do many things.

  • Adjust your site to serve your audience better. For example, by improving technical aspects or adding a language.
  • Optimize traffic sources – invest more in those that work particularly well or ramp up your efforts on those that don’t.
  • Improve your content and content strategy to give visitors more of what they are interested in.
  • Track marketing campaigns and see if they are working.
  • Find your worst-performing pages and take steps to make them better.

In short – web analytics allow you to eliminate guesswork and make decisions about your site based on facts and hard data instead of gut feeling. Google Analytics is completely free to use, so it doesn’t contribute to website costs.

Before adding it to your WordPress site, we first need to talk about some caveats.

Google Analytics and GDPR Compliance

Here’s how to use Google Analytics in WordPress in a post-GDPR world:

  1. Audit your site — One of the most important things is to make sure you don’t accidentally send personal identifiable information (PII) to Google Analytics. These are email addresses, home addresses, birth dates – anything that can be used to personally identify a user. More info about that topic here.
  2. Turn on IP anonymization — Under the new law, an IP address is also considered PII. To be on the safe side, you can switch on IP anonymization in Google Analytics. Find instructions on how to do that in this post.
  3. Include a privacy policy — With GDPR, every website needs to have a privacy policy in plain language that explains all the things about data collection mentioned above. Same goes for cookie usage and contact forms. Detailed tips in this article.
  4. Implement GDPR-compliant cookie consent — For all the non-strict necessary cookies, you need to get consent before the cookie is set in the user browser. It must include clear information about what the cookie is for, users must still be able to use your site even if they opt out, they must have access to the cookie settings and you need documentation that they accepted it. There is a number of plugins for this in the WordPress directory.

For more general information on GDPR and what it means for the website owners, check out this article and this one.

Now that we have this out of the way, we can finally get to the main part of this post.

How to Sign Up to Google Analytics

Before you can connect the software to your site, you first have to sign up. We will do that first by heading on over to the Google Analytics homepage.

Create Google Analytics account

If you already have a Google account, simply use it to log in. If not, click the button to sign up and create a whole new account. Once done with either, you will see the screen below.

Set up Google Analytics account

It pretty much already shows the process we will go through. Hit Sign up to get started. On the next screen, when given a choice between mobile app and website, make sure to choose website. After that, you need to insert your details.

Specify account details

Note that your account can contain several websites you track. Consequently, an appropriate account name can be something like Business Websites. Website name, address, industry category, country and time zone should be self-explanatory. Under Data Sharing Settings, you can choose whatever you find appropriate.

Once done, click Get Tracking ID to continue. You might have to agree to Google Analytics’ terms and conditions in the next step. After that, you will see the page with your tracking code and tracking ID.

Google Analytics tracking code and ID

For now, that is all we wanted to achieve.

In the next step, we will add the tracking code to your WordPress site. To do so, you might either need the code or the tracking ID, so it makes sense to leave the browser tab open. In case you close it and need to get there later, you can always find your tracking code under Admin > Tracking Info > Tracking Code.

A Quick Tour of the Program

Before we get to how to add the code to your site, let’s take the opportunity to have a quick look around the main Google Analytics interface. That way, you get a better understanding of the type of information you can find there. You can get there by clicking the Home button.

New Google Analytics account

On the right, you will always see the statistics for your website, while on the left you can choose which data to look at. The part of the program where you will spend your most time is the Reports tab. You will find a number of different sections that, if clicked on, contain other submenus and contain the following information:

  • Real-time — Shows you what is going on on your site at the moment of the observation. How many visitors are currently on it, where they are from, what they are looking at and more.
  • Audience — Contains information that helps you learn more about your users e.g the number of visitors in a period of time, how long they stayed on average and the demographical as well as the technical information about them.
  • Acquisition — Shows you which sources send users to your site and information about your marketing channels.
  • Behavior — Everything about what your users do after they come to your site, the content they look at and more.
  • Conversion — If you have set up goals inside Google Analytics, here’s where you understand how well you are doing in achieving them.

All good so far? Then let’s get down to how to add Google Analytics to your WordPress site now.

3 Ways to Add Google Analytics to WordPress

In order for Google Analytics to start tracking your visitors, the code above needs to be present on every page of your site you want to do that on. Usually, that’s all of them. Here’s how you can achieve it manually.

1. Insert the Code Into header.php

One of the main ways to add the tracking code to your site is to insert it into your header. This way, it will load on every page.

Thanks to the way WordPress is set up, this is very easy to achieve. Most standard themes have a header.php file that is responsible for outputting the site header section. So, you can simply input the Google Analytics code here.

However, when you make changes to theme files, be aware that it’s always best to do it in a child theme. Otherwise, they will get lost when you update your main theme. Child themes are super useful in general and you should definitely read further about them.

Once you have created your child theme, simply copy the header.php from the main theme into it and start editing it. Then copy and paste the tracking code from Google Analytics into it right before the closing </head> bracket.

Add Google Analytics tracking code to WordPress header file

Also, make sure the code is wrapped in <script> brackets! Otherwise, browsers won’t recognize it for what it is. That’s it! Now save and upload the file back to your server.

Easy, right? Alternatively, you can also use a plugin like Head, Footer and Post Injections to insert the script into your header. Some themes also have native functionality for doing so.

2. Use functions.php

Another possibility to add Google Analytics to WordPress without a plugin is to use the functions file. You can do the same thing as we did above. Instead of adding the tracking code to header.php directly, insert it into the head section via a function.

To do so, simply edit your (child) theme’s functions.php file and add the following piece of code (use your own tracking ID instead of mine):

function ns_google_analytics() { ?>
  <script async src="https://www.googletagmanager.com/gtag/js?id=UA-121810903-1"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', 'UA-121810903-1');
  </script>
  <?php
  }
  
add_action( 'wp_head', 'ns_google_analytics', 10 );

Save, upload and from that moment, Google Analytics should start tracking.

3. Using a Custom Plugin

A third option to manually add the tracking code to your site is creating your own plugin. This also gives you the abilitiy to switch tracking on and off from the WordPress back end. Plus, it makes sure that the Google Analytics tracking code stays on your site even if you change themes.

If you have never created a plugin before, don’t worry – it’s really easy! Before we get to it just a quick note: for the below to work, your active theme needs to have the wp_head hook inside header.php. That’s usually implemented by default but it’s worth checking!

Once you’re done, all you need to do is create a new PHP file. Take the text editor of your choice, make a new file and save it as something similar to nick-google-analytics.php. Then, add the following code to it (hint: it’s the same function as before plus a header that lets WordPress know this is a plugin).

<?php
/*
Plugin Name: Custom Google Analytics Plugin
Plugin URI: http://websitesetup.org
Description: Add the Google Analytics tracking code your site header.
Author: Nick Schäferhoff
Version: 1.0
*/

 function ns_google_analytics() { ?>
  <script async src="https://www.googletagmanager.com/gtag/js?id=UA-121810903-1"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', 'UA-121810903-1');
  </script>
  <?php
  }
  
add_action( 'wp_head', 'ns_google_analytics', 10 );

Be sure to replace the sample tracking code (everything between <script> and </script>) with your own. Otherwise, it won’t work. Also adjust the header to whatever you want.

Once done, save the file and upload it to your site’s plugin folder (wp-content > plugins). You can also give it its own subfolder if you like. When you are done, activate it from the WordPress back-end like any other plugin.

Activate Custom WordPress plugin for Google Analytics tracking code

While the above isn’t very complicated, an even easier way is using one of the many available plugins. We will go over the process with one example now and later look at a few more plugins that can do the same.

How to Install Google Analytics with a WordPress Plugin

For this tutorial, we will use Google Analytics for WordPress by MonsterInsights. It is the most popular plugin in the directory for this purpose with more than two million installs. It’s also really easy to use and comes with a module that helps make it GDPR compliant (unfortunately, only in the premium version).

Let’s go!

1. Install the Plugin

Naturally, whenever you want to use a plugin on your WordPress site, the first step is to install it. For that, simply go to Plugins > Add New. Search for it by name (looking for google analytics should be enough).

Google Analytics WordPress plugin

When you find it on the list, hit Install Now to start downloading it to your site. Activate when it’s done.

2. Connect to Google Analytics

When the plugin is active, you will find a new menu item called Insights in your site’s dashboard. Click it to go to the Settings menu.

Authenticate MonsterInsights with Google Analytics account

Click on Authenticate with your Google account. This will redirect you to the Google Analytics page. You will be asked to sign in or, if you already are, to select the account you want to use for tracking.

Sign into Google Account

Once you’re logged in, Google Analytics will ask you to give permission for the WordPress plugin to access your data.

Allow plugin to add Google Analytics to WordPress

This is necessary for it to work, make sure you allow it. After that, you still need to select the profile you want to track. Select the one you created earlier and complete the authentication when you have done so.

That’s it! You will be directed back to your site. Told you it was gonna be easy!

2.1 Alternative Method

This isn’t the only way to add Google Analytics to your WordPress site with this plugin. You can also enter your tracking UA code manually. For that, simply click on Click here to authenticate manually.

Manually enter Google Analytics tracking ID into WordPress

You will find your UA code as the tracking ID in your Google Analytics account.

Google Analytics tracking ID

Copy and paste it in the field in the plugin settings. Then save your changes. Done!

Just be aware that you are not able to use the MonsterInsights reports in your WordPress back-end this way.

3. Adjust Tracking Settings

Once connected, you can click on the Tracking tab on top to make changes for how the app tracks your visitors.

MonsterInsights tracking settings

Let’s cover the most important quickly:

  • Engagement — Options to let you ignore site users by user role and enable event tracking (meaning clicks on the links)
  • Demographics — Switch on demographics and interest reports. More importantly – it’s where you can switch on IP anonymization.
  • Permissions — Let’s you control who on your site can view the reports and change settings, whether you want the plugin to update automatically, track usage statistics and display announcements.

There are a few more minor things you can change e.g handling affiliate links and more. Unfortunately, the majority of options are part of the premium version of the plugin.

4. Check the Reports

The cool thing about MonsterInsights is not only that it’s easy to use but also that it pulls Google Analytics data directly into the WordPress dashboard. You can see it under Insights > Reports.

MonsterInsights reporting

Be aware, that it takes a while for Google to display any data. Therefore, this part might be empty at the moment just like in your Google Analytics account. Generally, it takes 12-24 hours before data starts appearing, simply check back later.

Google Analytics and WordPress in a Nutshell

Google Analytics is an important tool for improving your WordPress site. It can give you invaluable information on your audience so you can serve them better.

In the post above, we talked about everything you need to know about this web analytics suite, including what you need to think about in terms of data privacy laws.

Aside from that, as you have seen, adding the analytics to your site is not complicated. In essence, you just need to sign up and add the tracking code to your site.

The latter you can do manually either by adding it to template files, including it in a function or a custom plugin. Plugins offer an even more convenient way as you have seen. There are also many different options for that.

By now, you are ready to add Google Analytics to your WordPress site. We hope it will help you make your website even more successful!

Any thoughts on Google Analytics and WordPress? If so, please share in the comments section below.


Want to ask a question or leave a comment?


Ask me anything

captcha



  • +

0 comments

Leave a Reply

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

*