Skip to content

Instantly share code, notes, and snippets.

@kimhogeling
Created September 24, 2018 08:59
Show Gist options
  • Save kimhogeling/bdf8cd9fa680139dcaa415b3b3e72a7a to your computer and use it in GitHub Desktop.
Save kimhogeling/bdf8cd9fa680139dcaa415b3b3e72a7a to your computer and use it in GitHub Desktop.
Push shopping24 widget click events to Google Tag Manager's dataLayer
// Click listener for tracking clicks on shopping24 products into the Google Tag Manager's DataLayer
document.addEventListener('click', function (event) {
if (!window.dataLayer) {
return;
}
// recursively traverse from a clicked element to detect if the element lives inside a shopping24 product
var s24product = (function findProductNodeByChildNode(potentialProductNode) {
// This guard exists, because anything can happen in the DOM
if (!potentialProductNode) {
return false;
}
// For privacy and performance
if (potentialProductNode.id && potentialProductNode.id === ('s24widget')) {
return false;
}
// FOUND IT!
if (potentialProductNode.classList && potentialProductNode.classList.contains('s24_product')) {
return potentialProductNode;
}
// Go up..
return findProductNodeByChildNode(potentialProductNode.parentNode);
})(event.srcElement || event.target);
if (!s24product) {
return;
}
dataLayer.push({ 'event': 'shopping24_click' });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment