Quantcast
Channel: Google Analytics Tip of the Day
Viewing all articles
Browse latest Browse all 105

Use onmousedown JavaScript Listener for Events if onclick Doesn’t Work

$
0
0
Real-Time Events reportview at full size

Whichever JavaScript listener you use, you can check your event in the Real-Time reports.

If you’re trying to generate a Google Analytics event based on a click and onclick isn’t working, try onmousedown instead.

Let’s say that you want to track a click on the following mailto: link as a Google Analytics event:

<a href="mailto:service@xyz?subject=Demo%20Request">mailto: link</a>.

If you use the JavaScript onclick handler, make sure to test the event in multiple browsers, because if the “mouseup” part of the click is not recognized, onclick never fires:

<a onclick="ga('send', 'event', 'link', 'mailto', this.href);" href="mailto:service@xyz?subject=Demo%20Request">mailto: link</a>.

If onclick is proving unreliable, use onmousedown instead:

<a onmousedown="ga('send', 'event', 'link', 'mailto', this.href);" href="mailto:service@xyz?subject=Demo%20Request">mailto: link</a>.

If you’re using jQuery, you can similarly use the .mousedown() listener instead of .click().

Whichever option you choose to capture your events, you can test your events in the Real-Time reports.

Please share this post:
Facebook Twitter Plusone Linkedin Email

Viewing all articles
Browse latest Browse all 105

Trending Articles