Created
September 24, 2018 08:59
-
-
Save kimhogeling/bdf8cd9fa680139dcaa415b3b3e72a7a to your computer and use it in GitHub Desktop.
Push shopping24 widget click events to Google Tag Manager's dataLayer
This file contains hidden or 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
// 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