Friday, 3 January 2014

How to Track Button Clicks With Google Analytics if the click does not take to a new Page?

Sometimes, a button on a page does not take a visitor to a new page. So, the best way to track this click is through a custom variable, a JavaScript code you put on this button code to send Google Analytics the required data to track the visitor behavior whether it was a click, or even a hover. 
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 onclickhandler, 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.

This post was published in Events and Virtual PageviewsJavaScript by Eric Fettman. Bookmark thepermalink.