Skip to content

Instantly share code, notes, and snippets.

@rafaelperozin
Last active September 30, 2019 10:49
Show Gist options
  • Save rafaelperozin/928866cdbd2439be6683396eafa82a12 to your computer and use it in GitHub Desktop.
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
// 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