How to remove self traffic from Google Analytics

How many times do you visit your product website in a day? 10 times a day 🙂 Yes, you are obsessed like me. But the problem is – I always get bugged by too many visits from my city. Wondered how many of these are actual visitors? Recently I was deploying Google Analytic tracking code to some of the page of QwikDial.com and found a deterministic way to exclude my own generated traffic.

Solution:
Google Analytics Web API has a feature to dynamically disable reporting of analytic data. For example, if you want a page to stop sending anymore analytics after some user action, you can set a global variable and no more data is reported back to Google servers.

Step 1: Consider that your tracking code is UA-XXXXXXXX-Z. Create a new HTML file disable-ga.html with following contents:

<script>

	// Set your tracking ID of the web property. (Can be copied from tracking code)
	var gaProperty = 'UA-XXXXXXXX-Z';
	var disableStr = 'ga-disable-' + gaProperty;

	// Create a cookie for lifetime. Opt-out.
	document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
	window[disableStr] = true;


</script>

Step 2: As per the Google Analytics API documentation, if analytics.js finds that window['ga-disable-UA-XXXXXXXX-Z'] is true, then nothing is reported to Google servers. Ensure that you add following code just before tracking your code in all the pages. In the following code we are checking against a cookie and setting window tracking property to false. This ensures that analytics.js does not track any of the data from onwards. Paste this code to all your pages just before the tracking code.

<script>

	// Set your tracking ID of the web property. (Can be copied from tracking code)
	var gaProperty = 'UA-XXXXXXXX-Z';

	// Check if cookie exists, set a window property.
	var disableStr = 'ga-disable-' + gaProperty;
	if (document.cookie.indexOf(disableStr + '=true') > -1) {
	  window[disableStr] = true;
	}

	»»»paste your tracking code here«««

</script>

Step 3: Upload this disable-ga.html page to somewhere on server. Open this page from all the web-browsers you use. Make sure you visit both http://qwikdial.com/disable-ga.html and http://www.qwikdial.com/disable-ga.html . (The difference is sub-domain, else you might end up seeing some self traffic). Any subsequent visits on this tracking code will not be tracked, unless the browser cookie is deleted. You can verify this by opening your website and checking out Realtime visitors in GA.

There are some other solutions as well:

  1. Install a Ad blocking plugin for your browser. For example: AdBlockPlus for Firefox can block all reads to analytics.js
  2. If you have static IP address, you can also create a filter. Go to Admin, select the view and then open Filters. Here you can configure filters based on IP address.

One thought on “How to remove self traffic from Google Analytics

  1. Aniket

    Your articles and thoughts are really great..but I am wondering where is the link to this blog from your homepage? You would be glad to know that your 2008 articles still rank on page 1 of google and thats how I came here.

Leave a Reply

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