How to Add Google Analytics to WordPress (Manually and via Plugin)

How to Add Google Analytics to WordPress (Manually and via Plugin)

As a website owner, something that you should definitely be familiar with is how to add Google Analytics to WordPress. Web analytics allow you to understand how visitors interact with your site. That information enables you to make improvements and create an even better user experience.

For that reason, this article will show you everything you need to know about this topic. It will explain what Google Analytics is and why you should use it, talk about privacy concerns in connection with the new GDPR rules and several ways how you can add Google Analytics to WordPress both manually and via plugin.


Ready to go? Then let’s.

What is Google Analytics (And Why Use It)?

Before getting to the how-to part, let’s quickly cover what we are talking about. If you don’t know what Google Analytics is and what you can use it for, this part is for you.

Get to Know Your Audience

Google Analytics is a popular suite of web analytics tools present on literally millions of websites. One of the main reasons for its prevalence is that it is available for free. However, what does it do?

Basically, Google Analytics collects information about your web traffic and makes it available to you. For that to happen, all you need to do is add a piece of JavaScript code to your site (more on that soon), which enables the software to track every visitor on your website and what they do. After it does so, Google Analytics turns this information into graphs and statistics that you can access inside your account.

In particular, you can learn the following:

  • Who are your visitors? — Where are they located in the world? What devices do they use to access your website?
  • What do they do on your site? — See which pages they frequent, how many of them leave after just one page (the so-called bounce rate), how long they stick around, which links they click and other interactions with your site. You are also able to set goals (i.e. email subscriber conversions) and track them.
  • When are they coming by? — Learn when your site is particularly popular, broken down by hour, day, week and month.
  • How do they find you? — Google Analytics can tell you what your primary traffic sources are: search engines, social media, links or direct traffic

Use This Information to Improve Your Site

You might be thinking, “that’s all fine and dandy, but how does that help me?” Well, knowing the above allows you to further optimize your site in many ways, such as:

  • Improve the user experience for different kinds of devices if needed
  • Optimize under-performing traffic sources or establish more of those that perform well already
  • Improve your content strategy to give visitors more of what they want
  • Make under-performing landing pages better
  • Track the performance of your marketing campaigns

In short, learning how to add Google Analytics to your site means you no longer have to guess what would make your site better. Instead, you can take steps based on facts and data to improve what you have to offer. However, collecting this data also comes with some responsibility. Let’s talk about that next.

Google Analytics and GDPR

By now, you are probably aware that we recently saw a new European rule on consumer data protection come into effect. It is called General Data Protection Regulation. This law has consequences for anyone who is active in the European market in any way. If that is you (which is likely), you have to make sure your use of Google Analytics is compliant with it.

  1. Don’t send any Personal Identifiable Information (PII) — This means things like email or home addresses, phone numbers or anything else that can be used to personally identify someone. It’s crucial that you audit your site whether you are passing any of this kind of data on to Google. More on that in this article.
  2. Enable IP anonymization — PII extends to IP addresses. For that reason, to be on the safe side, it makes sense to switch on anonymization within Google Analytics. Find out how here.
  3. Include a privacy policy — Under the new law, every website needs their own privacy policy. This document needs to lay down in plain language everything you are doing concerning collecting and processing user data, including for cookies and contact forms.
  4. Make your cookie consent GCPR compliant — For all cookies that are not strictly necessary, you need to get consent from your users to use them. Your consent solution must include information about the purpose of the cookies, ensure that visitors can still use your site even if they decline, provide access to cookie settings and document their acceptance. There are several plugins in the WordPress directory for that.

For more information on GDPR and what it means for your site, We recommend this post. Alright, now we can finally get to what you came here for.

How to Add Google Analytics to WordPress Manually

In order to use Google Analytics on your site, you first have to sign up to an account. You can do this on the Google Analytics homepage. Either sign into your existing account or follow the instruction to create a new one. This will give you access to your tracking ID and code, which you will need soon. You can find it under Admin > Property > Tracking Info > Tracking Code.

Method #1: Add the Tracking Code to header.php

One of the easiest ways to add Google Analytics to WordPress is to insert the tracking code into the header file. That way, it will load on every single page. This is an especially good solution if your theme is using the standard header.php file.

Be aware, however, that the best idea to implement this is in a child theme. Otherwise, you will lose your changes to a theme update. So, either open up header.php in your main theme or copy it to your child theme folder and then edit it. After that, copy the tracking code from your Google rel=”nofollow”Analytics account and paste it right before the closing </head> bracket.

Don’t forget to include the <script> brackets so browsers know what it is. Save the file, upload it to your theme and you are done.

Method #2: Use functions.php

Another possibility of how you can add Google Analytics to WordPress manually is functions.php. It works the same way as above only that you add the analytics code to your header with a function instead of inside the file.

To achieve this, edit the functions.php file of your theme or child theme and add the tracking code like so:

Be sure to change the details so it uses your own tracking code! After that, save, upload and you are done.

Installing Google Analytics via a WordPress Plugin

Of course, WordPress also gives you the ability to add Google Analytics through a plugin. There are several options and for this tutorial, we will be using GA Google Analytics. It makes connecting your site very easy, is fast and not as bloated as other plugins on the market. Plus it supports IP anonymization for GDPR compliance and other privacy settings. You will find more plugin options at the bottom and I recommend you compare to find your favorite solution.

1. Install the Plugin

In order to use the plugin, the first thing you need to do is install it on your site. For that, go to Plugins > Add New and write its name into the search bar.

Find it in the results list and click Install Now to start the download onto your site. Once done, hit Activate and you are done.

2. Input Tracking Code

With the plugin installed, you can find its configuration options under Settings > Google Analytics. Alternatively, click on Settings under its name in the plugin list. Both will take you to the same screen. There, all you need to do is click on Plugin Settings either at the top or bottom to open up the settings.

The next thing you will need is your Google Analytics Tracking ID. As mentioned, you find it in your account under Admin > Property > Tracking Info > Tracking Code. This time you don’t need to copy the entire code but only the ID at the top. In addition to that, pay attention to the name of your tracking code, whether it’s gtag.jsanalytics.js or ga.js.

Now go back the plugin settings on your WordPress site. Input the tracking ID at the top of the plugin settings where it says GA Tracking ID. After that, be sure to choose the correct tracking method according to the name of your tracking code.

3. Configure the Rest of the Plugin

Once you have done that, it’s time to move on to the rest of the settings. Let’s go over them real quick. Not that some of the settings are only available for certain types of the Google tracking code.

  • Display Advertising — This will enable Google Analytics data for the Google Display Network.
  • Link Attribution — Enhanced Link Attribution is a feature that lets you track which link to the same URL users click on a single page. You can enable it here.
  • IP Anonymization — Check this box to enable IP anonymization, recommended for anyone who has to obey the new GDPR.
  • Force SSL — Switch this on to send all data via secure HTTPS connections, even if your site is not on HTTPS yet.
  • Tracking Code Location — Determine whether to load your tracking code in the header or footer of your pages. Google recommends the former but moving the script to the footer can increase page performance.
  • Custom Tracker Objects — Here you can add code to collect custom data. More information here.
  • Custom GA Code — Add any custom code here that you want to include in the Google Analytics tracker.
  • Custom Code — If you have any other markup to execute together with the tracking code, you can input it here.
  • Custom Code Location — This option lets you define whether to execute any custom code before or after the Google Analytics tracker. The default is after.
  • Admin Area — Enable this to include user behavior in the WordPress admin area in your Google Analytics data.
  • Admin Users — Here you can disable tracking for admin-level users on your site, I would recommend doing so.

Once you have configured everything to your liking, don’t forget to save the changes for them to take effect. After that, it can take up to 48 hours before you see data in your analytics account. However, that’s it. Well done, you just learned how to add Google Analytics to WordPress!

Fancy you stumbling on my piece of the internet. Bonjour!

My name is Anmol and I'm the Blogger-In-Chief of this joint & working as the Chief Technology Officer at Azoora, Inc. I'm putting up my views here trying to help creative solopreneurs, developers & designers build their business using the power of websites, apps & social media, this, is, my jam.

If you're looking to start your own online business with a professional high quality website or mobile app, just get in touch. I'd be more than happy to assist.

SKYPE | FACEBOOK | LINKEDIN | TWITTER | EMAIL

2 comments

  1. pisinaspa χημικα πισινας says:

    Hello there! This is my first comment here so I just wanted to give a quick shout
    out and say I truly enjoy reading through your articles.
    Can you suggest any other blogs/websites/forums that cover the same subjects?

    Thank you so much!

  2. Anonymous says:

    Hmm it looks like your website ate my first comment (it was
    super long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog.
    I as well am an aspiring blog blogger but I’m still new to
    the whole thing. Do you have any recommendations for novice blog writers?
    I’d definitely appreciate it.

Leave a Comment

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