Skip to content

Instantly share code, notes, and snippets.

@bohman
Last active April 6, 2020 16:10
Show Gist options
  • Save bohman/9bffcc903e92a2b83dd3 to your computer and use it in GitHub Desktop.
Save bohman/9bffcc903e92a2b83dd3 to your computer and use it in GitHub Desktop.
Quick and dirty way to collect GA-events in one file. No, doesn't need jQuery. I'm just lazy at the moment.
(function ($) {
window.sw_ga = {};
//
// Events
//
sw_ga.events = [
// Menu toggle
{
selector: '.menu-toggle',
event: 'click',
callback: function() { ga('send', 'event', { eventCategory: 'Menu', eventAction: 'Menu toggle button' }); }
},
// Newsletter focus
{
selector: '.newsletter input[type="text"]',
event: 'focus',
callback: function() { ga('send', 'event', { eventCategory: 'Newsletter', eventAction: 'Field focus' }); }
},
// Newsletter submit
{
selector: '.newsletter form',
event: 'submit',
callback: function() { ga('send', 'event', { eventCategory: 'Newsletter', eventAction: 'Form submit' }); }
},
];
//
// Setup
//
sw_ga.setup = {
// Init events
init_events: function() {
for (i = 0; i < sw_ga.events.length; i++) {
var event = sw_ga.events[i];
$(event.selector).on(event.event, event.callback);
}
}
}
// Init for realz
jQuery(document).ready(function() {
for (var fn in sw_ga.setup) {
if (sw_ga.setup.hasOwnProperty(fn)) {
sw_ga.setup[fn]();
}
}
});
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment