Where to Put Google Analytics Code in WordPress: A Step-by-Step Guide

Where To Put Google Analytics Code In Wordpress

Adding Google Analytics to your WordPress site is essential for tracking visitor behavior, understanding your audience, and making data-driven decisions to improve your website. However, if you’re new to WordPress or Google Analytics, you might be unsure of where to put the tracking code.

In this guide, we’ll show you the best methods to add Google Analytics code to your WordPress site, ranging from manual insertion to using plugins. By the end of this article, you’ll have Google Analytics successfully tracking your website’s traffic and performance.


Step 1: Get Your Google Analytics Tracking Code

Before adding the Google Analytics code to your WordPress site, you need to get your tracking code from Google Analytics.

  1. Go to the Google Analytics website.
  2. Sign in or create an account if you don’t have one.
  3. Set up a new property for your website.
  4. Once the property is created, Google will generate a tracking code (Global Site Tag – gtag.js).
  5. Copy the tracking code provided by Google. This is the code you will need to add to your WordPress site.

Step 2: Choose the Best Method to Add Google Analytics to WordPress

There are two main methods to add Google Analytics to your WordPress site:

  1. Manually adding the code by editing your theme files.
  2. Using a plugin, which is a more user-friendly approach.

Let’s explore both methods.


Method 1: Manually Add Google Analytics Code to WordPress

If you prefer not to use a plugin, you can manually add the Google Analytics tracking code directly to your theme’s header.php file. This method requires basic knowledge of WordPress theme files and coding, but it is straightforward.

1. Edit the Theme’s header.php File

  1. Log in to your WordPress dashboard.
  2. Go to Appearance > Theme File Editor.
  3. On the right-hand side, find and click on header.php under the Theme Files section. This is the file that contains the opening <head> tag for your website.
  4. Locate the closing </head> tag in the code.

2. Insert the Google Analytics Code

  • Paste the Google Analytics tracking code that you copied from your Google Analytics account right before the closing </head> tag.

Example:

<head>
<!-- Other existing head content -->

<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=YOUR_TRACKING_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'YOUR_TRACKING_ID');
</script>
<!-- End Google Analytics -->
</head>

3. Save the Changes

Once you’ve pasted the code into the header.php file, click the Update File button to save the changes.

Pros:

  • Directly adds the tracking code to your theme.
  • No additional plugins required.

Cons:

  • Changes will be lost if you update or change your theme, so you will need to re-add the code.
  • Requires manual handling, which can be risky if you accidentally break your theme.

Method 2: Using a Plugin to Add Google Analytics

For users who prefer a safer, more user-friendly approach, using a plugin to add Google Analytics to WordPress is the easiest option. This method doesn’t require you to edit theme files, and the plugin will manage the code for you.

Here are some popular plugins that allow you to add Google Analytics:

1. MonsterInsights

MonsterInsights is one of the most popular Google Analytics plugins for WordPress. It not only allows you to add the tracking code easily but also provides analytics reports directly within your WordPress dashboard.

Steps to Use MonsterInsights:

  1. Go to your WordPress dashboard.
  2. Navigate to Plugins > Add New and search for “MonsterInsights”.
  3. Install and activate the MonsterInsights plugin.
  4. Once activated, go to Insights > Settings.
  5. Follow the setup wizard, which will guide you through connecting your Google Analytics account to your WordPress site.
  6. The plugin will automatically add the Google Analytics tracking code to your site.

Pros:

  • Simple setup with no coding required.
  • View analytics directly in your WordPress dashboard.
  • No need to manually add or update tracking code if you change themes.

Cons:

  • Can add extra overhead to your WordPress site (depending on the plugin’s size and complexity).
  • Some features may require a premium version.

2. Insert Headers and Footers

If you prefer to use a lightweight plugin that simply allows you to insert code into your theme’s header, Insert Headers and Footers is a great option.

Steps to Use Insert Headers and Footers:

  1. Go to your WordPress dashboard.
  2. Navigate to Plugins > Add New and search for “Insert Headers and Footers”.
  3. Install and activate the plugin.
  4. Once activated, go to Settings > Insert Headers and Footers.
  5. Paste your Google Analytics tracking code into the Scripts in Header section.
  6. Save the changes.

The code will now be inserted into the header of every page on your WordPress site.

Pros:

  • Lightweight plugin.
  • Easy to use without modifying theme files.
  • Can be used for other tracking codes as well, not just Google Analytics.

Cons:

  • Does not provide in-dashboard analytics like MonsterInsights.

Verifying Google Analytics on Your WordPress Site

Once you’ve added the Google Analytics code using either method, you should verify that the tracking is working correctly.

Steps to Verify:

  1. Go to your Google Analytics dashboard.
  2. Navigate to Real-Time > Overview.
  3. Open your WordPress website in a new browser tab or on a different device.
  4. If the tracking code has been properly installed, you should see real-time traffic data in the Google Analytics dashboard indicating that someone is visiting the site.

Which Method Should You Use?

  • For beginners: Using a plugin like MonsterInsights is the simplest option as it requires no coding and provides additional features like in-dashboard analytics.
  • For advanced users: If you prefer fewer plugins and are comfortable editing your theme files, adding the code manually to header.php or using Insert Headers and Footers is a lightweight solution.

Conclusion

Adding Google Analytics to your WordPress website is crucial for tracking performance and making data-driven decisions. Whether you choose to manually insert the tracking code or use a plugin, the process is straightforward and ensures you can monitor your site’s traffic and user behavior effectively.

By using Google Analytics, you gain access to valuable insights that can help you optimize your website, improve SEO, and increase engagement. Now that you know where to put the Google Analytics code in WordPress, you’re ready to start tracking your website’s performance and growing your online presence.