Skip to content

Instantly share code, notes, and snippets.

@hozza
Last active August 6, 2020 07:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hozza/9da4d2bc39aa2610df5cf3dcc0656da7 to your computer and use it in GitHub Desktop.
Save hozza/9da4d2bc39aa2610df5cf3dcc0656da7 to your computer and use it in GitHub Desktop.
jQuery Google Analytics Button Event Tracking
// Google Analytics Event Button Tracking
// wait till eveythings ready
jQuery(document).ready(function () {
// use whatever selectors you want to track, e.g. "a.trackMe"
jQuery('.gaTrack, button, a.button, input[type="submit"], input[type="button"], input[type="reset"], .wp-block-button__link, .menu-item a, .menu a, li a').on('click', function() {
// what was clicked?
var name = jQuery(this).text();
// assuming Google Analytics is already loaded, fire event log
ga('send', 'event', 'button', 'click', name);
});
});
@nsswebservices
Copy link

nsswebservices commented Aug 6, 2020

hi,

Just a quick question if using gtag.js should the event be written like this:

gtag('event','click'{'event_category':'button', 'event_label':name});

instead of

ga('send', 'event', 'button', 'click', name);

Thanks,
Rob

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment