Support cross domain tracking with Google analytics

AI-generated summary

The discussion revolves around supporting cross-domain tracking with Google Analytics in Discourse. TopherTheGeek explains the issue of tracking multiple domains as separate properties and how Google provides a solution to track them as a single property. However, implementing this requires tweaking the GA code, which is not currently possible in Discourse.

TopherTheGeek proposes a potential implementation detail by adding an option for ga linked domains to adjust the GA tag accordingly.

In the meantime, TopherTheGeek and thangngoc89 suggest workarounds. TopherTheGeek recommends using Admin → Customize → Text Content → Bottom of the pages to paste the GA tag with linked domains, while thangngoc89 suggests adding the GA code to Admin > Customize > Text Content > HTML head as a more straightforward solution.

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 лайк

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 лайк

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 лайка