There are problems with many of the popular answers to this question...
- Even if you're lucky enough to find an IP range, what if you're working from a coffee shop or hotel?
- Checking host name eliminates hits from a dev environment, but what if I'm debugging the live site?
- Editing server configurations is annoying/advanced and multiple domains become complicated.
- Opt-Out extensions either block hits on all websites or none at all depending on who you ask.
So, I combined several other solutions into something that works for me...
- It follows me wherever I go
- It works on a dev environment and on live/public domains
- It only affects me and the sites that I'm developing
- It turns on/off with one click
- It's easy to verify that it is truly not sending any data to analytics
- It can be used by a whole team of people
I keep a "developer cookie" set on my machine at all times just for the domains that I manage. This cookie has a unique value that is specific to me. Then I simply check for this cookie in my scripts before sending any data to Analytics.
Examples of how I put the code into my pages...
JavaScript
if (window.location.host==="mydomain.com" || window.location.host==="www.mydomain.com") {
if (document.cookie.indexOf("COOKIENAME=COOKIEVALUE") === -1) {
// Insert Analytics Code Here
}
}
PHP
if ($_SERVER['HTTP_HOST']==="mydomain.com" || $_SERVER['HTTP_HOST']==="www.mydomain.com") {
if (@$_COOKIE["COOKIENAME"] !== "COOKIEVALUE") {
// Insert Analytics Code Here
}
}
Verifying that the HOST name equals the domain of my live site ("mydomain.com") ensures that the analytics data will never be sent by ANY visitor while viewing from a test domain such as "localhost" or "beta.mydomain.com". In the examples above, "www.mydomain.com" and "mydomain.com" are the two valid domains where I DO want visits to be recorded.
The live site sends data to analytics as expected UNLESS a developer cookie is found with matching values. If it finds that unique cookie set on my device, then my visit will not count towards my totals in Google Analytics (or whatever other analytics tool I might decide to use one day).
But what happens when my cookies get cleared? How do I keep the "developer cookie" set in the background? I created my own Browser Extension for that...
https://chrome.google.com/webstore/detail/lknhpplgahpbindnnocglcjonpahfikn
It works just for the specific domains that you choose. You customize your own unique NAME and VALUE for the cookies in the extension's settings.
This method can easily be used by a team of people as long as they use the same NAME/VALUE pair, so developers, content creators, proofreaders, and anyone else in your organization can all view pages without inflating the statistics.
Feel free to share my solution and use my extension to keep those cookies set.