Stop GA4 from Tracking Logged In Users in WordPress With GTM

Summary: This is a quick tutorial on how to stop GA4 from tracking the traffic from users that are logged into WordPress using GTM. There is no coding or extra plugins needed. Everything is done directly in Google Tag Manager and it takes 5 minutes.

A quick tutorial to stop Google Analytics (GA4) from tracking the traffic from users that are logged into WordPress (commonly internal traffic).

Why do we care? Google Analytics 4 (GA4) makes it very easy to exclude internal traffic (for those with static IP addresses). In my case, I have a dynamic IP address, so it’s more difficult to exclude my own traffic from GA4.

I find myself doing most development work, building, editing, or exploring client sites while I’m logged in. I don’t want my internal traffic mucking up GA4 insights. This is a really good workaround for folks with dynamic IP addresses to exclude their internal traffic from GA4, especially if they’re frequently logged into their WP admin.

So, if we can tell GA4 that we’re logged-in, we can exclude that traffic.

This is all done in Google Tag Manager. Why? Because it’s common practice to use GTM to drop in analytics and other tracking codes into websites.

Requirements:

  1. Google Analytics 4 (GA4) property
  2. Google Tag Manager

Both of these tools are regular tools in a web marketer’s toolbox, so I’m not going to explain how to set them up.

First, You Need To Check That logged-in Exists

  1. Open your site and view Page Source.
  2. CTRL+F and search for logged-in

Whenever you visit your site as a logged-in user, WordPress naturally inserts a class attribute into the page’s body called “logged-in.” This class doesn’t exist if a visitor isn’t logged in. The odds of it not being there, or if someone has modified it, are very low. Even themes won’t mess with default classes supplied by WordPress.

If you don’t see it or it doesn’t exist; unfortunately, you’ll need to find a different workaround.

You should see something like this in the first part of the body class:

A quick tutorial to stop Google Analytics (GA4) from tracking the traffic from users that are logged into WordPress (commonly internal traffic).

Step 1: Create A New Variable

Open your Google Tag Manager or your clients’ property. Make sure you’re in the property that you intend to edit.

  1. Go to the Variables section in the left menu.
  2. See the “User-Defined Variables” section at the bottom
  3. In the upper right of that section, click the “New” button.
A quick tutorial to stop Google Analytics (GA4) from tracking the traffic from users that are logged into WordPress (commonly internal traffic).

Use these settings to create a new User-Defined Variable:

  1. Name / Untitled Variable: WordPress Logged In
  2. Variable Configuration: DOM Element
  3. Selection Method: CSS Selector
  4. Element Selector: body.logged-in
  5. Attribute Name: class
  6. Hit “Save” in the upper right
A quick tutorial to stop Google Analytics (GA4) from tracking the traffic from users that are logged into WordPress (commonly internal traffic).

Step 2: Create A New Trigger

Next, in Google Tag Manager, go to the Triggers section in the left menu. Click “New.”

A quick tutorial to stop Google Analytics (GA4) from tracking the traffic from users that are logged into WordPress (commonly internal traffic).

Use these settings when creating a new trigger.

  1. Name / Untitled Variable: Not Logged into WordPress
  2. Trigger Type: Page View – DOM Ready
  3. This trigger fires on: Some DOM Ready Events
  4. Fire this trigger when…
    1. Choose “WordPress Logged In” variable from drop-down
    2. equals
    3. null
A quick tutorial to stop Google Analytics (GA4) from tracking the traffic from users that are logged into WordPress (commonly internal traffic).

Step 3: Modify Your Original GA4 Tracking Tag

Edit the Firing Trigger for your GA4 Tag in GTM, which was likely set to “All Pages.”

It probably looks something like this. Click the Tag’s name to edit the Tag.

A quick tutorial to stop Google Analytics (GA4) from tracking the traffic from users that are logged into WordPress (commonly internal traffic).
  1. Edit the Trigger section by selecting the pen icon on the upper right corner of the triggering section. If you don’t see it, hover over the area and it should appear.
  2. Select the remove trigger icon on the All Pages trigger to remove it.
  3. Choose the “Not Logged into WordPress” trigger you created previously.
A quick tutorial to stop Google Analytics (GA4) from tracking the traffic from users that are logged into WordPress (commonly internal traffic).
A quick tutorial to stop Google Analytics (GA4) from tracking the traffic from users that are logged into WordPress (commonly internal traffic).

Step 4: Save, Submit, Publish

  1. Save
  2. Submit
  3. Publish

You know this drill having worked in GTM before.

Last, Check That It Works

Test 1 – Make Sure You’re Logged Into Your WP Site

Use the Preview Tool in GTM to open your site. What you’re expecting to see is that your Tag for Google Analytics: GA4 Configuration is NOT firing.

This is what we want. We don’t want your GA4 to fire if you’re logged in.

A quick tutorial to stop Google Analytics (GA4) from tracking the traffic from users that are logged into WordPress (commonly internal traffic).

Test 2 – Make Sure You’re NOT Logged Into Your WP Site

End your Preview/Debug session in GTM. Log out of your site.

Use the Preview Tool in GTM to open your site. What you’re expecting to see is that your Tag for Google Analytics: GA4 Configuration IS firing while NOT logged in.

This is what we want. We want your GA4 to fire if you’re not logged in.

A quick tutorial to stop Google Analytics (GA4) from tracking the traffic from users that are logged into WordPress (commonly internal traffic).

Test 3 – [Optional] Testing in Google Analytics 4 (GA4)

You could also use GA4 > Reports > Real Time to ensure there’s no traffic coming from yourself.

You could also use GA4 > Admin > DebugView to ensure there’s no traffic from yourself.

End Comments

This is how we stop GA4 from tracking logged in users in WordPress. It’s an excellent method for excluding internal traffic if you’re using a dynamic IP address, too. The Google Analytics 4 (GA4) tag will only fire WHEN it’s missing the specific variable for logged in users.

I sometimes write-up internal processes to help me use them when developing new clients. Putting them on my website is a simple way for me to recall the process over several accounts and future clients. But, maybe it can also be used the help others in a similar situation.