1

We have an affiliate websites and we are attempting to track the clicks to outbound affiliates sites. I inserted the below code into our html.

In header:

<script>
/**
* Function that tracks a click on an outbound link in Analytics.
* This function takes a valid URL string as an argument, and uses that URL string
* as the event label. Setting the transport method to 'beacon' lets the hit be sent
* using 'navigator.sendBeacon' in browser that support it.
*/
var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {
     'transport': 'beacon',
     'hitCallback': function(){document.location = url;}
   });
}
</script>

In the body on the link:

<a href="http://domain.com?aff_link=123456" onclick="trackOutboundLink('http://domain.com'); return false;"> The link </a>

This script was correctly tracking affiliate link clicks but managed to strip the affiliate code from the domain when the user clicked. In other words

http://domain.com?aff_link=123456

became

http://domain.com

As a result no commissions were tracked. Does anyone know possibly why this is happening and how to track the links with GA and keep the affiliate links in tact?

Thanks!

jdubtd10
  • 105
  • 1
  • 2
  • 9

2 Answers2

0

So the problem is that you pass a url without affiliate information in onclick="trackOutboundLink('http://domain.com') ...

This, instead, should work:

onclick="trackOutboundLink(this.href); return false;"

This should use the value of the link's "href" attribute.

This being said, I would strongly suggest using native DOM events instead of the html "onclick" attribute.

For instance, using jQuery:

$("a").click(function(event) {
    event.preventDefault();
    trackOutboundLink(this.href);
});

UPDATE

The main benefit of the event listener approach (second option) is that there can be multiple event listeners on the same DOM node, for the same event. This SO answer explains it very well, in great details.

Hope this helps!

Community
  • 1
  • 1
nibnut
  • 3,072
  • 2
  • 15
  • 17
0

So you want to track the full url instead of the root path. Please try to use this one.

var trackOutboundLink = function(event) {
   event.preventDefault();
   var url = event.target.href;
   ga('send', 'event', 'outbound', 'click', url, {
      'transport': 'beacon',
      'hitCallback': function(){document.location = url;}
   });
}

I hope it would help you.

Perfect
  • 1,616
  • 1
  • 19
  • 27