I’ll try and keep this as simple as possible, and illustrate with an example so you can see what I’m doing. First, check the problem below to make sure the scenario matches what you’re trying to achieve.

Problem
Filter all WordPress logged in users out of Google Analytic (GA) reports. GA is setup using Google Tag Manager (gtag).

Requirements

  • Don’t use a plugin eg Monster Analytics – Our general website development policy for WordPress is use as few plugins as possible for improved speed and security.
  • Segment logged in users by cookie (instead of IP address, particularly with a lot of staff working from home) and no dedicated IP in the office.
  • Set up as part of Google Tag manager

Solution
In summary:

  1. Add a function in WordPress to detect a logged in user and set a cookie.
  2. Add a custom dimension in GA.
  3. Create a new view in GA
  4. Create a filter in GA to exclude traffic
  5. Add cookie variable in gtag and attach to GA tag.
  6. Test and publish gtag

1 – Detect logged in users

The first thing we’re going to do is detect anyone who is logged in (regardless of role) and create a cookie called ‘user_is_admin’. The value of the cookie will be the user’s primary role e.g. administrator (in most cases a user will only have one role, so you’ll see in the code below we retrieve the first element [0] of the roles array). This means you can filter your GA reports by one specific role (if you want).

Add the following function to your WordPress theme’s function.php file.

/* add 180-day cookie for logged in users to exclude them from GA view */
function my_exclude_admin_cookie() {
    $current_user = wp_get_current_user();
    $roles = $current_user->roles;
    $mainrole = $roles[0];
    $expire_time = time() + 60 * 60 * 24 * 180;
    setcookie( 'user_is_admin', $mainrole, $expire_time, '/' );
}
if( is_user_logged_in() ) {
    add_action('init', 'my_exclude_admin_cookie');
}

After saving, use the inspector to check the cookie is being created correctly.

For non-WordPress websites, you’ll just need to figure out a way to identify logged in users on your site (e.g. login page url) and use that (you don’t have to create a cookie, you could push a variable using JS instead).

2 – Create a custom dimension in GA

Log in to your GA and go to the Admin section > Property > Custom Definitions > Custom Dimensions. Click New Custom Dimension.

Save the new custom dimension. Take note of the index number – you’ll need this when setting up the variable in Tag Manager.

Learn more about scope

3 – Create a new view in GA

Best practice with GA is to have a primary default view that is ALL data. Don’t delete this view (I once had a bad experience deleting a view and that data is not recoverable – although I see Google have since added a safety net where deleted views can be restored, unfortunately too late for me!).

Create a new view. This is the reporting view that will automatically exclude any logged in user activity.

4 – Create a filter to exclude logged in traffic

In the new view you’ve just created, go to the Filters section and click Add Filter. Under Custom, choose Exclude and select the Custom Dimension you created earlier (e.g. User is Admin).

Because the user_is_admin cookie value could be any WordPress role, I’m going to enter a regex regular expression (below) in the Filter Pattern to catch any value.

(?!^ +$)^.+$

As a custom filter, you won’t be able to verify it here, but in my experience the filters start working within a few minutes once it’s all setup (I had read 24 hours but you shouldn’t have to wait that long, so if it’s not working double check your setup instead of just waiting).

Save your filter.

5 – Add the variable in Google Tag Manager (gtag)

Standard implementation for collecting custom dimension data is to add the script to the page. If you’ve got your GA as part of Tag Manager then you can bypass that by including the variable with your Google Analytics tag.

First, add the variable (User-Defined Variables).

Then, include it with your Google Analytics tag. You will need to click the ‘Enable overriding settings in this tag’ checkbox and go down into More Settings, where you’ll add the Custom Dimension. Enter the Index number and the value, which you just created.

Save your changes.

  • Test and publish Tag Manager changes
  • You can then switch Tag Manager into Preview mode and test that the variable is being generated correctly (comparing logged in to logged out visits).

    Publish your Tag Manager workspace. Your filter should start working straight away.

    Your new custom dimension can also be used for regular GA reports (you’ll find it under the Secondary dimension drop-down).

    Happy reporting!

    How to exclude WordPress users from Analytics (without a plugin)