Last active
March 14, 2019 17:18
-
-
Save thegreekjester/0ab94869a93e2489d02975a83f62e6c1 to your computer and use it in GitHub Desktop.
Iframe Optimizely Click Tracker
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
//This tracks clicks to Iframes | |
window.inIframe = false; | |
| |
//Basically the next two event listeners turn this "inIframe" variable from false to true if they are within | |
//the bounds of the iframe by using mouseenter/mouseleave | |
document.querySelector('selector_here').addEventListener('mouseenter', function(){ | |
inIframe = !inIframe | |
}) | |
| |
document.querySelector('selector_here').addEventListener('mouseleave', function(){ | |
inIframe = !inIframe | |
}) | |
| |
//This sets a "blur" event listener to the entire window | |
window.addEventListener('blur', function(){ | |
if(inIframe){ | |
console.log('iframe is clicked!') | |
} | |
}) | |
/////////////// OTHER SOLUTION //////////////////////// | |
//Throw this into shared code | |
window.addEventListener('blur',function(){ | |
if (document.activeElement instanceof HTMLIFrameElement) { | |
//Send custom event for clicking here(can't use regular click tracking) | |
console.log('iframe is clicked!'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment