Tracking multiple domains/subdomains and aggregating the data from all sources can be a tricky thing to tackle. I did some research and found a way to use Google Analytics to solve this issue.
The Problem
You have multiple domains to track, but you also want to be able to view the data from all domains in one area. One option is to create multiple filters, which could make your life a bit difficult. Another option is to create multiple properties in Google Analytics and have your website send data to multiple properties in one go.
Many of the solutions I came across described how to do this. However, I found it quite annoying with regard to things such as event tracking. My code basically looked like this:
<a href="teletubbies.com" onclick="ga('send','event','Teletubby','Tinkywinky','Click'); ga('Foo.send', 'event', 'Teletubby', 'Tinkywinky')">Teletubbies</a>
That doesn’t look pretty at all and seems like a waste of time if you’re going to be tracking multiple things in multiple areas. Ideally, I’d want to have one piece of code that triggers this for multiple properties at the same time.
The Solution
Initially, you will need to setup the multiple Google Analytics properties. For this example, we will be using UA-XXX-1 and UA-XXX-2 for the two properties.
Instead of the code you would normally insert at the head of your code from GA, you will update it slightly to use the following:
<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-XXX-2', 'auto'); // Subdomain ga('send', 'pageview'); ga('create', 'UA-XXX-1', 'auto', {'name': 'Collection', 'allowLinker': true}}); // Main Domain/Aggregated Data ga('Collection.send', 'pageview'); </script>
One of the main things you will notice is the addition of “{‘name’: ‘Collection’}” on lines 9 and 10. Each property needs to have its own name to make it unique, so it can be called specifically. For instance, on line 10, we call that specific object to send a page view via “Collection.send” instead of the normal “send”.
Note: You can name it whatever you want!
This code basically sends a page view to your subdomain/subproperty and also a page view to your main domain/aggregated data property. The ideal way would be that the first code is always the subdomain/subproperty, and the second bit of code would be the main/aggregated data property. This way, you can copy and paste your code in the other sites and just update the first property’s ID.
For the next part, we want to have our GA triggers get sent to both properties without making our code look really ugly. A clean and simple solution was provided by Philip Walton over at Stack Overflow:
<script type="text/javascript"> function runGACommand() { var mainTrackerArgs = Array.prototype.slice.call(arguments); var collectionTrackerArgs = mainTrackerArgs.slice(0); // Sets the "Collection" name on the second set of arguments. collectionTrackerArgs[0] = 'Collection.' + collectionTrackerArgs[0]; // Runs the command on both trackers. ga.apply(null, subTrackerArgs); ga.apply(null, collectionTrackerArgs); } </script>
Then all you would need to do is the following:
<a href="teletubbies.com" onclick="gaCommand('send','event','Teletubby','Click','Tinkywinky');">Teletubbies</a>
Optional Step
If you would like to have the domain names show up in reports for the aggregated data, do the following steps in the main/aggregate data property. Note that this is optional.
- Admin > Filters > Add Filter
- Filter Type: Custom filter > Advanced
- Field A: Hostname Extract A: (.*)
- Field B: Request URI Extract: (.*)
- Output To: Request URI Constructor: $A1$B1
Questions? Comment below or reach out to us on twitter @MediaCause.
The post Tracking Multiple Properties And Aggregating Data in Google Analytics appeared first on Media Cause.
0 التعليقات:
Post a Comment