Skip to content

Instantly share code, notes, and snippets.

@thegreekjester
Last active March 14, 2019 17:18
Show Gist options
  • Save thegreekjester/0ab94869a93e2489d02975a83f62e6c1 to your computer and use it in GitHub Desktop.
Save thegreekjester/0ab94869a93e2489d02975a83f62e6c1 to your computer and use it in GitHub Desktop.
Iframe Optimizely Click Tracker
//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