Is there a way to stop Google Analytics counting development work as hits?

Yeah, you go into Analytics Settings, edit your site, and +Add Filter to define a filter that excludes your IP address.

Past data is not regenerated with filters applied, so you'll only have the benefit of them moving forward.


If you're not using static IP, setting IP filters on GA can't help you.

Set an environment variable and conditionally display it. Take the following Ruby on Rails code, for instance:

<% unless RAILS_ENV == "development" %>
    <!-- your GA code -->
<% end %>

You can extend this behavior every language/framework you use on any operating system. On PHP, you can use the getenv function. Check it out the Wikipedia page on Environment Variables to know how to proceed on your system.


It's 2014 and I'm still unsatisfied with all existing solutions...

  • IP filters require a static IP address. What if I'm working from home or from a coffee shop?
  • 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 are complicated.
  • Opt-Out extensions either block hits on all websites or none at all depending on who you ask.

So, I created my own Browser Extension... https://chrome.google.com/webstore/detail/lknhpplgahpbindnnocglcjonpahfikn

  • 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 works by keeping a "developer cookie" set on your machine at all times just for the domains that you choose. You then simply check for this cookie in your script before sending any data to Analytics. You customize your own unique NAME and VALUE for the cookies in the extension's settings. This can easily be used by a team of people, so developers, content creators, proofreaders, and anyone else in your organization can all view pages without inflating the statistics.

Examples of how to put the code into your 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 your 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 we 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 sees that unique cookie set on your device, then your visit will not count towards your totals in Google Analytics or whatever other analytics tool you prefer to use.

Feel free to share my solution and use my extension to keep those cookies set.


I like the simple approach of using javascript. It works anywhere.

<script type="text/javascript">
if (document.location.hostname.search("myproductiondomainname.com") !== -1) {

//google analytics code goes here

}
</script>