3

According to google's documentation for the new analytics.js - you can set up multiple trackers, and send events to them by explicitly mentioning the trackers by name in separate send calls:

https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#multipletrackers

ga('create', 'UA-XXXX-Y');
ga('create', 'UA-12345-6', {'name': 'newTracker'});  // New tracker.
ga('send', 'pageview');
ga('newTracker.send', 'pageview'); // Send page view for new tracker.'

I'm wondering if there's a way to send one event to both trackers without having to write two separate send calls for every event, or even if there is a way to send using one send call and a comma delineated list of trackers or something. Anybody know?

mheavers
  • 29,530
  • 58
  • 194
  • 315

3 Answers3

2

Based on the Asynchronous Synchronization docs & example code, you might be able to do something like:

function multiSend() {
  var trackers = ga.getAll();
  for (var i = 0; i < trackers.length; i++) {
     tracker=trackers[i].get('name')+'.send';
     argum = Array.prototype.slice.call(arguments);
     argum = argum.toString();
     ga(tracker,argum);
  }
}

ga(multiSend('pageview')); // send a pageview to all defined trackers...
Greg F
  • 25
  • 6
mike
  • 7,137
  • 2
  • 23
  • 27
0

Create a function with three parameters (you could include the additional two optional parameters for events, too, but for this example I only used category, action, and label.)

On your link, button, etc., use onclick to call the function and pass your parameters for the event.

<head>
    <script>
        function evtTrack(evtCat,evtAction,evtLabel) {

            ga('send', 'event', evtCat, evtAction, evtLabel);
            ga('newTracker.send', 'event', evtCat, evtAction, evtLabel);
}
    </script>
</head>

//Example
<body>
    <a href="blah.com" onclick="evtTrack('Product Category','Image Click', 'Shoes');">Example</a>
</body>
Blexy
  • 9,573
  • 6
  • 42
  • 55
0

Based on Mike's answer and it works with GA Universal.

function multiSend() {
  var trackers = ga.getAll();
  for (var i = 0; i < trackers.length; i++) {
    tracker=trackers[i].get('name')+'.send';
    argum = Array.prototype.slice.call(arguments); 
    argum.unshift(tracker);
    ga.apply(window, argum);
  }
}
Felipe Cruz
  • 940
  • 5
  • 14