should you put code in wordpress header or footer

Should You Put Google Analytics Code in WordPress Header or Footer?

Want to know where should you put your Google Analytics code?

While it’s safe to put your code in the header or footer, Google recommends you to add your code to your WordPress header.

But how can you add the code without the need to hire a developer?

In this article, we’ll explain should you put Google Analytics code in the WordPress header or footer?

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

Let’s start with…

The Benefits of Adding Analytics Code into Header

If you add your Analytics tracking code to your footer and run a lot of other JavaScripts on your site, chances are Analytics won’t accurately track your traffic data.

It is mainly because JavaScript snippets are triggered in the order they are embedded in your page. Your page may prevent running your footer scripts due to other JavaScript errors or performance issues of other scripts/components.

This is why Google recommends you add your tracking code in the header of your site. It helps you to accurately record traffic stats because your header scripts are loaded earlier than any other scripts on your page.

Besides, 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 performance.

Now, are you ready to learn how to add the tracking code in your website’s header?

Video Tutorial

If you don’t want to watch the video, then continue reading and go through our written tutorial.

How to Add Your Google Analytics Code into Header

There are many ways to add your Google Analytics code snippet to your WordPress site. First, you can use a WordPress plugin like MonsterInsights to add the tracking code without editing code or hiring a developer.

Or you can manually enter the code to your website. This method isn’t recommended as it requires going through your website’s code. And if you make any mistake while entering the code, it could have adverse effects on your site’s performance.

That being said, let’s look at both methods.

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

monsterinsights best analytics plugin for wordpress

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.

Another benefit of using a WordPress plugin is that if you decide to update your WordPress theme in the future, you won’t have to reenter the Google Analytics tracking code.

setup wizard

You can 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 insides 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: Manually Add Google Analytics Code to Header

The next 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 change 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. You can find the code by going to admin settings and navigate to Tracking Info » Tracking Code.

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

theme header

Now paste the Google Analytics tracking script you copied earlier before the </head> tag and click the Update File button.

enter tracking code in header

And that’s it!

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.

We hope you liked our guide on should you put Google Analytics code in the WordPress header or footer. If you found this helpful, do check out our post on how to track link clicks in WordPress.

And don’t forget to follow us on Twitter and Facebook for more Google Analytics tutorials.

Want to Try MonsterInsights for Free?

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


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.