Tracking button clicks in Google Analytics

This article presents the example of JavaScript-code. The code should be placed in the main file of your current active theme scripts. If you don’t know where to paste JavaScript-code you can use a special plugin.

Don’t forget to connect Google Analytics on your website.

It is recommended to add unique CSS-class to the shortcode you are going to track. You can make, for example, it in the following way:

[su_button class="my-custom-button"] Click Me [/su_button]

Now you can use the following JavaScript code:

jQuery(document).ready(function($) {
// Handle button click
$('body').on('click', '.my-custom-button', function() {
setTimeout(function() {
// Track event in GA
ga('send', {
hitType: 'event',
eventCategory: 'Buttons',
eventAction: 'clicked',
eventLabel: 'Shortcodes Ultimate Campaign'
}, 100);
🤝 Thank you!