williamsandonz
williamsandonz

Reputation: 16440

Analytics on test site + live site

We are deploying .war servlet,s on test and live.

I can't be bothered putting conditional code to include analytics or not, can I use same code on both sites, and will analytics ONLY show the stats for the live site? (as that's the registered domain)

Will it exclude test hits?

Upvotes: 0

Views: 214

Answers (3)

ryanve
ryanve

Reputation: 52601

Yea just compare the hostname. Like this:

<script>if ('example.com' === window.location.hostname){
    // Google Analytics - mathiasbynens.be/notes/async-analytics-snippet - Change UA-XXXXX-X to your ID:
    var _gaq=[["_setAccount","UA-XXXXX-X"],["_trackPageview"]];
    (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.async=1;g.src=("https:"==location.protocol?"//ssl":"//www")+".google-analytics.com/ga.js";s.parentNode.insertBefore(g,s)}(document,"script"));
}</script>

If you're using www. then check that too.

Upvotes: 0

greg
greg

Reputation: 2349

I recommend you leave on your Analytic test site: you have to test that the tracking works.

The conventional approach is to create an Analytics profile which takes into account only the test version and an other that excluded this version. You can do this thanks to the filters (based on your host or IP) in the Analytics interface.

Upvotes: 3

Theron Luhn
Theron Luhn

Reputation: 4092

You could use location.host to determine whether the site is in development or not.

if(location.host=='mysite.com') {
    _gaq.push(['_trackPageview']);
}

Or maybe the inverse of that code:

if(location.host!='localhost') {
    _gaq.push(['_trackPageview']);
}

Upvotes: 1

Related Questions