Last active
September 30, 2019 10:49
-
-
Save rafaelperozin/928866cdbd2439be6683396eafa82a12 to your computer and use it in GitHub Desktop.
Tag Manager Custom JavaScript Trigger and Custom Variable to track a third-party iFrame interaction
This file contains 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
// Add focus on current window | |
window.focus(); | |
// When click on iframe the focus change and we are listen this | |
var listener = window.addEventListener('blur', function() { | |
//console.log('active == iFrame: ', document.activeElement == document.querySelector('iframe[title="Help Scout Beacon"]')); | |
// if the current focus is on your iFrame (you can use any css or js selector here) | |
if (document.activeElement == document.querySelector('iframe')) { | |
//console.log('clicked'); | |
// Add to dataLayer your information | |
window.dataLayer = window.dataLayer || []; | |
window.dataLayer.push({ | |
'chatBtn': true, | |
'event': 'chatBtn' | |
}); | |
} | |
// Remove listener | |
window.removeEventListener('blur', listener); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment