Вопрос

I have one website, which can be reached under three hostnames. Each hostname has a different domain name.

For example, my website can be reached at www.example.com, www.example2.com and www.example3.com. On each the same content is served. It's duplicate content.

For each hostname I have created a universal analytics property under one account. So for example UA-XXXXXX-1, UA-XXXXXX-2 and UA-XXXXXX-3

The content which appears is identical on all hostnames. So there is only one folder on the webserver that serves the same HTML content for all hostnames.

It's like stackoverflow.com could be reached also under stackoverflow2.com and do on.

How does the tracking code have to look like, so that I collect stats for each hostname? I have tried the following javascripts, all of which won't work:

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXXX-1', {'cookieDomain': 'example.com'});
ga('create', 'UA-XXXXXX-2', {'cookieDomain': 'example2.com'});
ga('create', 'UA-XXXXXX-3', {'cookieDomain': 'example3.com'});
ga('send', 'pageview');
</script>

I also tried:

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXXX-1', 'example.com');
ga('create', 'UA-XXXXXX-2', 'example2.com');
ga('create', 'UA-XXXXXX-3', 'example3.com');
ga('send', 'pageview');
</script>

I also used named trackers in combination with cookieDomain, with no success.

How does the single javascript snippet look like in order to track the visits of all pages?

Это было полезно?

Решение

You need only one property, you can then filter views by hostname (and have rollup view to track the numbers for alle three combined). That would reduce the necessary code to

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXXX-1', 'auto');

ga('send', 'pageview');
</script>

Where the "auto" parameter to ga create means that the cookie domain is set to the current hostname.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top