How to Add Google Analytics Tracking Code to WordPress

How to Add Google Analytics Tracking Code to WordPress

When you think about the greatest pairings of all time, you probably think about duos like Batman and Robin, Mickey and Minnie, and peanut butter and jelly. I mean, you can’t find more compatibility than those couples, right? Then again, there is the very modern duo of WordPress and Google Analytics.

While they might not make the layman’s list of greatest partners, I bet they’d be pretty high up there for WordPress design, development, and marketing professionals.

If you have a WordPress website, then you should have Google Analytics, too. No questions asked. It’s not like the two can’t exist without one another (as is the case in any healthy relationship), but I’d argue that the universe just doesn’t seem right when the two aren’t paired up. The question is: where do you make this love connection happen?

Well, you’re most likely aware of at least one of three options available to help you connect Google Analytics to your WordPress site. Each of which grants Google access to your site so it can start pulling in and reporting on performance data.

Here are those three options:

  1. Use a WordPress plugin to do the heavy lifting for you. Google Analytics+ is a reliable pick.
  2. Add Google Analytics tracking to the backend wp-content folder with a simple plugin.
  3. Manually add the tracking code to your site.

Since we’ve already covered options one and two, let’s do a quick review of the third option to make sure everyone has a way to add Google Analytics to WordPress.

How to Add Google Analytics to WordPress

In eight simple steps, you can have a Google Analytics account created and running on your WordPress site. Ready to get started?

Step #1: Create a Google Account

Create a Google account.

Create a Google account if you don’t have one already.

Step #2: Log in to Google Analytics

Sign into Google Analytics

With your Google account created, you can now sign into Google Analytics. Select your email address and log in.

Step #3: Get Your Tracking Code

Tracking info

Once inside Google Analytics, you’ll need to retrieve the tracking code that has to be manually added to your WordPress site.

Go to the Admin area at the bottom-left corner of your Google Analytics dashboard. You’ll see three columns of admin settings controls. In the middle column, click on Tracking Info and then select the Tracking Code option.

Step #4: Copy Your Tracking Code

Tracking code

The Tracking Code tab will open and provide you with your Google Analytics property’s unique tracking ID along with the full website tracking code that needs to be added to WordPress.

Copy the tracking code in full.

Step #5: Log in to WordPress and Add Tracking Code

Log in to your WordPress website.

Once logged in, go to the Appearance > Editor tab in the sidebar. On this screen, you’ll see all of your site’s files displayed to the right, and each file’s code displayed in the middle of the page.

Unless you’re only planning to track visitor activity on certain pages of your website (which really should never be the case), you’ll want to insert the code somewhere that will automatically apply the tracking site-wide.

The file you’re looking for then is called header.php. Click on it to open the file within the code editor in the middle.

Use your browser’s search function to look for the closing Head tag.

Look for the closing tag within the code. A simple Ctrl + F will help you quickly locate it.

Once you find it, insert your cursor before the tag and paste in your Google Analytics tracking code. Then click on the Update File button below the code editor to save your changes.

Note: You should always create a child theme for your WordPress site before editing theme files. Check out our post How to Create a WordPress Child Theme if you need a recap on how to do it.

Step 6: Wait for Google Tracking to Kick In

While you won’t be able to start tracking traffic on your site immediately (Google advises you wait 24 to 48 hours before relying on any of the data imported into the system), you can at least test the connection you’ve established.

Return to the Tracking Code page in Google Analytics. To the right of your Tracking ID is a Status update. To confirm that the connection is set, click on Send Test Traffic. This will open your site in a small window.

If all is working properly, your Google Analytics account should update to show an active user count of 1. (Google says this can take up to a minute, so be patient.)

Wrapping Up

If you missed Jon Penland’s comprehensive guide to Google Analytics last year, take some time to read through it once everything is set up and ready to go on your WordPress site. It’ll give you a great introduction into what Google Analytics is, what it does, and what you can learn from the insights found within it. And if you want to get even more granular with your tracking, check out Tracking File Downloads With Google Analytics and WordPress.

Over to you: Of the three methods to add Google Analytics to your site, which is your favorite and why?