How to Add Google Analytics Tracking Code to Your WordPress Blog (Two Methods)

  •  
  •  
  •  
  • 894
  •  
  •  
    894
    Shares

A step-by-step guide to adding Google Analytics to your self-hosted WordPress site by two methods: via your header.php file, or by using a plugin.

Do you know what the most important thing is for a business owner?

Oh wait, I’ve got something better!

Do you know what the most important thing is for a blogger turned business owner?

It’s the knowledge of who your audience is.

Learning about your audience is tricky business. I’ll have to dedicate a whole blog post to this very topic on another day. However, I want to point out one thing that you ABSOLUTELY must have access to if you want to learn about your audience, and more importantly, the way your audience interacts with your blog.

It’s a set of data that gives you insight into your audience’s behavior on your blog. For example, how do your readers find your blog? How long do they spend on your blog? Do they stick around and check out different pages and posts? Or do they leave your site within seconds never to come back again? Which blog posts do they tend to visit most often? And much, much more!

All these data will help you understand your readers better and the more you understand them, the better you can serve them.

But how DO you get these data in the first place?

Enter Google Analytics!

Why You Should Use Google Analytics

Let’s say that you’re a mom blogger and you create content targeting females between 22-45, but turns out that your blog is, in fact, reaching more males between 25-45… well, somethings’ gotta give, huh! These data will tell you many things about not only your audience but perhaps your content as well. Maybe a better look at your content will reveal why you’re reaching more men than women? Could it be that your posts are more about parenting itself than motherhood?

Once you have these data, you can use them to your benefit in a multitude of ways. For example, you may discover that you need to create more content geared towards motherhood than parenthood in general. or maybe you want to retain your current readership and focus more on creating content that parents, in general, will benefit from, and not just moms.

The potentials here are limitless!

Either way, the point is, 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 called content upgrades and may be used to attract more readers, get subscribers (if you offer these content upgrades as opt-in freebies), and more.

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, free analytics software out there. And did I mention that 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!]

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

(Refer to figures 1-5 for the following steps in this section.)

  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 6.

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 5). 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 6.

Click on Save. You’re done!

Figure 6: 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 7), then install and activate the plugin.

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

IN the next page, click on “Plugin Settings (shown in Figure 9). 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 9.

Scroll down and click “Save Changes.”

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

That’s it! You’re all done!

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

It's time to grow your blog already!

Sign up for weekly tips on blogging, branding, design, business, and monetizing your hobbies and skills + Subscribers get access to the library of epic freebies! 

Your email is safe here; no spam, like, EVER! You can unsubscribe at any time.

A step-by-step guide to adding Google Analytics to your self-hosted WordPress site by two methods: via your header.php file, or by using a plugin | The Side Blogger

A step-by-step guide to adding Google Analytics to your self-hosted WordPress site by two methods: via your header.php file, or by using a plugin | The Side Blogger
A step-by-step guide to adding Google Analytics to your self-hosted WordPress site by two methods: via your header.php file, or by using a plugin.

Maliha

Maliha created The Side Blogger as an experiment to see if turning a blog into a profitable business is a possibility for moonlighting side bloggers. Learn more here, and connect with Maliha on Medium.

  •  
  •  
  •  
  • 894
  •  
  •  
    894
    Shares

2 thoughts on “How to Add Google Analytics Tracking Code to Your WordPress Blog (Two Methods)”

  1. Hi I’m Kiki. I just wanted to say THANK YOU!
    I’ve been going on my analytics for weeks now wondering why nothing has shown up… Turns out I never added the code o the editor…
    Thanks so much <3

    1. Hi Kiki, OMG, this is funny LOL

      BUT, I’m glad it’s all sorted now 🙂

      Hope you’ll come back to my little blog again!

      🙂

Leave a Comment

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