Shark7
Shark7

Reputation: 569

Google Analytics - tracking an javascript redirect outbound link

With Google analytics I'd like to be able to track outbound links, but those outbound links are provided a third party API that uses javascript to redirect the user when the link is clicked. The links look like this:

<a href='#/' onclick="redirect(a00033908, c001406055, b518047627)">anchor text</a>

I don't think the standard GA event tracking code for eventCategory: 'Outbound Link' is going work because the href='#/' will not be considered an outbound link.

I'm really stumped by this and looking for ideas.
If you've managed to get GA to track javascript redirects, please share some info on how you got it to work.

Thanks in advance.

Upvotes: 0

Views: 614

Answers (1)

Squiggs.
Squiggs.

Reputation: 4474

use a function to first call GA, then call your redirect. something like this, note the hitcallback provided in GA already.

function doLogThenRedirect( url )
{
   ga('send', 'event', 'outbound', 'click', url, {
     'transport': 'beacon',
     'hitCallback': function(){
       redirect(a00033908, c001406055, b518047627);
      }
   });

}

<a href='#/' onclick="doLogThenRedirect(window.location.href)">anchor text</a>

Upvotes: 0

Related Questions