Mobile app version of vmapp.org
Login or Join
Cugini213

: Is there an elegant way to track multiple domains under separate accounts with Google Analytics? I have a situation where a content management system uses the same template for multiple websites

@Cugini213

Posted in: #GoogleAnalytics #Javascript #MultipleDomains

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.

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Cugini213

3 Comments

Sorted by latest first Latest Oldest Best

 

@BetL925

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.

10% popularity Vote Up Vote Down


 

@Bryan171

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.

10% popularity Vote Up Vote Down


 

@Carla537

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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme