Skip to content

Instantly share code, notes, and snippets.

@mitchdaniels
Forked from averyvery/gtm-macros.js
Created January 27, 2014 16:16
Show Gist options
  • Save mitchdaniels/8651603 to your computer and use it in GitHub Desktop.
Save mitchdaniels/8651603 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"
//
// 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