Mild-Mannered Canadian Fury

Doug Stephen is Politely Peeved

Nerd Tip of the Day: How To Block Yourself From Your Site's Google Analytics (Carpet Bomb edition)


Tue, 12 Jun 2012 Ā«permalinkĀ»

When you work on the web, you find yourself frequenting your own properties quite a bit. As it stands, you probably don’t want your own visits to your site added in to your analytics and metrics, so it is often helpful to block yourself from your service. Additionally, depending on your development stack, it can be difficult or maybe just tedious to exclude your analytics headers from your site when you are doing local development/design; this can lead to an inordinate amount of skewed data due to the constant refreshing you’ll be doing while you work.

I’m going to quickly talk about how to block yourself from Google Analytics (it isn’t hard, and for a lot of you this probably won’t be any sort of revelation), but be warned: I couldn’t find a ton of useful data on this topic, so this is a nuclear option. This will block you from showing up on the Google Analytics service entirely, whether for yourself or on any other website. You ready? Here it goes:

Using your hosts file, redirect the Google Analytics request endpoint URL’s to a bad IP address.

A fucking revelation, I know. Google Analytics provides communication between the scripts on your site and its application services through two URLs: http://www.google-analytics.com and http://ssl.google-analytics.com. I reroute both of these URL’s to localhost (127.0.0.1) in my hosts file so that I don’t log presence on any site with Google Analytics because the scripts can’t communicate or execute properly.

Now, let’s say that you want the ability to turn this blocking on and off. Maybe you only want this on while you’re doing some local development and the rest of the time you want to be able to contribute in some small part to the success of your peers on the web. Constantly editing your hosts file is a huge pain. Thankfully, if you’re on OS X, there’s an easy way. Even better, it’s free.

The “way” is an open-source application for Snow Leopard and Lion called Gas Mask. Gas Mask is a hosts file editor and manager that lives in your menu bar for quick access; not only can you quickly tweak hosts files, but you can keep several hosts files sitting around to quickly switch around between them. I keep two hosts files in my Gas Mask, one that reroutes Google Analytics to localhost and one that doesn’t, so that I can quickly switch back and forth. It has a ton of other advanced features that I won’t get in to, but if you’re a web developer on OS X then Gas Mask is a tool that is almost required to be in your arsenal. Check it out.