is_page ) { $cat = is_front_page() ? 'Front' : 'Page'; } elseif ( $wp_query->is_home ) { $cat = 'Home Page'; } elseif ( $wp_query->is_single ) { $cat = ( $wp_query->is_attachment ) ? 'attachment' : 'Blog Post'; } elseif ( $wp_query->is_category ) { $cat = 'Category'; } elseif ( $wp_query->is_tag ) { $cat = 'Tag'; } elseif ( $wp_query->is_tax ) { $cat = 'Taxonomy'; } elseif ( $wp_query->is_archive ) { if ( $wp_query->is_day ) { $cat = 'Day Archive'; } elseif ( $wp_query->is_month ) { $cat = 'Month Archive'; } elseif ( $wp_query->is_year ) { $cat = 'Year Archive'; } elseif ( $wp_query->is_author ) { $cat = 'Author'; } else { $cat = 'Archive'; } } elseif ( $wp_query->is_search ) { $cat = 'Search'; } elseif ( $wp_query->is_404 ) { $cat = '404 Page'; } return $cat; } ?>

GA4: How to exclude WordPress logged-in users with GTM

When you are working with any kind of data, one important thing is to keep it as clean as possible from data that really doesn’t matter.

No matter what web analytics tool you use, you should always make sure to exclude your own data. The reason is that what you do on your own site usually differs greatly from what your actual visitors are doing.

In this post, I am going to show you how easy it is to exclude your own traffic (or anyone that’s logged into WP Admin) from Google Analytics 4, without having to filter out based on IP.

I am going to exclude everyone that’s logged into your WordPress, but this is easily adapted to e.g. only exclude Administrators only.

First, we need to have Google Tag Manager

In order to get this to work, we need to make sure to have Google Analytics 4 (GA4) installed via Google Tag Manager. Installing Google Analytics through GTM is something you should be doing anyway.

Send the data with a plugin

I’m always going with GTM4WP on all my WordPress installations because of its simplicity and built-in configuration options.

Setting up the basic data collection we need in Google Tag Manager.

In the settings for GTM4WP, head over to the Basic Data tab and then select Visitors.

Make sure to at least tick the box for Logged in status, which need to be active. If you’d rather want to exclude specific user roles or IDs, then also make sure to activate that.

When you have the above selected and visit a page on your site, this is the data that will be available to you in the dataLayer.

visitorId: 1
visitorLoginState: "logged-in"
visitorType: "administrator"

With this data available to us in the dataLayer, we now have information that shows if the user is logged in or not.

Send the data without a plugin

For many reasons, you might not want to run a plugin and in that case, you need to extract the data yourself from WordPress. Thankfully this is very easy, thanks to the WordPress function is_user_logged_in(). All we need to do with the data is to push it into a dataLayer for Google Tag Manager to work with.

Note: The dataLayer() must be declared and populated with data before the GTM Container loads.

The code I use in order to check whether or not a visitor is logged in or not is the following. For the sake of consistency, I’ve named the variable exactly the same as the plugin does so that we can continue in the same way no matter how we collect the data.

    if ( is_user_logged_in() ) {
      echo "dataLayer.push({'visitorLoginState': 'logged-in'});";
    } else {
      echo "dataLayer.push({'visitorLoginState': 'logged-out'});";

Make sure to put these lines of code inside the <head> tag of your site, usually residing under header.php in your Child Theme, or by using a plugin that injects it in the right place.

Create the dataLayer variable in GTM

At this point, all we have is a dataLayer variable containing information whether or not the visitor is logged in or not. Next step is to make sure to include this variable inside Google Tag Manager so that we can push it on to Google Analytics 4.

Creating the dataLayer-variable for visitorLoginState in Google Tag Manager.
  • Create a new variable and give it a proper name.
  • Change the Variable Type to Data Layer Variable.
  • Change Data Layer Variable Name to visitorLoginState.
  • (Optional) Set the Default Value to logged-out.

I usually always set a default value in GTM to make sure I am not sending anything into GA that will be resolved with (not set). In this case I know that all users will be logged out per default, unless they’re logged into the CMS that is, in which case the default value will be overridden with the value logged-in.

Send the information to GA4

In the previous version of Google Analytics (Universal Analytics), I always sent this data as a Custom Dimension and filtered on that, but in Google Analytics 4 you have the Property of traffic_type we can use for this which makes it really simple.

Add the data from visitorLoginState to the Google Analytics 4 Configuration in Google Tag Manager.

First we have to make sure we send the data from Google Tag Manager into Google Analytics 4.

Just go into the Tag Configuration for GA4 and add a new Field as in the screenshot above.

  • Field Name: traffic_type
  • Value: {{DLV – visitorLoginState}}

This will now ensure that everytime we send data from GTM -> GA4, we will also send data whether or not the user performing the action on your site is logged in or not.

Filter out the data in GA4

In the Admin section of your Property in Google Analytics 4, make sure to go under Data Settings and then click on Data Filters.

Creating the filter in GA4 where we exclude  internal traffic.

Create a new filter and change the Parameter value from internal (default) to logged-in, which is the data we’re sending from Google Tag Manager in the case the user is logged in.

Also, don’t forget to make sure the Filter State is set to Active.

Now, all you have to do is to make sure that you and your team stay logged in to WordPress and your data won’t be processed, which is great.

This is naturally also applicable if you are not running WordPress, you just have to adapt where you get the data from in your CMS.

As always, drop a comment if you’ve got questions.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Try Jasper AI (Free Trial)

Create amazing blog posts and marketing copy using extremely well performing templates.
Get original content 10X faster with AI.

© 2023 Daniel Carlbom