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

NameHero Blog

Web Hosting Tips & Resources From NameHero

  • Web Hosting
  • Reseller Hosting
  • Managed Cloud
  • Domains
  • Account
  • Blog Home
  • Categories

How To Test If A User Is Logged Into WordPress In Google Tag Manager

Bhagwad Park

Published on: March 20, 2020

Categories: WordPress 0

I’d written earlier about how to load the Google Tag Manager only on specific pages, and I’d mentioned there that we could ensure that certain tags would only fire when the user was or wasn’t logged into the CMS. A great use-case scenario for this is when we don’t want our Google Analytics code to load when we ourselves are visiting the page. Even though there are ways to exclude our visits, like with the IP address for example, these aren’t always reliable. But by detecting the login status of a visitor, a lot of sites can get extremely accurate results.

How HTML Code Changes for Logged in Users

The WordPress HTML code is different for anonymous and logged in users. The differences run pretty deep, even if it’s not easily visible on the outside. Most websites don’t show any external differences between the two versions, apart from the “admin” bar that’s often present on the top – and even with that, some might choose to disable it.

But when you dive into the code, you can see the real difference. For example, here’s the HTML code of my website WP-Tweaks when I examine the source after logging in:

WordPress Logged in HTML Code
WordPress Logged in HTML Code

You can see that hidden amongst the plethora of classes attached to the “body” tag, we have one called “logged-in”. The presence of this code means that we are currently logged into WordPress. The good news is that we can determine the presence or absence of this via JavaScript, and then determine whether or not we want to load a particular Google tag.

Creating the GTM Variable

To get started, we need to use GTM to create a variable that reflects whether or not we’re logged in. We do this by checking for the presence of the above code. Here’s how to do it.

When you log into GTM, click the “Variable” section on the left-hand side like this:

Variables in Google Tag Manager
Variables in Google Tag Manager

On the right-hand side pane, you’ll see a number of pre-defined variables. What we want however, is a new user-defined variable. For this, scroll down and click “New”. This will bring up the variable configuration screen. You need to enter the following details:

Configure the Variable
Configure the Variable

Choose to create a new variable based on the DOM Element as shown above. Then choose “CSS Selector” and for the actual element, type in “body.logged-in” and input “class” as the “Attribute name” field. Once all this is done, save the variable and you’re done.

Using the Variable in a Trigger

Creating a variable by itself doesn’t matter much, unless you use it in a trigger. Specifically, if the variable turns out to be “null”, it means that GTM didn’t find the HTML code and the user is anonymous. If it’s anything else, it means that the user has logged in.

In the Trigger configuration screen below, you can see that I’ve used the new variable to check if a user is logged in or not:

Creating the Trigger Based on the GTM Variable
Creating the Trigger Based on the GTM Variable

Once you’ve created this trigger, you can apply it to a specific tag in GTM to isolate the conditions under which you want the tag to run. Using this, I’ve personally created a lot of combinations of triggers that determine precisely when my code will run. It’s very convenient!

Bottom Line

Google Tag Manager allows you to create complex rules that determine when a piece of JavaScript will run on a page. Using the variable functionality, you can easily create a trigger that fires only when a user hasn’t logged into WordPress. I hope you find it as useful as me!

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

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

How To Inline And Defer CSS On WordPress Without Plugins

Top Categories

  • WordPress
  • Website Performance
  • Web Hosting
  • Resellers
  • Website Security
  • VPS Hosting
  • Website Development
  • 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
Name Hero
  • Web Hosting
  • Reseller Hosting
  • Managed Cloud
  • 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