Where and How to Add Google Analytics Code in WordPress

Want to know where to put your Google Analytics code in WordPress, and how to get it installed in the right spot?

Adding Google Analytics to WordPress is super important. In order to find out how much traffic you’re getting, where it’s coming from, and what those visitors are doing on your site, you need to install Google Analytics correctly.

In this article, we’ll show you where to add Google Analytics code in WordPress, and a few different ways of installing it.

Where to Add Google Analytics Code in WordPress

Your Google Analytics code in WordPress should always go in your header.

MonsterInsights is the best WordPress Analytics plugin. Get it for free!

Specifically, it should go immediately after the <head> element, or at least before the closing </head> tag.

Google Analytics will only track your traffic after their tag is loaded on your page. If the tag is placed somewhere that takes longer to load (like the footer), your visitors could click something on your page or navigate to another page before Analytics is even able to capture their visit.

So, adding the code anywhere but the header can result in incorrect data.

How to Add Google Analytics Code in a WordPress Header

There are many ways to add your Google Analytics code snippet to your WordPress header. We’re going to take a quick look at three different ways:

  1. Use a Google Analytics plugin to install your code in a couple clicks
  2. Use a code snippets plugin to easily install your code along with other snippets if needed
  3. Add your code manually to your WordPress header file

Method 1: Use MonsterInsights to Add Google Analytics Code

The easiest way is to set up your Google Analytics with your WordPress site using the MonsterInsights plugin.

MonsterInsights is the best Google Analytics plugin for WordPress. It allows you to easily connect your WordPress site with Google Analytics so you can view all the data that matters most right in your WordPress dashboard.

MonsterInsights Home Dashboard

Plus, with the click of a button, you can set up sophisticated tracking features such as event tracking, eCommerce tracking, form tracking, custom dimension tracking, outbound link tracking, and more.

MonsterInsights makes it easy to add your tracking code into your WordPress header without even touching the code. All you have to do is install the plugin on your site, and it will take care of the rest.

The best part is, if you don’t currently need any of MonsterInsights’ advanced features, you can use it for free. Simply head to your WordPress dashboard and click on Plugins » Add New. Search for MonsterInsights, then click Install and Activate.

Install MonsterInsights Google Analytics for Free

Once the plugin is active, follow the setup wizard, and you’ll have Google Analytics tracking code in your site’s header in no time.

setup wizard

Or, to grab our most-loved features like eCommerce tracking, form tracking, video tracking, and much more, follow our detailed step-by-step instructions on how to properly add Google Analytics to WordPress.

After setting up Google Analytics, you can see all your important stats inside your WordPress admin area. MonsterInsights dashboard reports make it very easy to spot the data you need without having to leave your site.

MonsterInsights Overview Report

Method 2: Use a Code Snippets Plugin

A code snippets plugin is another great way to install your Google Analytics code. While it doesn’t include an analytics dashboard, WPCode is a great code snippets plugin with a lot of other great features.

With WPCode, installing your Google Analytics code takes just one step. Just open the Header box, paste your code, and click Save. That’s it!

WPCode Google Analytics Installation - WordPress Header

You can also start with WPCode for free.

Method 3: Manually Add Google Analytics Code to Header

The final method for adding Google Analytics tracking code to your website’s header is without using a WordPress plugin.

This method is tricky, and we don’t recommend it for beginners. That’s because it can be tricky to add code, and any mistake can mess up tracking. Besides that, you’ll have to enter the tracking code every time you update your website theme.

That said, to manually enter the tracking script, the first thing you need to do is copy the code from Google Analytics. Find it by heading to the Admin cog, then Data Streams. Click on your data stream.

From there, click Configure tag settings » Installation Instructions. Then, you’ll be able to copy your tracking code.

After that, go to your WordPress website admin area and then navigate to Appearance » Theme Editor. Next, click the Theme Header (header.php) option under Theme Files. Then, paste your code into your header (at the beginning or before the closing </head> tag) and click the Update File button.

Google Analytics script before closing head tag

You’ve now manually entered the tracking code to the header of your website. Google Analytics will now start tracking your visitors and show you reports to see how they interact with your site. Just remember, your code will be wiped out if you update your theme.

Google Analytics Code in WordPress FAQ

Will the Google Analytics tracking code slow down my site?

The tracking code loads on your site asynchronously, meaning other scripts can also load simultaneously. For that reason, adding your tracking code into your header doesn’t reduce the page speed or impact your WordPress site’s performance.

Can I add the Google Analytics code to my header or footer?

Google Analytics should always be installed in your header. Installing the code in your footer can result in incorrect data.

Why use a Google Analytics plugin to install Google Analytics code in WordPress?

Using a Google Analytics plugin to install the Google Analytics code in WordPress is super easy for beginners, and it comes with other tracking features you don’t have to set up yourself.

Another benefit of using a WordPress plugin is that if you need to update your WordPress theme, you won’t have to remember to reinstall your Google Analytics tracking code.

And that’s it!

We hope you liked our guide on how to add your Google Analytics code to WordPress. If you found this helpful, check out our post on how to track link clicks in WordPress.

Not using MonsterInsights yet? What are you waiting for?

And don’t forget to follow us on Twitter, Facebook and YouTube for more helpful Google Analytics tips.

Want to Try MonsterInsights for Free?

Enter the URL of Your WordPress website to install MonsterInsights Lite.

Comments

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

This form is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.