Google Analytics: how to filter out traffic from logged-in admins

As a website manager or owner, you probably don't want your Google Analytics account to track your own activity. Not only would this activity inflate your site's traffic figures, but you are likely to use the site fundamentally differently to other users: you might spend longer on a page, view more pages per session and so on. By including your own activity, you would skew all of these metrics.

One common approach to this problem is to build a filter to exclude all traffic from your IP address. In fact, Google Analytics Help has its own guide to doing this.

IP filter or login filter?

But the IP address filter is only useful if you have a static IP address, in other words one that is the same every time you connect to the internet. Even if you think your IP address is static, it may not be; IP addresses for Virgin Media domestic customers, for example, often don't change for months or years but are still technically 'dynamic'. This has caught me out in the past, leaving me with a non-functioning filter. I remained blissfully ignorant of the fact for quite some time, polluting my analytics data with my own visits all the while.

That's not the only limitation; if you access your website with your mobile while out and about, an IP address filter won't block your visits. All in all, an IP address filter is most suitable for a larger business with a fixed IP and a predominantly office-based workforce. For someone like a blogger, I would recommend excluding traffic from logged-in users instead. You will be logged in (as an admin) on your site: your users won't.

There are a few benefits to this approach: you won't be caught out by a sudden change in IP address; it works on any device in any location (as long as you are logged in); and you can still easily test your analytics at any time by logging out of your site. It also works well if you have more than one admin. However it isn't suitable if users are able to log in to your site, for example to change their preferences - or at least it would need to be adapted to take that into account.

These are the basic steps I usually follow:

  1. Pass the user's login state to the data layer
  2. Pass this value to a Google Tag Manager (GTM) variable
  3. Create a Custom Dimension in Google Analytics
  4. Pass the GTM variable value to the Custom Dimension
  5. Create a filter that excludes traffic from logged-in users based on this Custom Dimension

And here's a more detailed guide to how I would accomplish each of these with a WordPress site.

Pass the user's login state to the data layer

I add the data layer to WordPress sites using the free Head, Footer and Post Injections plugin (which, incidentally, is what I also use to add the Google Tag Manager snippet). This saves me having to edit the theme files directly and means that my work won't be lost if the theme is changed in future.

I am neither a JavaScript nor a php developer, but here is a simplified version of the code I am currently using. (Also, I've recently learned that it is always better to use push() when interacting with the data layer, but I am yet to implement this, so please forgive me.)

  1. Make sure the Head, Footer and Post Injections plugin is installed and activated
  2. In your WordPress dashboard, go to Settings > Header and Footer
  3. Paste the above code into the box labelled '<HEAD> PAGE SECTION INJECTION - ON EVERY PAGE'. It's important that the data layer snippet goes BEFORE the Google Tag Manager snippet - otherwise GTM won't be able to make use of the data layer. Take a look at the screengrab below to see what I mean.
  4. Click Save

Head page section injection

Pass the data layer value to a GTM variable

  1. In Google Tag Manager, go to Variables
  2. Click New
  3. Give the variable a name (I called mine 'Login State DL')
  4. Click Choose a variable type to begin setup...
  5. Select Data Layer Variable
  6. The Data Layer Variable Name should exactly match the name used in the data layer, which is of course: loginstate
  7. Click Save

Login State variable

Create a Custom Dimension in Google Analytics

  1. In Google Analytics, go to Admin (cog icon in the bottom left)
  2. Make sure you have the correct Property selected
  3. In the Property column, click on Custom Definitions
  4. Click on Custom Dimensions
  6. Give your Custom Dimension a suitable name, leave the 'Scope' as 'Hit', and make sure that 'Active' is ticked

Add custom dimension

  1. Click Create
  2. Make a note of the 'Index' value for your new Custom Dimension - if it is your first one, then the value will be '1'

Pass the GTM variable value to the Custom Dimension

  1. In your Google Tag Manager workspace, go to Variables
  2. Click on your Google Analytics Settings user-defined variable (I am assuming this is already in place!)
  3. Click on the Variable Configuration box to open up all the settings
  4. Under More Settings, find Custom Dimensions
  5. Add the Index value that you noted down previously (again, this will be '1' if you only have one Custom Dimension)
  6. Under Dimension Value, click the 'Choose a Variable' icon which looks a bit like a Lego brick
  7. Select your Data Layer Variable from before (again, I called mine 'Login State DL'). It will appear with double curly brackets to show that it is a variable - that's fine

Custom Dimensions in GTM

  1. Save the changes to your Google Analytics Settings variable
  2. You've finished in GTM! So click Submit
  3. Give your Version a name and description if you like
  4. Click Publish

Within a few hours, you should be able to start to see your Custom Dimension data in Google Analytics reports. The easiest way to do this is to add your Custom Dimension as a secondary dimension. Here's how:

  1. In Google Analytics, go to a report such as Behaviour > Site Content > All Pages
  2. With Primary Dimension set to Page, click on Secondary Dimension
  3. Search for your Custom Dimension by name and click on it
  4. Voila! You will now see something like the following:

Custom dimension as secondary dimension

Create a filter that excludes traffic from logged-in users

Now it's time to add your Filter in Google Analytics. This is applied at the View level, rather than the Property level. Hopefully you have several Views - my standard setup is to have a Raw View, a Test View, and a Filtered View.

I would add any new filter to my Test View first, and then if all is well I would apply it to my Filtered View. My Raw View is left alone in case my filters go hideously wrong and I need to view the raw, unfiltered data.

  1. In Google Analytics, go to Admin (the cog icon in the bottom left)
  2. Make sure you have the right Property AND View selected
  3. In the View column, click on Filters
  4. Click + ADD FILTER
  5. Make sure Create new Filter is ticked
  6. Give the filter a sensible name, for example 'Exclude logged-in'
  7. Select 'Custom' filter type
  8. Make sure that 'Exclude' is ticked
  9. Click on Select field and select the name of your new Custom Dimension (you can search for it using the search box if that's easiest)
  10. In the Filter Pattern field, add: logged-in
  11. Click Save

Exclude logged-in filter in GA

That's it! You can test your implementation using the 'Realtime' reports in Google Analytics. Remember you have four different scenarios to test:

ViewStateShould pageviews show in Realtime?
With filterLogged inNo
With filterLogged outYes
No filterLogged inYes
No filterLogged outYes
Notify of
Inline Feedbacks
View all comments
Daniel V
1 year ago

Neat guide.

But why not save the custom dimension and hits - and use your variables as an exception trigger to your GA tags along with other trackers such as Facebook, hotjar, etc...

Take care


James Clark
Hi! I'm James Clark and this is my website. I publish all my guides completely free, so if you find them useful then please:
Copyright © James Clark
What do you think? Leave a commentx
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram