Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do I get Google Analytics to stop counting me?

The easy way to stop Google Analytics from tracking your own site views. Install a Chrome extension called Block Yourself from Analytics. That's it. You're done.

How do I turn off development in Google Analytics?

To disable Analytics programmatically, set the following window property to true : window['ga-disable-GA_MEASUREMENT_ID'] = true; Replace GA_MEASUREMENT_ID with the Analytics ID of the property that you would like to disable.

Why you should stop using Google Analytics?

It's overkill for the majority of site owners For most site owners, the amount of data Google Analytics collects is overkill. It's a powerful but complex tool that takes time to understand and requires training. Most people find real and regular use for only a fraction of the metrics it measures.

What is hit count in Google Analytics?

An interaction that results in data being sent to Analytics. Common hit types include page tracking hits, event tracking hits, and ecommerce hits. Each time the tracking code is triggered by a user's behavior (for example, user loads a page on a website or a screen in a mobile app), Analytics records that activity.


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>

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.


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.


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.


You can use this code

<script>
var host = window.location.hostname;
if(host != "localhost")
{
    // your google analytic code here
}
</script>

We setup a 2nd google analytics tracking code for development and QA work -- actually comes in handy when you want to test your analytics integration, also ensures one doesn't have bleedover into production stats.