Monday, October 29, 2012

Google Analytics and development/staging/production servers

It's common (best practice, actually) to have multiple copies of a site for development, staging, production, etc. Ideally you want to use the same code on all the sites. This can lead to an analytics problem -- having data from the dev and staging sites pollute the production data. One approach to dealing with this is to add a filter to the analytics profile to remove any data coming from the staging or dev servers.

I've starting modifying my analytics code as follows:
if (!/staging|localhost/.test(window.location.hostname))
 _gaq.push(['_setAccount', 'UA-XXXXX-X']);

In brief, if we're on our staging server, don't set the web property ID . This results in the tracker using a default ID -- something like "UA-99999-9".

  • The analytics code functions normally, but any data is sent to the default web property ID. This is nice because it's possible to verify that the analytics code is working.
  • More Javascript (the hostname comparison) that runs on every page.
Note: I used to put the conditional on the _trackPageview call -- The problem with this is that other analytics code (like _trackEvent, etc) will still be sending data.

No comments:

Post a Comment