Sending click events to Google Analytics in WordPress: the easy way

This is working in my WordPress site. Remember we are using the plugin “Per page head”, which allows you to add content into the section for a specific page, like custom JS or custom HTML. This is the custom code added:

<script>// <![CDATA[
jQuery(document).ready(function($){ 

$('.class-name-of-the-button').click(function() {  

  ga('send', 'event', {
    eventCategory: 'Outbound Link',
    eventAction: 'click',
    eventLabel: event.target.href,
    transport: 'beacon'
  });

});

});
// ]]></script>

Explanation:

Finally, notice I have seen in real time in Google Analytics (on Real time – Events) when I have pushed the button to test it.