2 Ways to Add Google Tag Manager to WP For Free

Archives

|

January 2, 2025

Google Tag Manager (GTM) is a powerful tool for implementing and managing various tracking codes and scripts on your WordPress site, like analytics, conversion pixels, etc.

I specifically always use it to include the following scripts on my sites:

  • Google Analytics
  • Microsoft Clarity
  • Google ad conversion linker
  • Facebook Pixel

But there are so many more tags to use, including any custom ones you write.

Gtm Tags

Let’s walk through how to add the GTM code snippets to your WordPress site using a must-use plugin.

Method 1: Using mu-plugins (Recommended)

This method is my preferred approach as it’s simple and has no overhead.

We’ll add the GTM code directly to your site’s mu-plugins file.First, if you haven’t set up mu-plugins yet, follow the instructions in this article: Installing mu-functions Plugin

Once you have mu-plugins set up, add the following code to your mu-plugins file:

add_action('wp_head', 'gtm_head_code', 20);

function gtm_head_code() {
?>
<!-- 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','XXX');</script>
<!-- End Google Tag Manager -->
<?php
}

add_action('wp_body_open', 'gtm_body_code', 20);

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

This code does two main things:

  1. It adds the GTM script to the <head> section of your site using the wp_head action hook.
  2. It adds the GTM noscript iframe just after the <body> tag using a custom __before_header action hook.

Important: You need to replace XXX in both snippets with your actual Google Tag Manager container ID.

Why Use a Must-Use Plugin?

Using a must-use plugin ensures that the GTM code is always active on your site, regardless of theme changes or regular plugin updates.

It’s a more robust and reliable method for implementing site-wide functionality like analytics tracking.

Method 2: Using a Plugin

If you need extra functionality, like WooCommerce integration and more, you can use DuracellTomi’s Google Tag Manager for WordPress plugin.

This plugin offers advanced features like dataLayer variables, WooCommerce integration, and more.

However, it does add some overhead to your site.

Install it as you would any other WP plugin:

  1. Go to your WordPress dashboard
  2. Navigate to Plugins > Add New
  3. Search for “DuracellTomi Google Tag Manager for WordPress”
  4. Click “Install Now” and then “Activate”
  5. Go to Settings > Google Tag Manager
  6. Enter your Google Tag Manager container ID
  7. Configure any additional settings as needed

Which Method Should You Choose?

Both methods will successfully add Google Tag Manager to your WordPress site.

Choose Method 1 if you want a lightweight solution with no extra overhead, or Method 2 if you need additional features and integrations.

Other Articles You'll Like

Leave a Reply

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