
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.

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

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.

Google then displays your installation code in two parts.

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

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:



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.

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.

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

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

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.

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.

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.

Click the Submit button on the top right of the main screen to publish your tag.
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.

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.

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.

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.

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.
<head> for the first GTM code.<body> – Start location.How do I know if GTM is installed?
You can verify your GTM installation by checking your website’s page source code.
<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.





