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.
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.
- Google Analytics 4 (GA4) property
- 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
- Open your site and view Page Source.
- 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:
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.
- Go to the Variables section in the left menu.
- See the “User-Defined Variables” section at the bottom
- In the upper right of that section, click the “New” button.
Use these settings to create a new User-Defined Variable:
- Name / Untitled Variable: WordPress Logged In
- Variable Configuration: DOM Element
- Selection Method: CSS Selector
- Element Selector: body.logged-in
- Attribute Name: class
- Hit “Save” in the upper right
Step 2: Create A New Trigger
Next, in Google Tag Manager, go to the Triggers section in the left menu. Click “New.”
Use these settings when creating a new trigger.
- Name / Untitled Variable: Not Logged into WordPress
- Trigger Type: Page View – DOM Ready
- This trigger fires on: Some DOM Ready Events
- Fire this trigger when…
- Choose “WordPress Logged In” variable from drop-down
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.
- 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.
- Select the remove trigger icon on the All Pages trigger to remove it.
- Choose the “Not Logged into WordPress” trigger you created previously.
Step 4: Save, Submit, 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.
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.
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.
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.