-
-
Save mitchdaniels/8651603 to your computer and use it in GitHub Desktop.
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
// Macro: "data-track-gtm - get from element" | |
// Returns: Boolean - does this element, or one of its parents, have a data-track-gtm attribute? | |
function() { | |
var isSet = function(val) { | |
return val !== null && val !== ''; | |
}; | |
var el = {{element}}; | |
var val = el.getAttribute('data-track-gtm'); | |
while (el && el !== document.body && !isSet(val)) { | |
el = el.parentElement; | |
val = el.getAttribute('data-track-gtm'); | |
} | |
return isSet(val) ? val : false; | |
} | |
// Macro: "has data-track-gtm attribute" | |
// Returns: Boolean - does this element, or one of its parents, have a data-track-gtm attribute? | |
function() { | |
return !!{{data-track-gtm - get from element}}; | |
} | |
//Macro: "data-track-gtm - event action" | |
// | |
// replace the attribute with the appropriate data attribute | |
// if the attribute is divided with something other than commas, change tracking.split | |
// change the number to 0, 1, or 2, depending on whether you want the category, action, or label | |
function() { | |
tracking = {{data-track-gtm - get from element}}; | |
tracking = tracking.split(','); | |
return tracking[1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment