How To Add Google Tag Manager To WordPress

Learn how to add Google Tag Manager to WordPress using a code snippet plugin or a child theme to track your website traffic safely.

Eptekhar Hasan9 min read
Illustration for How To Add Google Tag Manager To WordPress

To learn how to add Google Tag Manager to WordPress, you need to place two pieces of code on your website. You put the first snippet inside your site header and the second snippet immediately after the opening body tag. You can do this using a code snippet plugin or by editing your child theme files.

Before you decide how to add Google Tag Manager to WordPress, you must understand what this platform actually does. Google Tag Manager is a container that holds your marketing scripts. It is not a reporting tool. You set up the container once. After that, you manage all your tracking pixels from one dashboard without touching your website code again.

Get Your Container Code

You cannot install anything until you have an active workspace. Go to the official Google Tag Manager website and sign in with your Google account. Click the button to create a new account.

Google asks you for an account name. Use your company name. Next, you must set up your container. A container represents your specific website. Type your domain name into the container setup box. Select the Web platform option. Click create and accept the terms of service.

The platform will immediately show you a popup window containing two blocks of code. Keep this window open. You need to copy these exact scripts in the next steps. The top script goes into the head section of your website. The bottom script goes into the body section.

Check Your Website For Existing Tags

You must ensure your website does not already have a container installed. Adding the same container twice will duplicate your tracking events. This ruins your analytics data.

You might have an old plugin running a container from a previous developer. You can run your domain through a Google Tag Manager checker to see if a container is already firing and which specific tags are active on your page. If you see an active container ID that matches yours, you can stop right here. Your setup is already complete. If the scan shows no tags, you can proceed with the installation.

Understand The Two Code Snippets

Google gives you two different snippets for a specific reason. You must install both of them in the correct locations.

The first snippet uses JavaScript. It does the heavy lifting for your tracking. You place this code as high in the head section of your website as possible. Loading it early ensures your tracking fires the moment a visitor opens the page.

The second snippet uses an HTML noscript tag. This acts as a backup system. Some privacy browsers block JavaScript. Some users disable JavaScript manually. When JavaScript fails, the noscript tag activates. It places a tiny invisible image on the screen to track the basic page visit. You place this code immediately after the opening body tag in your website structure.

Install The Tracking Code Using A Plugin

Using a plugin is the safest method for most website owners. It keeps your tracking code separate from your theme files. If you change your website design next year, your tracking stays intact. Many professionals install and set up Google Tag Manager in WordPress using a plugin because it prevents accidental syntax errors.

You can use a dedicated snippet manager to handle this. We recommend a free plugin called WPCode. Follow these exact steps:

  1. Log into your WordPress admin dashboard.
  2. Click on Plugins in the left menu and select Add New.
  3. Type WPCode into the search bar.
  4. Click install and then click activate.
  5. Hover over the new Code Snippets menu item and click Header and Footer.

You will see three empty boxes on this screen. Go back to your Google Tag Manager window. Copy the first block of code. Paste it into the box labelled Header.

Go back to Google Tag Manager and copy the second block of code. Paste it into the box labelled Body. Click the save changes button at the top right of the screen. Your container is now active on every page of your website.

Add The Code Manually To Your Theme

You might prefer to keep your plugin count low. You can add the tracking code directly to your WordPress theme files.

You must never edit your parent theme files directly. Quality WordPress themes receive regular updates to address security and industry changes. When you update the theme, WordPress overwrites all the files. You will lose your tracking code instantly. You must create a child theme to make these edits safely.

Activate your child theme. Go to Appearance in your WordPress dashboard. Click on Theme File Editor. Look at the list of files on the right side of the screen. Click on the file named header.php.

Find the opening head tag. It looks like this: <head>. Paste your first Google Tag Manager snippet immediately below this tag.

Scroll down the file until you find the opening body tag. It looks like this: <body>. Paste your second snippet immediately after this tag.

Click the update file button at the bottom of the screen. If you cannot see the Theme File Editor in your menu, your hosting provider has disabled file editing for security reasons. You will need to use the plugin method or connect to your server via FTP to edit the file.

Clear Your WordPress Cache

Your tracking code will not appear on your live website immediately if you use a caching plugin. Caching tools take a snapshot of your website and serve that static version to visitors. This speeds up your page load times.

Your caching plugin is still serving the old snapshot of your website without the tracking code. You must clear the cache to force the system to take a new snapshot.

Look at the top bar of your WordPress dashboard. You will usually see a button for your caching tool. This might be WP Rocket, LiteSpeed Cache, or a tool provided by your hosting company. Click the option to purge all cache.

If you use a content delivery network like Cloudflare, you must clear the cache there as well. Log into your Cloudflare account, navigate to caching, and click purge everything. You can read more about managing your website infrastructure in our guide to routine website maintenance.

Test Your Container In Preview Mode

You must verify that your container loads correctly before you add any marketing pixels. Google provides a built in testing tool for this exact purpose.

Go back to your Google Tag Manager dashboard. Click the preview button in the top right corner. A new tab will open for Google Tag Assistant.

Type your full website address into the box. Click connect. Tag Assistant will open your website in a new window. Look at the bottom right corner of your website. You should see a small box saying Tag Assistant is connected.

Go back to the Tag Assistant tab. The screen will say connected successfully. You will see a timeline of events on the left side of the screen. Click on the first event called Initialization. The main screen will show you which tags fired on the page. Right now, this area will be empty because you have not created any tags yet. The important thing is that the container itself has loaded.

If Tag Assistant cannot connect, you have a problem with your installation. Check your caching plugins again. Ensure you copied the entire code snippet without missing any brackets.

Publish Your Workspace

Google Tag Manager uses a version control system. When you make changes in your dashboard, those changes do not go live automatically. They sit in a draft state called a workspace.

You must publish your workspace to make your container active. Click the submit button in the top right corner of your dashboard.

Google will ask you for a version name and a version description. Always fill these out. Write a clear note explaining what you changed. For example, write "Initial container installation". This creates a permanent log of your work. If you ever break your tracking setup in the future, you can look at this log and restore a previous working version.

Click the publish button. Your container is now live and ready to accept your marketing tags.

Configure The Data Layer For Ecommerce

The standard installation methods above are perfect for lead generation websites. They allow you to track page views, button clicks, and form submissions.

Ecommerce websites require a more complex setup. If you run a WooCommerce store, you need to track product views, add to cart actions, and final purchase values. The standard Google Tag Manager snippet cannot see this transaction data by itself.

You must activate a data layer. A data layer is a hidden JavaScript object that collects transaction details from your WordPress database and hands them to Google Tag Manager.

You cannot create a data layer by pasting a simple code snippet. You need a dedicated plugin that understands the WooCommerce architecture. The most reliable tool for this is GTM4WP. This plugin automatically builds a comprehensive data layer for your store.

Install GTM4WP from the WordPress repository. Enter your container ID in the plugin settings. Navigate to the integration tab and select WooCommerce. Tick the box to track enhanced ecommerce.

This setup pushes all your sales data into the browser. However, browser tracking is becoming less reliable due to ad blockers and privacy updates. To capture every single purchase accurately, you will eventually need to rebuild this setup using server side tracking. Server side setups move the tracking logic away from the visitor browser and process the data securely on your own cloud server.

Manage User Permissions Safely

Google Tag Manager gives you the power to inject any code into your website. This is a massive security risk if you hand out access to the wrong people.

A malicious user can use Google Tag Manager to insert spam links, redirect your traffic, or steal customer data. You must control who can edit your container.

Go to the admin tab in your Google Tag Manager dashboard. Click on user management. You can invite team members or marketing agencies using their Google email addresses.

Never give anyone publish access unless they are a senior technical lead. Give your marketing team edit access instead. This allows them to create new tags and test them in preview mode. They cannot push those tags to the live website. They must ask you to review their work and click the publish button. You can read more about securing your site in our guide to WordPress security audits.

Review your user list every three months. Remove access for any contractors or agencies you no longer work with. Keep your container secure to protect your website performance and your customer data.

Protect Your Core Web Vitals

Adding Google Tag Manager to WordPress will impact your page speed. Every marketing pixel you add to your container forces the visitor browser to download more scripts.

Google measures your website speed using Core Web Vitals. If your site loads too slowly, your search rankings will drop. You must be ruthless about which tags you allow in your container.

Common tags that slow down your website include:

  • Facebook and Meta advertising pixels
  • Session recording tools like Hotjar
  • Live chat widgets and support bots
  • Third party font libraries

Do not install tools you do not use. If you are not running Microsoft Ads, do not install the Bing tracking pixel. If you stop using Hotjar for heatmaps, pause the tag in your workspace and publish the change.

You can also use the consent mode feature in Google Tag Manager to delay non-essential scripts. This stops marketing pixels from loading until the user accepts your cookie banner. This improves your initial page load speed and keeps you compliant with privacy laws.

Keep reading

All guides