Asitha De Silva
Asitha De Silva

Reputation: 183

Google Analytics - Tracking Multiple Properties Including Parent

I'm trying to setup tracking for multiple subdomain properties while also sending data to the main property.

Right now, we have the following setup:

domain.com - UA-XXX-1

foo.domain.com - UA-XXX-2

bar.domain.com - UA-XXX-3

If a user visits the domain "foo.domain.com", I want there to be data sent to UA-XXX-1 and UA-XXX-2 for a pageview. From my understanding, I need to setup the GA code as such:

<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-1', 'auto');
ga('send', 'pageview');
ga('create', 'UA-XXX-2', 'auto',{'name': 'Foo'});
ga('Foo.send', 'pageview');
</script>

Setting this up for all subdomains as such would mean that if I go to UA-XXX-1, I will see a mixture of data from all the subdomain properties I've setup, and if I go into the individual ones, I'll only see data for those specific properties. Easy.

However, what I'm trying to figure out is, if this the proper method to use when it comes to adding things like event/conversion tracking?

If I had a link as such:

<a href="teletubbies.com" onclick="ga('send','event','Teletubby','Tinkywinky','Click');">Teletubbies</a>

This would only send the event to the UA-XXX-1 property, and not the second property UA-XXX-2 that was setup as well, correct? Does this mean, that for each tracking code I will need to add in two separate tracking codes just to send an event tracking or is there a simpler solution?

<a href="teletubbies.com" onclick="ga('send','event','Teletubby','Tinkywinky','Click'); ga('Foo.send', 'event', 'videos', 'predators');">Teletubbies</a>

Upvotes: 1

Views: 626

Answers (1)

Philip Walton
Philip Walton

Reputation: 30461

Everything you have above is correct. And yes, you'll need to send the hits twice to have them show up in two separate properties.

If you don't like repeating yourself so much, you can abstract away the two send commands into a single JavaScript function that always sends the hit to both properties.

For example:

function runGACommand() {
  var mainTrackerArgs = [].slice.call(arguments, 0);
  var fooTrackerArgs = mainTrackerArgs.slice(0);

  // Sets the "Foo" name on the second set of arguments.
  fooTrackerArgs[0] = 'Foo.' + fooTrackerArgs[0];

  // Runs the command on both trackers.
  ga.apply(null, mainTrackerArgs);
  ga.apply(null, fooTrackerArgs);
}

Now you can simplify your event handlers to be just this:

<a href="teletubbies.com" onclick="runGACommand('send','event','Teletubby','Tinkywinky','Click');">Teletubbies</a>

And it will send the data to both properties.

Upvotes: 3

Related Questions