0

I have a simple line of code as below:

<a href="http://sub.domain.org" onmousedown="trackOutboundLink('http://sub.domain.org'); return false;" >

With JavaScript:

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

I want to to track the clicks on the link, but I have onmousedown for the tracking for people who right click and open in new tab (I realize it won't be perfectly accurate, but that's ok).

The thing is, when you right click on the link, the page loads to the link now. I have removed the onmousedown command and the problem goes away, so I know it's that.

I though the return false is meant to prevent this? Thank you in advance for any help

adiga
  • 34,372
  • 9
  • 61
  • 83
Gerhard
  • 3
  • 3
  • 3
    Unanswerable without knowing what `trackOutboundLink` does. [mcve] – baao Jan 02 '19 at 09:20
  • Remove `'hitCallback': function(){document.location = url;}` – baao Jan 02 '19 at 09:22
  • Updated - I didn't think it should matter though – Gerhard Jan 02 '19 at 09:22
  • Don't I need hitCallback to define 'url'? When I remove it, the code breaks on testing – Gerhard Jan 02 '19 at 09:26
  • Yeah, you'll need to check if it was the right or left button and only set the location on left – baao Jan 02 '19 at 09:27
  • Here's how to check https://stackoverflow.com/questions/3944122/detect-left-mouse-button-press – baao Jan 02 '19 at 09:28
  • Than simply pass the `event` to your function `onmousedown="trackOutboundLink('http://sub.domain.org', event);`, check with an if and only update the location inside the if – baao Jan 02 '19 at 09:29
  • Sorry bambam, I should have first experimented a bit more first - the code below in the Answers work for me. Thank you. – Gerhard Jan 02 '19 at 09:33

1 Answers1

0

Solution:

Change JavaScript to:

var trackOutboundLink = function(url) {
   ga('send', 'event', 'referral', 'click', url, {
     'transport': 'beacon',
     function(){document.location = url;}
   });
}
Gerhard
  • 3
  • 3