Support cross domain tracking with Google analytics

We have multiple sites (for example, forum.treode.com, treode.github.io and www.treode.com), which I’m sure is not uncommon. Currently we track each one as a separate “property” in Google Analytics. However, when a user moves from one site to the next, it shows up as a drop-off in analytics. Google has written this helpful page to explain how one can track multiple domains as a single property and see viewers flow from one domain to the next.

One of the steps to make that work involves tweaking the GA code that’s on the webpage. It appears that I don’t have a way in Discourse to make that happen.

1 Like

Implementation Detail

I was thinking about how to implement this. Currently, under Admin -> Basic Setup there are options ga universal domain name and ga tracking code. These options trigger the addition of this script for Google Analytics:

<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-12345678-1', 'auto');
ga('send', 'pageview');
</script>

To support this feature, we could add the option ga linked domains. If that was blank, we would continue to emit the tag above. However, if it contained a comma separated list of domains, we’d adjust the tag to look like this:

<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-12345678-1', 'auto', {'allowLinker': true});
ga('require', 'linker');
ga('linker:autoLink', ['a.example.com', 'b.example.com', 'etc.example.com']);
ga('send', 'pageview');
</script>

The difference between these two is in the line that begins ga('create' , and the two additional lines after it.

Workaround

In the meantime, I found that there’s a simple workaround. You can get this by using Admin -> Customize -> Text Context -> Bottom of the pages. I cleared my Google Analytics settings under Basic Setup, and pasted the Google Analytics tag (with linked domains) into Bottom of the pages.

1 Like

You can easily do this by add your Google Analystic code into Admin > Customize > Text Content > HTML head
I never use Discourse build in to embed GA.

4 Likes