Created
April 8, 2016 20:24
-
-
Save meteormanaged/3ab32aa1ff516689bf6648c9974b2fe4 to your computer and use it in GitHub Desktop.
Simple filtering for Google Tags - ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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