kmg-rabelo
kmg-rabelo

Reputation: 13

Outbound link tracking not working

I'm trying to track events in Google analytics for outbound links and it doesn't seem to be working. Links that don't take you off the site work fine but not the outbound links. I'm attaching the javascript to the onclick event

onclick="__gaTracker('send', 'event', {
eventCategory: 'Magazine',
eventAction: 'Tutorial Files',
eventLabel: '<?php echo esc_attr($title); ?>', 
transport: 'beacon'
});"

Upvotes: 1

Views: 231

Answers (1)

Philip Walton
Philip Walton

Reputation: 30431

There are a lot of gotchas around proper outbound link tracking, the most common one being that most browsers stop executing JavaScript on the current page once a request has started for the next page.

That means the code in your onclick handler is often not even going to run, which is why it's not working.

The easiest solution I can recommend is to use the analytics.js autotrack plugins, which do outbound link tracking for you.

If you want to code the implementation yourself, you can look at the autotrack outboundLinkTracker source code to see how it handles all of the gotchas.

Upvotes: 2

Related Questions