Analytics & TestingContent Marketing

How To Exclude Tags In Google Tag Manager By The Visitor’s Logged-In User Role in WordPress

Throughout any day, I log into Martech Zone to read form submissions, add and edit content, and improve the site’s performance. At issue is that I don’t want that activity skewing my analytics or executing tags within Google Tag Manager (GTM), such as opening my chatbot or detecting a visitor’s location using an IP lookup service (our chat tag fires only when the user is in the United States).

Detect Logged In on WordPress With Google Tag Manager

WordPress already has built-in functionality to display a logged-in class on your body tag that displays whether or not a user is logged in. Many people utilize this within Google Tag Manager to exclude tags from firing, such as Google Analytics tags. The process works by looking for a CSS class automatically added to your WordPress site’s body tag, recording the result in a variable, then using a trigger to execute specific tags.

Here’s how to set this variable and trigger up in GTM:

  1. Add Variable called Logged-In using a DOM Element.
    • Set the Selection Method to CSS Selector
    • Set the Element Selector to body.logged-in
    • Set the Attribute Name to class
    • Save
google tag manager variable wordpress logged in
  1. Add a Trigger that utilizes the Logged-In Variable. In this case, you only want the trigger to fire when the user is NOT logged in, so you can do this by creating a Not Logged In Trigger.
    • Set the Trigger Type to DOM Ready
    • Set the trigger fire on Some DOM Ready Events
    • Select the variable Logged-In and equals null
google tag manager trigger wordpress not logged in

Alternatively, you could set the variable to not equals logged-in and you could use the trigger as an exclude rather than an include. Either way, utilize this trigger to execute, typically instead of All Pages.

There’s a limitation with this, though. What if you’re running a WordPress site where you want to exclude a specific role from firing a tag? In my case, I have registered contributors that I still want to fire our location and chat triggers. Using the logged-in methodology will eliminate the tags from firing on any registered and logged-in user. Unfortunately, WordPress doesn’t automatically display any role within the HTML of the page for you to trigger a tag on. But you can add it, though!

Add A Body Class With The Visitor’s Role On WordPress

Just as WordPress automatically injects a class of logged-in on the body class, you can add the user’s role within a body class. Within your child theme functions.php file, you can add the following function:

function add_role_to_body_class( $classes ) {
    if ( is_user_logged_in() ) {
        $current_user = wp_get_current_user();
        $user_role = array_shift($current_user->roles);
        $classes[] = 'role-' . $user_role;
    } else {
        $classes[] = 'role-guest';
    }
    return $classes;
}
add_filter( 'body_class', 'add_role_to_body_class' );

So, if you’re an administrator, this will add <body class="role-administrator" along with any other classes like logged-in, etc. And if you’re not logged in, the function adds an imaginary class of role-guest.

Detect WordPress Visitor’s Role on WordPress With Google Tag Manager

In my case, I want to exclude some tags from being fired when an administrator is logged into the site. Just as in the example above with logged-in works for a variable and trigger; now I can do this with role-administrator. Rather than specifying the class in the element selector, I’m just going to pass the entire class contents.

  1. Add Variable called Body Class using a DOM Element.
    • Set the Selection Method to CSS Selector
    • Set the Element Selector to body
    • Set the Attribute Name to class
    • Save
Capture Body Class in Google Tag Manager Variable
  1. Add a Trigger that utilizes the Body Class variable. In this case, you only want the trigger to fire when the user is NOT logged in, so you can do this by creating a Is Administrator trigger.
    • Set the Trigger Type to DOM Ready
    • Set the trigger fire on Some DOM Ready Events
    • Select the variable Body Class contains role-administrator
google tag manager trigger wordpress administrator
  1. On your Tag Triggering, add the trigger Is Administrator to your Exceptions. This will ensure the tag is never fired when an administrator is logged in while viewing your site. In the case of my site, I have a tag that detects whether someone is on the core domain (rather than a translation) and that the tag is NOT fired when the administrator is logged in.
google tag manager tag trigger exceptions

Update: I updated this logic after publishing it and emailing it. Capturing the entire body class was a much more efficient variable so that I could use it to specify roles or whether the person was logged in from a single variable.

Appreciate this content?

Sign up for our weekly newsletter, which delivers our latest posts every Monday morning.

We don’t spam! Read our privacy policy for more info.

Douglas Karr

Douglas Karr is CMO of OpenINSIGHTS and the founder of the Martech Zone. Douglas has helped dozens of successful MarTech startups, has assisted in the due diligence of over $5 bil in Martech acquisitions and investments, and continues to assist companies in implementing and automating their sales and marketing strategies. Douglas is an internationally recognized digital transformation and MarTech expert and speaker. Douglas is also a published author of a Dummie's guide and a business leadership book.
Back to top button
Close

Adblock Detected

Martech Zone is able to provide you this content at no cost because we monetize our site through ad revenue, affiliate links, and sponsorships. We would appreciate if you would remove your ad blocker as you view our site.