I want to create a variable in GTM which returns the element which is closest. In a javascript tag it can be done this way:
function() {
return function(target, selector) {
while (!target.matches(selector) && !target.matches('body')) {
target = target.parentElement;
}
return target.matches(selector) ? target : undefined;
}
}
To make our website CSP compliant custom JS tags aren't allowed and we should create gtm variable templates.
The issue with a GTM template is you are not able to pass {{click element}} so I am looking for a method on how to access gtm.click from the datalayer and execute the closest function on that. https://www.simoahava.com/analytics/custom-templates-guide-for-google-tag-manager/#restrictions-to-what-type-of-data-you-can-handle
Is the only way I am able to do this by injecting a script? I would rather fix this by using one of the gtm template api's.