Exclude Internal Traffic in Google Universal Analytics (analytics.js)

Recently, I was working on a new feature for one of my existing web apps when I realized that internal traffic was also being captured into our Google Analytics account. Due to lack of time and pressure for launching the web app, one of my team members added basic tracking but didn’t get the time to add a way of excluding internal traffic. Now this topic has been blogged about by lots of authors out there, but the reason for yet another blog article on it was due to shortage of how to achieve this functionality in the new Google Universal Analytics framework.

The objective of this blog is to cover different ways you can exclude internal traffic in the new Google Universal Analytics.

4 ways to exclude internal traffic

There are 4 ways of excluding internal traffic in the new Universal Analytics (analytics.js) system. Some of them have existed in the classic Google Analytics (ga.js) system, while others are new.

  1. Google Analytics Opt-Out Browser Plugin
  2. Filter by IP Address/Internet Service Provider
  3. Using “User Opt-out” feature
  4. Using “Custom Dimensions and Metric” feature

Due to lack of time, I am going to only explain option 3 in this blog post. For those interested in options 1 and 2, there are a lot of blogs out there that explain how to achieve them. Option 4 will be covered in a follow up blog post.

Using “User Opt-out” feature

The User Opt-out feature allows developers to offer the option to opt out of analytics tracking to users. If users don’t want us to track information about them (such as IP address, browser, country, etc.) we can use this feature instead of removing the JavaScript tracking snippet altogether.

When you think about it, that’s exactly the sort of functionality we are looking to achieve over here. From the perspective of any site or web app, anyone, who goes to to the URL where the site/application can be accessed from,  is a user. We just want a way to tell the system ignore whatever we are doing on the site.

Here is how to achieve this:

  1. We need a page where we can specify disable all tracking from this device. Caution: This page should not known or discoverable to users. In my example below, I have added a very basic version of how this page should look like but for your sites, please determine what sort of security measures you would like to implement to ensure only your team can disable tracking.DoNotTrack
  2. In the header where you have your existing Google Analytics code, you want to check whether the DoNotTrack cookie exists or not. If it does, then check its value. If the value is true, then use the code specified the User opt-out page of Google Analytics.GoogleAnalyticsWithDoNotTrack
  3. Finally, make sure you visit the page where the Do Not Track cookie gets create. You will have to do this from every device you would be using to view your site/web app. If you are like me, then you should visit this page from your Macbook Pro, Microsoft Surface and iPhone!DoNotTrackPage

And that’s it! Now any traffic generated from your devices will not be mixed with traffic from your actual users!

I hope you find this information useful. Please feel free to post comments/questions/any feedback and I will get back to you as soon as possible.

Thank you for reading and happy coding!


Add a Comment

Your email address will not be published. Required fields are marked *