googleanalytics / autotrack

Automatic and enhanced Google Analytics tracking for common user interactions on the web.
Other
4.93k stars 563 forks source link

Replace eventLabel in outboundLinkTracker #192

Closed Levy-Ioel closed 7 years ago

Levy-Ioel commented 7 years ago

Hi Guys,

I want to replace an 'eventLabel' in order to customize outboundLinkTracker. So I used fieldsObj option and I replaced link.href with name assuming <a name=""> attribute. Is my code correct?

ga('require', 'outboundLinkTracker', {
   events: ['click', 'auxclick', 'contextmenu'],
   fieldsObj: {
     hitType: 'event',
     eventCategory: 'Outbound Link',
     eventAction: 'event.type',
     eventLabel: 'name'
   }
});
philipwalton commented 7 years ago

Is my code correct?

No, in this case your event label needs to be dynamically determined from the <a> element itself, so you can't use fieldsObj (since it will always use the same value).

Instead, you should use the hitFilter option like this:

ga('require', 'outboundLinkTracker', {
   events: ['click', 'auxclick', 'contextmenu'],
   hitFilter: function(model, element) {
     model.set('eventLabel', element.getAttribute('name'), true);
   },
});
Levy-Ioel commented 7 years ago

Thank you so much for your help!