Search code examples
google-analyticsanalyticsuniversal-analytics

Universal Analytics cross domain tracking with multiple trackers


So i have Universal Analytics code with multiple trackers:

<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-xxxxxxxx-x', 'auto');
  ga('create', 'UA-yyyyyyyy-y', 'auto', {'name': 'second'});
  ga('send', 'pageview');
  ga('second.send', 'pageview');
</script>

How do i create Cross Domain tracking since UA-yyyyyyyy-y profile is used to gather info from 2 separete domains. With one tracker it is pretty simple:

<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-yyyyyyyy-y', 'auto', {'allowLinker': true});
  ga('linker:autoLink', ['other-domain.com'] );
  ga('send', 'pageview');
</script>

But what do i do wen i have 2 trackers, how do i set up cross domain tracking for this Universal Analytics profile: UA-yyyyyyyy-y? If you answer my question you will also answer a part of this question:

http://stackoverflow.com/questions/20126897/google-analytics-cross-domain-tracking-with-multiple-trackers

Solution

  • In your example with just a single tracker, you're missing the call to require the linker plugin. Without that it won't work. Perhaps that is the source of your issue.

    Regardless, in analytics.js you can call any method on any tracker by simply prefixing the method name with the tracker name. You'll notice your first example already does this in the line ga('second.send', 'pageview').

    So, to implement auto-linking on both trackers you'd do something like this:

    ga('create', 'UA-yyyyyyyy-y', 'auto', {allowLinker: true});
    ga('require', 'linker');
    ga('linker:autoLink', ['other-domain.com'] );
    ga('send', 'pageview');
    
    ga('create', 'UA-yyyyyyyy-y', 'auto', {name: 'second', allowLinker: true});
    ga('second.require', 'linker');
    ga('second.linker:autoLink', ['other-domain.com'] );
    ga('second.send', 'pageview');
    

    Notice the second. prefix on all the method calls in the second block which corresponds to the name "second" that I gave the tracker.

    For more information on the auto-linker plugin, check out this resource:
    https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain#autolink

    And for more information on the various methods and method signatures, here is the guide:
    https://developers.google.com/analytics/devguides/collection/analyticsjs/method-reference