Welcome to Shaun Luttin's public notebook. It contains rough, practical notes. The guiding idea is that, despite what marketing tells us, there are no experts at anything. Sharing our half-baked ideas helps everyone. We're all just muddling thru. Find out more about our work at bigfont.ca.

Google Analytics: Track One Website with Two Domains (e.g. dot-com and dot-ca) with Universal Analytics

Tags: google-analytics

The Muddling Thru blog has two domain names: muddlingthru.ca and muddlingthru.com. I wanted to setup Google Analytics to track both. Here is the code that did it.

Example

<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');

// this must happen before require ga('create', 'UA-XXXXXX-X', 'auto', { 'allowLinker': true });

// this links the two domains ga('require', 'linker'); ga('linker:autoLink', ['muddlingthru.com', 'muddlingthru.ca']);

// this sends the analytics ga('send', 'pageview');

</script>

Test

  1. Install the Google Analytics Debugger in Google Chrome.
  2. Turn it on.
  3. Open the JavaScript console (Ctrl + Shift + J)
  4. Reload the page to view possible errors.

See also

https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain#autolink

https://chrome.google.com/webstore/detail/google-analytics-debugger/jnkmfdileelhofjcijamephohjechhna?hl=en