Reputation: 1956
I'm using Google analytics on a website which is being tracked by multiple accounts. Since some of the trackers are added dynamically as part of the response from the web server, there is no easy way for me to know which page is tracked by which accounts. I.e. I don't have the names of the trackers.
I want to find an easy way to send events to all trackers, without knowing which trackers are currently active for a specific page.
I found that all trackers can be listen with the var ga.getAll()
method, as found in an example on https://developers.google.com/analytics/devguides/collection/analyticsjs/method-reference#blownaway
ga(function() {
var trackers = ga.getAll();
for (var i=0; i<trackers.length; ++i) {
var tracker = trackers[i];
tracker.send('pageview');
}
});
Instead of doing ga('myTracker.send', 'pageview')
I could use the code above to send to all trackers. But I want to be able to do something in the terms of ga('allTrackers.send', 'pageview')
to not have to duplicate the code above everywhere I want to send commands. Another option being to create custom methods for each type of call, but I do many different calls with different parameters, so that doesn't seem like a good way either. I'm sure that there must be some way to do it which is more scalable and less repetitive than the ones above.
I want to add this behaviour for allTrackers.send
, allTrackers.ecommerce:addTransaction
, allTrackers.ecommerce:addItem
and allTrackers.ecommerce:send
. (Ecommerce refers to https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce)
Cheers!
EDIT:
I just noticed that the ecommerce part doesn't work with MisterPhilip's solution below, the code is returning "require does not exist on the ga tracker #0", "ecommerce:addTransaction does not exist on the ga tracker #0", "ecommerce:addItem does not exist on the ga tracker", "ecommerce:send does not exist on the ga tracker #0".
These methods are a bit different than the others, since they used to be called by myTracker.ecommerce:send, instead of just myTracker.send I guess the method needs to be updated to handle the "ecommerce:" but I don't know how.
Here's the code I'm trying to call which returns the errors:
allGa('require', 'ecommerce', 'ecommerce.js');
allGa('ecommerce:addTransaction', {
'id': '1234',
'affiliation': 'myCompany',
'revenue': '27.55',
'shipping': '0',
'tax': '0',
'currency': 'USD'
});
allGa('ecommerce:addItem', {
'id': '1234',
'name': 'MyProduct (Blue - XL)',
'sku': '',
'category': 'myCompany',
'price': '25.99',
'quantity': '1',
'currency': 'USD'
});
allGa('ecommerce:send');
allGa('send', {
'hitType': 'event',
'eventCategory': 'checkout',
'eventAction': 'view',
'eventLabel': 'completed'
});
allGa('send', 'pageview', '/checkout/complete');
Upvotes: 1
Views: 316
Reputation: 2452
The code you provided is along the right track. I've made a basic function that can simply replace ga( ... )
with allGa( ... )
. This allows you to have a drop-in replacement, thanks to using .apply()
function allGa()
{
var args = [].slice.call(arguments),
method = args[0];
ga = window.ga;
if(method.indexOf('.') > 0)
{
// We only want to pass to 1 tracker
ga.apply(ga, arguments);
}
else
{
// Grab all of the trackers
ga(function()
{
var trackers = ga.getAll();
for (var i=0; i<trackers.length; ++i)
{
try {
var name = trackers[i].get('name'),
argsLocal = args.slice();
// Change the tracker name to the prefixed version
argsLocal[0] = name + '.' + argsLocal[0];
// Call the tracking call for this tracker
ga.apply(ga, argsLocal);
}
catch(e)
{
console.error(e.msg);
}
}
});
}
}
Sending a pageview to all trackers:
allGa('send', 'pageview');
Sending an event to all trackers:
allGa('send', 'event', 'category', 'action', 'label');
Setting a dimension:
allGa('set', 'dimension1', 'testing');
Sending a pageview to a single tracker
allGa('foobar.send', 'pageview');
Upvotes: 2