How to Add Google Tag Manager to WordPress

Learn how to add Google Tag Manager to WordPress. In this tutorial, we’ll show you a clean, easy way to add Google Tag Manager using WPCodeBox.

Meet WPCodeBox: The Best Code Snippets Plugin for WordPress
faces
Join thousands of developers and agencies who are working better and faster using WPCodeBox

Your WordPress site needs Google Tag Manager to track user behavior and marketing performance. However, setting one up isn’t as straightforward as changing a toggle. 

Manual file edits get wiped out during updates, while plugin-based solutions add unnecessary bloat to your site.

In this article, I’ll share a simple way to add Google Tag Manager to WordPress. This method works without weighing down your site or creating maintenance issues later.

What Is Google Tag Manager?

Google Tag Manager (GTM) is a free tool that lets you manage all your website tracking codes from one dashboard. You add GTM once, then manage analytics, Facebook pixels, and other tags without ever editing your theme files again.

Tracking codes, also called tags or pixels, come from tools like Google Analytics and Facebook Ads. These help you understand how visitors find your site, what they do while browsing, and whether they take actions like signing up or purchasing.

Before GTM, adding or updating these tags meant manually editing your website code every time. With GTM, you add two code snippets during setup, then manage everything from your GTM dashboard.

How to Add Google Tag Manager to WordPress Using WPCodeBox

Before adding Google Tag Manager to WordPress, you need to set up your GTM account and get your tracking codes. This process takes just a few minutes and gives you everything you need for proper setup.

You can start by visiting  tagmanager.google.com and signing in with your Google account. Click the Create account button and then enter your account name, choose your country, and accept if you want to share the data anonymously.

create new GTM account

Next, create a container for your WordPress site. Enter your container name (typically your website name), select Web as the target platform, and click Create. Google will display your container ID immediately in the format GTM-XXXXXX.

add new GTM account and fill in the container details

Google then displays your installation code in two parts.

Google Tag manager two setup code

The first code block is labeled “Paste this code as high in the <head> of the page as possible,” and it looks like a JavaScript snippet that starts with:

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXXX');</script>
<!-- End Google Tag Manager -->

The second code block is labeled “Paste this code immediately after the opening <body> tag,” and this code looks like:

<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

Now that you have both codes and the container ID, you’re now ready to add Google Tag Manager to WordPress using WPCodeBox.

Using WPCodeBox to Add Your GTM Snippet

While you can directly add Google Tag Manager to your website’s core files, there’s a safer solution that protects your site from crashing in case of an error. That solution is WPCodeBox.

It is the best WordPress code snippets plugin for managing your site’s custom code without editing theme files. Unlike traditional methods that add unnecessary plugin bloat, WPCodeBox lets you add Google Tag Manager safely through its snippet library. 

wpcodebox snippet plugin

It handles the technical complexity, so you don’t need to worry about PHP errors or site crashes. The plugin automatically detects most errors and disables problematic snippets to protect your site. Its powerful code editor even auto-suggests WordPress actions, hooks, and functions based on context. You get all the benefits of custom code implementation without the risks of direct file editing or the overhead of heavy plugins.

Let’s now look at the steps to add the code using WPCodeBox:

  1. Install and activate WPCodeBox on your WordPress site through the plugins menu.
  2. Navigate to WPCodeBox 2 from your WordPress dashboard. This will automatically open the new snippet screen. If you don’t see the editor, click the “Add New” button to create a new snippet.
in the WPCodeBox editor click the Repository option
  1. Inside the editor, click Repository to open the snippet library.
  2. Now, search for “Google Tag Manager” to find the ready-to-use GTM snippet and click on it.
  3. Click the Download snippet button to add the code to your editor.
download WPCodeBox snippet
  1. Replace <!– First Part of Google Tag Manager Code goes here (header) –> with the first code provided from the GTM website. Then replace <!– Second Part of Google Tag Manager Code goes here (body) –> with the second code provided from the GTM website.
paste google tag manager code in WPCodeBox editor
  1. The snippet settings apply automatically. However, you can manually configure them in the snippet panel if needed:
    • Type: PHP Snippet
    • How to run the snippet: Always (On Page Load)
    • Where to insert the snippet: Plugins Loaded (Default)
  2. Click the Save button above the editor. WPCodeBox saves your snippet in a disabled state to protect your site from potential syntax errors.
  3. Review your code and toggle the snippet to Enabled status to activate the restriction immediately.

You can verify Google Tag Manager is working by viewing your site’s page source and searching for your container ID. The GTM script should appear in your head section, and the noscript frame should appear right after your body tag. Your GTM implementation stays in place even when you update your theme or WordPress core.

Google Tag manager on the frontend

How to Create Your First Tag in Google Tag Manager

Now that Google Tag Manager is installed on your WordPress site, you can start adding tracking tags. Adding your first tag lets you test that GTM is working properly while setting up valuable tracking for your website.

Google Analytics 4 is the perfect first tag to create. It gives you insights into visitor behavior and is essential for most websites.

Start by opening your Google Tag Manager dashboard and clicking Tags in the left sidebar. Click the blue New button to create a new tag.

add new tag in GTM

Enter a name for your tag at the top, such as “Google Analytics 4 Configuration.”

name your GTM tag

Click on “Tag Configuration” and look for Google Analytics in the tag options.

choose your tag type in Google Tag manager

Enter your GA4 Measurement ID in the required field and enter the Event name. You can find this ID in your Google Analytics account under Admin > Data Streams.

input your measurement ID and add an event name in GTM

After you input your GA4 Measurement ID, you’ll see a notice appears that says “No Google tag found in this container.” You need to create a Google tag to make this work. Click the “Create tag” suggestion and follow the prompts to generate one. A green checkmark confirms everything is configured properly.

Next, scroll down and click Triggering. Select Initialization – All Pages so your tag fires on every page of your site.

choose trigger for your tag in GTM

Click Save in the top right corner. Your tag is now ready, but it won’t work on your WordPress site until you publish it.

Save and then Publish the tag in GTM

Click the Submit button on the top right of the main screen to publish your tag.

Verify Your GTM Installation

Now that you’ve created your first tag, let’s verify that Google Tag Manager is working correctly on your WordPress site. Testing confirms that both GTM and your tag are firing properly.

Go back to your Google Tag Manager dashboard and click the blue Preview button at the top right. Enter your WordPress site’s URL in the field and click Connect.

verify your google tag manager using tag assistant

A new tab will open showing your website with the Tag Assistant debug panel at the bottom. Browse through a few pages on your site and watch the panel.

tag assistant debug panel at the bottom

Switch back to the Tag Assistant page. The status should show as Connected with your GA4 tag and Google tag in the Tags Fired list. If you see any warnings or errors, check your tag configuration and try again.

Google tag manager tag assistant connected

Once you confirm everything is working, return to your GTM dashboard. Click the blue Submit button at the top right. Add a version name and description, then click Publish. Your tag is now live and collecting data on your WordPress site.

add a version name and publish your Tag in GTM

More on Adding Google Tag Manager to WordPress

How do I add Google Tag Manager to WooCommerce?

You add Google Tag Manager to WooCommerce through your WordPress site since both work together. You can use WPCodeBox to place your GTM code in the header and body sections and the same GTM installation will automatically capture WooCommerce events like cart additions and purchases without any special setup.

How to add Google Tag Manager code in WordPress Elementor?

Elementor Pro offers a built-in feature for adding custom code without plugins.

  1. Go to Elementor > Custom Code from your WordPress dashboard.
  2. Click “Add New Custom Code.”
  3. Enter a title to identify your GTM code later.
  4. Set the location to <head> for the first GTM code.
  5. Paste your GTM head script in the code editor.
  6. Click “Publish” and repeat steps 2-5 for the body code with <body> – Start location.
  7. Set conditions to apply the codes across your entire site, then click “Publish” again.

How do I know if GTM is installed?

You can verify your GTM installation by checking your website’s page source code.

  1. Right-click anywhere on your WordPress website.
  2. Select “View Page Source” from the menu.
  3. Press Ctrl+F or Command+F to open the search bar.
  4. Type your GTM container ID (GTM-XXXXXX) in the search field.
  5. Check if you see the script in the <head> section and the noscript frame after the <body> tag.

How do I add a tracking code to a WordPress website?

WPCodeBox is the safest way to add tracking codes without editing theme files.

  1. Install and activate WPCodeBox on your WordPress site.
  2. Go to WPCodeBox 2 and click the New Snippet button to create a new snippet.
  3. Enter your tracking code in the code editor.
  4. Set the snippet type to match your code format.
  5. Configure where the code should load on your site.
  6. Click Save and toggle the snippet to Enabled status.

More Ways to Customize Your WordPress Website

Related Tutorials

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.