Need a developer?

Hiding visits to your own site in Google Analytics

When working on a site, it’s common to browse it yourself from multiple browsers, multiple devices and multiple locations for testing purposes. For analytics about your user behaviour to be useful, we need a way to exclude this internal traffic from Google Analytics reports. In this post I explain a method I found useful for hiding visits to your own site when you want to be able to test your site from any browser or any location.

Google recommends setting up a filter on an IP range within Google Analytics to filter out internal traffic. This would be a foolproof method if your work IP address is always within a set range but isn’t going to help if you sometimes browse from a different location. However, this method could be used in combination with the following one I prefer.

Excluding traffic using a custom dimension

My preferred method is:

  1. Use a hidden page on your site that you initially browse to that attaches a custom dimension to your Google Analytic’s user.
  2. Filter that user’s activity from reports by looking for that custom dimension from within the Google Analytics web interface.

As well as working from any location, this also has the advantage of letting you confirm analytics activity is being successfully sent unlike approaches that disable the Google Analytics script for internal users altogether. I’ll now explain how to implement the above approach.

Creating a custom dimension for internal users

First you have to create a custom Google Analytics dimension:

  1. Visit the Google Analytics admin panel and select the site you’re working on from the “property” field.
  2. Select “Custom Definitions”, “Custom Dimensions” then “New Custom Dimension”.
  3. Create a new dimension with the name “Internal user” with “Scope” set to “User”. This is the label we’ll use to track internal users.
  4. Take note of the value of the index field that appears next to your new dimension as you’ll need it in the next step.

A hidden page to label you as an internal user

You then need to create a hidden page on your site that sets this dimension for the current user by sending a message to Google Analytics. You can do this with HTML such as the following:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="robots" content="noindex,nofollow"/>
    <title></title>
  </head>
  <body>
    <p>Internal user dimension for Google Analytics set so activity from
    this browser can be filtered. Redirecting to homepage...</p>

    <script>
      <!-- Standard Google Analytics snippet -->
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]
      ||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new
      Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=
      1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script',
      '//www.google-analytics.com/analytics.js','ga');

      ga('create', /*YOUR GOOGLE ANALYTICS ID HERE*/, 'auto');
      ga('require', 'displayfeatures');
      <!------------------------------>

      ga('set', 'dimension/*YOUR DIMENSION INDEX HERE*/', 'true');
      ga('send', 'pageview');
      setTimeout(function() { window.location.href = "/"; }, 5000);
    </script>
  </body>
</html>

The JavaScript code does the following:

  1. Uses the standard script snippet for loading Google Analytics. You’ll need to update the snippet with your Google Analytics ID.
  2. Sets the “Internal user” dimension for the current user. You’ll need to update the snippet with your dimension index.
  3. Redirects the user back to the homepage after a delay.

We also set the robots metatag so web crawlers won’t list this page in search results.

Excluding your custom dimension from reports

Next, you need to exclude any users that have the custom dimension attached to them from reports:

  1. From the Google Analytics admin panel, select “Filters” then “New Filter”.
  2. Set the filter type to “Custom”, set “Filter Field” to “Internal user”, set “Filter Pattern” to “true” then save the filter.

Conclusion

Now any time you want to browse your own site on a different browser or device, simply go to your hidden page first and your pesky internal traffic will be filtered out of your Google Analytics reports. Hopefully you’ll find this a useful way for eliminating noise from your user analytics.