• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero Blog

NameHero Blog

Web Hosting Tips & Resources From NameHero

  • Hosting
    • Web Hosting
    • VPS Hosting
    • WordPress Hosting
    • WooCommerce Hosting
  • Reseller
  • Enterprise
  • Domains
  • Account
  • Blog Home
  • Categories

Only Load Google Tag Manager Code On Specific Pages

Bhagwad Park

Published on: March 16, 2020

Categories: WordPress 0

When WordPress 5.2 released on 7th May 2019, it included the new “wp_body_open” action, which allowed to insert arbitrary code into the body section of WordPress. One of the best uses of this new functionality was inserting Google Tag Manager (GTM) code, because Google recommends placing it in the body and not the header or footer, unlike other scripts. In this article, I’m going to show you how to use Google Tag Manager to only insert code on specific pages instead of on the entire site.

Why Not Just Add the Code in the Gutenberg Editor?

Last week, I had praised the WordPress Gutenberg editor, and said that it was much more efficient to just inline CSS or JavaScript on specific pages, instead of adding them to a theme or the functions.php file. So why not use the same approach all the time? Why should we use GTM to inject code into certain parts of the site?

The reason is that GTM allows us to check a bunch of variables to decide whether or not we want to insert code. As an example, I want to run my Google Analytics code only on when I’m not logged into my site. This way, my analytics software doesn’t count my personal site visits. Similarly, it can check for a host of other things, which are just impossible to do from Gutenberg. Hence the need for GTM.

Using Triggers to Check for Specific Pages

Let’s say you have a certain tag that you want to run on one or two pages only. I personally have this requirement all the time for certain tracking codes. I occasionally need to get more information about how customers interact with a particular page, but don’t want to add the code everywhere. At most, one or two pages on which I’m running the experiment.

So you create your tag with the code you want in GTM in exactly the same way as before. Now at the bottom, you create a new Trigger like this:

Create a new GTM Trigger
Create a New Trigger in Google Tag Manager

Give the trigger a name, specify the Trigger Type as a “Page View”, then choose for the trigger to fire on “Some Page Views” as demonstrated in the screenshot. If this is a regular piece of code like Google Analytics, for example, you’d simply choose the other option. In fact, for the overwhelming number of tags, you’ll choose for it to trigger on all page loads. But not here.

Now we configure the event. Choose “Page URL” in the first box, and “Matches RegEx” in the second one as shown here:

Using the Page URL Slug to narrow down the target pages
Specify the Pages with the Page URL Slug

In the final field, enter the “page slug” of the page on which you want to run the code. If you have more than one page, you can separate the two with an OR operator (|) as shown above. You can repeat this as many times as you want.

There are ways to get more sophisticated if the number of pages increases, but this is enough to get you started. Now save your changes, and preview them using the normal GTM preview functionality. Make sure you don’t have anything blocking scripts from running. Then publish your changes, given it a name, and you’re done!

Bottom Line

GTM is a great way to selectively add certain scripts to your website without needing to change anything on your site itself. Sure, there are plenty of other ways to achieve this, but I find GTM to be the most flexible because of the ability to create sophisticated Triggers and check for a lot of variables. This allows for extremely precise targeting, like verifying that a user isn’t logged in before the tag fires!

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!

Most Popular Posts

Speed up your site: solving the WordPress slowdown

NameHero’s Recommended WordPress Plugin and Theme Setup (2023)

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • Website Performance
  • Web Hosting
  • Resellers
  • Website Development
  • Website Security
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • Domains
Help & Support
  • NameHero Blog
  • Knowledgebase
  • Announcements
  • Affiliates
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2023 NameHero, LLC. All rights reserved.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA