I'm confused as to why putting my YouTube tracking code into an IIFE stops it working.
My understanding was that IIFEs run instantly, so why is there a difference between including a script with just bare js vs an IFEE?
Here's my plain JS, which works correctlly:
if ( 0 < ga_options.youtube.length ) {
var tag = document.createElement( 'script' );
tag.src = '//www.youtube.com/iframe_api';
var firstScriptTag = document.getElementsByTagName( 'script' )[0];
firstScriptTag.parentNode.insertBefore( tag, firstScriptTag );
function onYouTubeIframeAPIReady() {
scroll_events.register_youtube_videos();
}
}
However, when I wrap it in an IIFE, like below, it no longer works (no tracking events are fired). I am trying to use an IIFE as part of a restructuring of existing code into self-contained units.
Please could someone explain what I am doing wrong? I have considered scope and tried using var tag
and var firstScriptTag
outside of the IIFE, but still no success.
gaEventsVideoTracking = (function(){
window.console.log( "why no youtube tracking?" );
if ( 0 < ga_options.youtube.length ) {
tag = document.createElement( 'script' );
tag.src = '//www.youtube.com/iframe_api';
firstScriptTag = document.getElementsByTagName( 'script' )[0];
firstScriptTag.parentNode.insertBefore( tag, firstScriptTag );
function onYouTubeIframeAPIReady() {
scroll_events.register_youtube_videos();
}
}
})();