Analytics & Reporting

Google Tag Manager for WordPress: Setup Guide

How to install Google Tag Manager on WordPress with a plugin or theme code, connect Universal Analytics, test with Preview mode and avoid duplicate tracking.

Quick answer: To set up Google Tag Manager on WordPress, create a GTM account and container, then add the two container snippets to your site: one in the <head> and one straight after the opening <body> tag. The easiest way is a plugin such as GTM4WP (Google Tag Manager for WordPress) or Google’s Site Kit. Developers can add the code through a child theme instead. Once installed, add your Universal Analytics tag inside GTM, test it with Preview mode, publish the container and remove any old tracking code so nothing is counted twice.

Why use Tag Manager on a WordPress site

Without Tag Manager, every tracking script, from Google Analytics to the Facebook pixel to an ads conversion tag, is pasted into the theme or added through its own plugin. Over time that turns into a mess of code nobody fully understands.

Google Tag Manager puts all those tags in one place:

  • Add, change or remove tags without editing theme files
  • Fire tags only where needed, such as a conversion tag on the thank you page
  • Track clicks, form submissions, downloads and scroll depth without custom code on every page
  • Test changes safely before publishing
  • Keep a version history so you can roll back

For a business site with more than a couple of tags, GTM saves time and reduces mistakes. Our Google Tag Manager best practices guide covers how to keep a container tidy once it grows.

Step 1: Create your account and container

  1. Go to tagmanager.google.com and sign in with the Google account your business uses.
  2. Create an account, usually named after your company.
  3. Create a container named after your website, and choose Web as the target platform.
  4. Accept the terms. GTM shows you two code snippets.

Keep this window open, or find the snippets later under Admin, then Install Google Tag Manager. Your container ID looks like GTM-XXXXXXX.

Step 2: Add GTM to WordPress

There are three common methods. Pick one only.

Method A: GTM4WP plugin

GTM4WP, listed in the plugin directory as “Google Tag Manager for WordPress”, is a popular free plugin built specifically for this.

  1. Install and activate the plugin.
  2. Go to Settings, then Google Tag Manager.
  3. Paste your container ID.
  4. Choose the container code placement. “Codeless injection” works with most themes. If your theme supports the wp_body_open hook, the body snippet can be placed correctly after the opening body tag.
  5. Save.

GTM4WP can also push useful WordPress information into the data layer, such as post type, categories, author and logged-in status, and it has WooCommerce options for ecommerce tracking. That data makes it easier to build triggers and variables later.

Method B: Site Kit by Google

Site Kit is Google’s official WordPress plugin. It connects Search Console, Analytics, PageSpeed Insights and Tag Manager to your dashboard. During setup you can connect Tag Manager, and Site Kit will place the container on your site.

This suits owners who want Google’s own tool and a dashboard view of their data. Be careful not to let Site Kit place Analytics code directly as well as through GTM. More on that below.

Method C: Child theme code

Developers often prefer adding the snippets in code. Use a child theme so a theme update does not wipe your changes. In the child theme’s functions.php:

add_action( 'wp_head', function () { ?>
<!-- Google Tag Manager head snippet here -->
<?php }, 1 );

add_action( 'wp_body_open', function () { ?>
<!-- Google Tag Manager noscript snippet here -->
<?php } );

The wp_body_open hook was added in WordPress 5.2. Many current themes call it, but older themes may not. If yours does not, the body snippet will not appear, and you would need to add it to the theme’s header.php in the child theme. The head snippet does most of the work, so a missing noscript part mainly affects visitors with JavaScript disabled.

A general “insert headers and footers” plugin can also add the head snippet, but GTM4WP handles both parts more neatly.

Step 3: Add Universal Analytics through GTM

  1. In GTM, go to Variables, then create a new user-defined variable of type Google Analytics Settings. Enter your tracking ID, which looks like UA-XXXXXXX-X.
  2. Go to Tags, then New. Choose Google Analytics: Universal Analytics.
  3. Set Track Type to Page View and select your settings variable.
  4. Set the trigger to All Pages.
  5. Save.

If you have not set up Analytics yet, our guide to Google Analytics setup covers the property and view settings first.

Step 4: Remove old tracking code

This step is often missed. If your site already sends pageviews through a theme option, an analytics plugin or code in header.php, and you add the same property in GTM, every pageview is counted twice. Signs include a bounce rate close to zero and doubled pageviews.

Check for:

  • Analytics fields in your theme settings
  • Analytics plugins that insert their own tracking code
  • Site Kit inserting Analytics directly
  • Hard-coded snippets in theme files

Keep one source of truth: GTM.

Step 5: Test with Preview mode

  1. In GTM, click Preview.
  2. Open your website in the same browser. A debug panel appears at the bottom of the page.
  3. Check that your Universal Analytics tag shows under “Tags Fired” on page load.
  4. Open the Real-Time report in Google Analytics and confirm your visit appears.
  5. Click around a few pages and check the tag fires each time.

The Tag Assistant extension for Chrome can also show which Google tags are on a page and flag duplicates.

If you use a caching plugin, clear the cache after installing GTM so visitors receive the new code.

Step 6: Publish

When everything checks out, click Submit in GTM, add a version name and description such as “Universal Analytics pageview”, and publish. The container is now live.

What to track next

With the basics in place, GTM makes common tracking much easier:

  • Form submissions: track contact and quote forms as events and goals. See our guide to form tracking.
  • Clicks on phone and email links: use click triggers on tel: and mailto: links.
  • File downloads: trigger on links ending in .pdf and other file types.
  • Outbound links: see which external sites visitors leave for.
  • Scroll depth: see how far visitors read long pages.
  • Ads and social pixels: add Google Ads conversion tags and the Facebook pixel with triggers on the right pages.

Our guide to event tracking explains how to name events so reports stay readable.

If you serve visitors in regions with consent rules, decide which tags can fire before consent. Many consent plugins work with GTM by setting a variable or pushing an event to the data layer, which you can use in triggers. Our cookie consent guide covers the options.

FAQ

Will Google Tag Manager slow down my WordPress site?

The container script itself is small and loads asynchronously. The tags you add inside it are what affect speed, so keep only the ones you use.

Do I still need an analytics plugin?

Not for tracking. Some people keep a plugin for a dashboard view, but it must not insert its own tracking code.

Can I use GTM with any theme?

Yes. Plugin methods work with almost any theme. The body snippet placement depends on whether the theme supports wp_body_open.

Tracking you can trust

A clean GTM setup gives you accurate data and makes future tracking changes quick. If you want analytics that show what is actually working on your site, see how we help businesses know what’s working.

About the author

Imran M

Leave a Reply

Your email address will not be published. Required fields are marked *

Ready to improve your website?

Let's build a faster, higher performing website.