Skip to content

Instantly share code, notes, and snippets.

@meteormanaged
Created April 8, 2016 20:24
Show Gist options
  • Save meteormanaged/3ab32aa1ff516689bf6648c9974b2fe4 to your computer and use it in GitHub Desktop.
Save meteormanaged/3ab32aa1ff516689bf6648c9974b2fe4 to your computer and use it in GitHub Desktop.
Simple filtering for Google Tags - ES6
const filterClasses = (ev) => {
// Filter those classes for a class containing with 'gtm-'
const filteredClasses = ev.toElement.className.split(' ').filter((thisClass) => thisClass.indexOf('gtm-' !== -1) ? thisClass : false);
//If it was a match, return the matching classname as a string, otherwise, return false.
if (filteredClasses.length > 0) return filteredClasses[0];
else return false;
};
const gaSend = (className) =>
ga('send', {
hitType: 'event',
eventCategory: 'Links',
eventAction: 'click',
eventLabel: className
});
//When the doc is ready.
$(document).ready(() =>
$(document).on('click', 'body a', (ev) =>
filterClasses(ev) ? gaSend(filterClasses(ev)) : false));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment