Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have a situation where a content management system uses the same template for multiple websites with different domain names and I can't make a separate template for each. However, each website needs to be tracked with Google Analytics. Would this be appropriate to track each domain like this by putting in some conditional code? And would this be robust enough not to break? Is there a more elegant way to do this?

<script type="text/javascript">
  var _gaq = _gaq || [];
  switch (location.hostname){
    case 'www.aaa.com':
        _gaq.push(['_setAccount', 'UA-xxxxxxx-1']);
        break;
    case 'www.bbb.com':
        _gaq.push(['_setAccount', 'UA-xxxxxxx-2']);
        break;
    case 'www.ccc.com':
        _gaq.push(['_setAccount', 'UA-xxxxxxx-3']);
        break;
  }

  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
  })();

</script>

Just to be clear, each website is a separate domain name and must be tracked separately, NOT different domains with same pages on one analytics profile.

share|improve this question
add comment

3 Answers 3

You can use one tracking code and setup segments within analytics to seperate the sites based on domains.

share|improve this answer
    
You can do this, but it's a major headache, any url based report filtering is a mine field, and you're never sure if you're seeing the right data. Better to have multiple accounts. –  toomanyairmiles Jun 13 '12 at 19:07
    
@toomanyairmiles The company I work for employed your method at first, but traffic comparison had to done manually. With the method I've described we didn't see significant change in the result values, but comparison was available, and setting up 3 segments is a simple task in GA. Try managing 18 or more domains. –  Aznim Jun 18 '12 at 14:16
1  
If you're trying to run that many domains then you should be using a pay to play solution. GA was never designed for this kind of stuff. Just because it can doesn't mean you should. –  toomanyairmiles Jun 18 '12 at 14:33
    
@toomanyairmiles We could go on and on about this, but my hands are tied. My company, my clients all require GA and don't want to hear about other solutions, free or paid. Only my employer reuqires the type of tracking mentioned above, but GA handles it greatly with segments, or with profile/directory filter structure (where it can be used...). –  Aznim Jun 19 '12 at 7:23
add comment

Do the switch statement for your GA code server side - so instead of using location.hostname, use $_SERVER['SERVER_NAME'] (if it's PHP).

That way only one profile will be on your multiple sites, however with different profile ids. Putting this code in JS could be confusing and may break.

share|improve this answer
add comment

Your solution of using the location.hostname in JavaScript is good enough. Google analytics relies on JavaScript to work. Your solution won't be any more fragile than the rest of the Google Analytics tracking.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.