• Home
  • Contact
No Result
View All Result
Blogs
  • Home
  • Contact
No Result
View All Result
Blogs
No Result
View All Result

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

admin by admin
June 5, 2023
in Marketing Automation
Share on FacebookShare on Twitter


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 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.



Source link

Related Posts

Hardware Data Transfer Speeds Every Marketer Should Understand
Marketing Automation

Hardware Data Transfer Speeds Every Marketer Should Understand

Marketing teams deal with more data than ever before. From 4K and 8K video to photography, analytics exports, and...

by admin
November 10, 2025
5 best email marketing tools for real estate businesses in 2025
Marketing Automation

5 best email marketing tools for real estate businesses in 2025

Real estate professionals face unique challenges when it comes to staying top-of-mind with clients, nurturing leads through long sales...

by admin
November 10, 2025
Marketing Automation

Top 10 Newsletter Tools to Elevate Your Email Marketing in 2026

Curious about how newsletters can improve your email marketing? Newsletters boost engagement, build strong customer relationships, and enhance your...

by admin
November 10, 2025
Build custom notification bots for new leads
Marketing Automation

Build custom notification bots for new leads

Hooray! Leads are flooding in from your website form, chatbox, and new ad campaign. Take a moment to celebrate...

by admin
November 10, 2025

POPULAR POSTS

  • Scheduling Instagram Reels: The Complete Guide

    Scheduling Instagram Reels: The Complete Guide

    0 shares
    Share 0 Tweet 0
  • How to set up Discord Server Subscriptions + Shops

    0 shares
    Share 0 Tweet 0
  • How to Use Instagram Collab Posts (+ Ideas to Boost Engagement and Reach)

    0 shares
    Share 0 Tweet 0
  • How to Add a Link to an Instagram Story (3 Steps + Examples)

    0 shares
    Share 0 Tweet 0
  • The Best Time to Post on LinkedIn in 2024 (+Heatmap Graph)

    0 shares
    Share 0 Tweet 0

© MarTechs All rights reserved.

Use of these names, logos, and brands does not imply endorsement unless specified. By using this site, you agree to the Privacy Policy

Navigate Site

  • Home
  • About
  • Services
  • Contact

Newsletter Sign Up.

Loading
No Result
View All Result
  • Home
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.