GTM for WordPress: Installation and Configuration Guide | Adslytics | Adslytics

Google Tag Manager Setup & Audit How-To Guide

GTM for WordPress: The Right Way to Install and Configure

By Muhammad Farooq · January 23, 2026 · 7 min read
GTM for WordPress: The Right Way to Install and Configure

Why GTM on WordPress Needs Care

WordPress's plugin ecosystem makes it easy to add Google Tag Manager — and equally easy to break it. Multiple plugins each trying to add analytics code, caching plugins that strip scripts, and theme frameworks that output code in non-standard locations can all interfere with clean GTM installation and operation.

The Best Installation Methods

Method 1: Dedicated GTM Plugin (Recommended for Most)

Use a dedicated GTM plugin that handles both the head snippet and the noscript body snippet correctly. Good options include:

  • GTM4WP (Google Tag Manager for WordPress): the most widely used, places both snippets in the correct positions, supports WooCommerce data layer, compatible with most major themes
  • DuracellTomi's Google Tag Manager for WordPress: same author as GTM4WP, well-maintained

After installing the plugin: Settings then Google Tag Manager (in WordPress admin), enter your GTM Container ID (GTM-XXXXXXX), and enable the plugin. The plugin handles snippet placement automatically.

Method 2: Manual Theme Function

For developers who prefer not to add a plugin, add the GTM snippets directly to your theme's PHP files:

In your theme's functions.php:

// GTM head snippet
add_action('wp_head', function() {
    echo "<!-- GTM --><script>(function(w,d,s,l,i){w[l]=w[l]||[];...})(window,document,'script','dataLayer','GTM-XXXXXXX');</script><!-- End GTM -->";
}, 1); // Priority 1 = very early in head

// GTM noscript body snippet
add_action('wp_body_open', function() {
    echo "<!-- GTM noscript --><noscript><iframe src='https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX' height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript><!-- End GTM noscript -->";
}, 1);

The wp_body_open hook requires your theme to support it (most modern themes do). If not, use wp_footer as a fallback — though this places the noscript snippet at the bottom of the page rather than immediately after the body tag.

Method 3: Insert Headers and Footers Plugin

A generic code injection plugin like "Insert Headers and Footers" lets you paste the GTM snippets into site-wide header and footer sections. Simple and reliable for the head snippet. The noscript body snippet may end up in the footer rather than immediately after the body tag — a minor deviation from Google's guidelines but rarely causes issues in practice.

Critical: Avoid Duplicate GTM Installations

Before installing GTM, check that no other plugin is already adding it. Common sources of duplicate GTM snippets on WordPress:

  • WooCommerce analytics plugins (MonsterInsights, Site Kit by Google, Pixel Caffeine)
  • A previous "Insert Headers and Footers" configuration
  • Theme-specific built-in analytics fields

In Chrome DevTools, load any page on your WordPress site, go to the Network tab, filter by "gtm.js", and check how many times it loads. It should load exactly once. If it loads twice, you have a duplicate.

WooCommerce Data Layer Setup

If you are running WooCommerce, use the GTM4WP plugin's built-in WooCommerce data layer feature. In the plugin settings, enable "Track WooCommerce" and select your data layer format (GA4 recommended). The plugin will automatically push ecommerce events (view_item, add_to_cart, begin_checkout, purchase) to the data layer on WooCommerce pages.

This significantly simplifies your GTM configuration for enhanced ecommerce tracking setup.

Caching Plugin Compatibility

WordPress caching plugins (WP Rocket, WP Super Cache, W3 Total Cache) can interfere with GTM in two ways:

  • Script minification/merging: aggressive JavaScript optimisation can break GTM's snippet. Exclude the GTM snippet from minification in your caching plugin settings.
  • Lazy loading of scripts: delay-loading JavaScript can defer the GTM container, causing some tags to miss early events. Exclude GTM from any delay-load or lazy-load configuration.

Verifying the Installation

  1. Install the Tag Assistant browser extension
  2. Load your WordPress homepage
  3. Check Tag Assistant — your GTM container should appear once, with a green status
  4. Confirm the noscript iframe is in the correct position by viewing source and searching for "noscript" near the top of the body

Summary

The GTM4WP plugin is the recommended approach for most WordPress sites. Manual theme installation gives more control for developers. Whichever method you choose, verify for duplicates, exclude GTM from caching optimisations, and use GTM Preview Mode to confirm tags fire correctly before going live.

See our Google Tag Manager Setup and Audit service for WordPress-specific configuration.

Need GTM installed and configured on your WordPress site? Contact Adslytics.

Need expert tracking setup?

Our Google Tag Manager experts have delivered 500+ tracking setups with a 98% success rate.

Get a Free Consultation →
← Back to Blog
Muhammad Farooq

Author

Muhammad Farooq GTM & Analytics Expert · Adslytics Founder

Tracking specialist with 10+ years of experience in Google Tag Manager, GA4, Server-Side Tracking, and Google Ads. Founder of Adslytics — a dedicated analytics agency with a 98% success rate across 232+ projects on Upwork.

Top Rated Plus LinkedIn Visit the author's profile →