How to Add Google Analytics to Your Hosted WordPress Site (Two Methods)

Google Analytics is a free analytics tool for websites that track traffic in real time, along with data on demographics, such as, location, age, sex, and much more.

In this post, I walk you through two different methods to add Google Analytics to your self-hosted WordPress website/blog: by directly adding the code to your header file, and by using a plugin.

There are a lot of benefits to having all these data.

Data helps you create content for your target audience. You may create content specifically for women between 25-55, but how do you know if your content is reaching the right demographic?

Enter Google Analytics!

Why Should You Use Google Analytics

Let’s say, you create content targeting females between 35-55, but turns out that your data is, in fact, reaching more males between 35-55, then well, something needs to be done. Either you re-analyze your content and see what you may be doing wrong, or you could instead decide to focus on the traffic you are already getting, and see how to improve your content for the existing demographic.

Either way, you need to know who you are reaching to see if you’re on the right track.

For bloggers specifically, these data can be extremely helpful. For example, you can see which posts are getting the most traffic, and use this data to repurpose your content or create an info product that’s like an extension to that blog post.

These are just a few examples of how analytics can be of use to you. Needless to say that if you have a website, you should definitely have a way to track your traffic.

And by far, Google Analytics is the best analytics software out there. AND IT’S FREE!

**Attention Jetpack Users**

For WordPress users, you may be tempted to use Jetpack, a multi-purpose plugin that comes with most auto installers, to track traffic. But let me tell you, Jetpack data is USELESS. Yes, it’s useless. Almost always Jetpack will give you incorrect data, so if you’re thinking that you already have traffic data through Jetpack, and you don’t need Google Analytics, then think again!

How to Add Google Analytics to WordPress

Adding Google Analytics to your WordPress site is pretty straightforward. Typically you can do it by either adding the analytics script directly to your header.php file or by adding a plugin. We will look at both. But first, let’s take a look at how to get the Google Analytics code in the first place.

How to Set Up Google Analytics

  1. Go to https://analytics.google.com
  2. If you’re not logged in already, it will ask you to log in to your Google account.
  3. Once you have logged in, click on admin located on the bottom-left of your screen.
  4. On the next screen, click on the account drop-down, then click on create a new account.
  5. On the next screen, follow prompts, and fill out all of the information: Your account name, your website name, website URL. Choose industry category and time zone. Leave all other boxed to their default state (they should all be checked). Make sure that at the top of the page, the website option is highlighted. Review all the information to make sure they are correct. Then click on the blue button that says “Get Tracking ID”.
  6. A Terms of Services pop-up should open up. Click “I Accept”.
  7. On the next page, you should see the tracking ID as well as the script to be added to your site.

Now you’re ready to add this script to your website.

Figure 1: Sign up to Google Analytics and click on “Admin”.
Figure 2: Create a new analytics account.
Figure 3: Follow prompts and fill out your website information.

 

Figure 4: After you have filled out all necessary information, click on “Get Tracking ID”.
Figure 5: Get Google Analytics tracking code and ID.

Method 1: How to Add the Google Analytics Script to header.php File

This method is for those who are comfortable with customizing theme files.

[Note that if you have a theme from WordPress theme foundry, they may be prone to periodic updates, in which case, all your custom codes will be gone when you update the theme. if that is the case, you will need to add the code to your header file each time you update your theme, or just use method 2 below.]

This method is useful and preferred as this minimizes the number of different files being parsed when your site is loading. Remember, the more plugins and different sources of files and scripts your site has to load, the slower it gets.

Before we start adding codes to your header file, make sure to create a backup of your site. You can use a plugin such as Updraft Plus to create a backup of your site very easily.

Now then, for the new gtag.js code, it needs to be added right after <head> on your header file.

For most themes, you can locate this header file here: Dashboard > Appearance > Editor, and then choose the header.php file from the list of files on the right, as shown in Figure 5.

Then locate <head> and right under this <head> tag, you will be inserting the gtag.js code.

Head over to the Google Analytics page where you got your tracking code (as shown in Figure 4). Copy the code and paste it under <head>. Make sure that this code is the first thing under the opening <head> tag, as shown in Figure 5.

Click on Save. You’re done!

Figure 5: Paste the gtag.js code right after the opening

Method 2: How to Add the Google Analytics Using a Plugin

If you are using a theme that updates periodically, or if you are not comfortable handing any of the theme files, then it is best to use a plugin.

The plugin I use most if GA Google Analytics.

Go to Dashboard > Plugins > Add New, then search GA Google Analytics. Choose the right plugin from the search results (shown in Figure 6), then install and activate the plugin.

Now go to Plugins, and under GA Google Analytics, click on Settings (shown in Figure 7).

IN the next page, click on “Plugin Settings (shown in Figure 8). On Google Analytics, you were given a tracking ID (shown in Figure 5), copy this ID, and paste it into the plugin settings, as shown. Make sure to choose “Global Site Tag/gtag.js (new method)” next to tracking method, and then “via wp_head” next to code location, as shown in Figure 8.

Scroll down and click “Save Changes.”

Figure 6: Install and activate plugin GA Google Analytics.
Figure 7: Go to GA Google Analytics Settings.
Figure 8: Copy and paste the tracking ID, choose the correct options, and save the plugin settings.

That’s it! You’re done!

If you have any questions, feel free to leave a comment, and I’ll try my best to help!

Thanks for reading, and if you’ve found this blog post to be of any help to you, consider supporting me by purchasing something from my shop. 🙂

In this post, I walk you through two different methods to add Google Analytics to your self-hosted WordPress website/blog: by directly adding the code to your header file, and by using a plugin.

  • 73
  • 5
  •  
  •  
  •  
  •  
    78
    Shares
  • 78
    Shares

Comments: 2

  1. Wow! How to Add Google Analytics to Your Hosted WordPress Site (Two Methods). This is so great, Maybe I can do this (Two Methods) in my work. Thanks for sharing your blog Maliha.

A penny for your thoughts?

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

Scroll Up