1

I've searched around on SO, and found a few questions that ask about this, but most of the answers that I've come across all seem to suggest either filtering via IP range or introducing conditional code to the engine powering the site; neither of these options are viable for me. If this does turn out to be a duplicate where this an answer that fits in to my use case, then I apologize in advance, but I just can't seem to find anything.

My original method of filtering myself out from Google Analytics was to simply redirect requests to google-analytics.com and the https version of GA to localhost in my hosts file. This works just fine on my desktop, but it is useless when testing on mobile devices like an iPhone (unless somebody knows how to finagle the iPhone's hosts file without jailbreaking).

My setup for local testing is to use pow with .dev domains for the local project directories, and then when I need to see the development version of sites on mobile devices I use Xip.io in conjunction with Adobe Shadow.

I can't filter by IP range because I frequently work from home, and my ISP doesn't provide static addresses for residential accounts.

I tried to set up a Custom exclude filter in my Google Analytics profile to filter out traffic originating from these domains, but they either don't work or I have a really bad misunderstanding of how the Exclude filters work (which is entirely possible). I have Custom Filters set up to Exclude based on the Hostname, matching the patterns \.dev and .*xip\.io/.* but these filters do absolutely nothing. I believe this is because this filter is, technically, looking for the hostname of the originating domain and not the hostname being requested, but I'm really not sure because the language is vague and differs depending on which help document you're looking at.

Short of manually removing the GA tracking codes from the site during local dev then going back and adding them back in, does anybody have any suggestions or can anybody tell me what I'm doing wrong with my Exclude filters?

Doug Stephen
  • 7,181
  • 1
  • 38
  • 46

2 Answers2

2

I've been using a variation on the analytics tracking code to prevent page views from being tracked on our staging server -- something like:

if (!/\.dev|xip\.io/.test(window.location.hostname)) _gaq.push(['_trackPageview']);

Alternatively, you could apply this to the _setAccount call. If you do this and look at the tracking requests, the web property ID shows up as 'UA-XXXXX-X'

mike
  • 7,137
  • 2
  • 23
  • 27
1

You could rely on

 window['ga-disable-UA-XXXXXX-Y'] = true;

where UA-XXXXXX-Y is the account ID. (details here).

Like creating a page or webserver directive that sets a cookie (page reserved to developers to be excluded from ga), and in your code that loads ga:

if (hasDeveloperCookie()) {
     window['ga-disable-UA-XXXXXX-Y'] = true;
}
Community
  • 1
  • 1
guido
  • 18,864
  • 6
  • 70
  • 95
  • This looks promising. I'm assuming that due to the nature of cookies, I would have to implement this "landing page" individually for every site that I am testing locally, correct? I haven't done much work with cookies yet so I'm just wanting to make sure I understand the principles behind your answer. – Doug Stephen Sep 24 '12 at 17:04
  • I think so, a cookie is valid for a domain (eventually domain/path) and has an expiration. You could set the cookie (maybe directly in javascript, or maybe with php for instance), and then show some visual indication on the page that cookie is loaded and GA is not loaded. Each desktop browser has function to delete cookies, I am not sure about mobile browsers. You should _include_ in some way the script containing GA code in your pages, in order to have just a single point on a website where to implement this modification. – guido Sep 24 '12 at 17:22