Skip to content

Instantly share code, notes, and snippets.

@averyvery
Last active January 4, 2016 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save averyvery/8611716 to your computer and use it in GitHub Desktop.
Save averyvery/8611716 to your computer and use it in GitHub Desktop.
// 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"
// Returns: String
// Is only run if "has data-track-gtm attribute" returns true
function() {
val = {{data-track-gtm - get from element}};
return val[1] ? val[1] : false;
}
@mitchdaniels
Copy link

This is awesome. I made a small change to Macro: "data-track-gtm - event action" to return the full parameter value, rather than a single character. The updated Macro is visible here: https://gist.github.com/mitchdaniels/8651603

(Not sure how best to collaborate on this kind of stuff over Gist. Your feedback is welcome.)

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