Hi JD,
I've had to revisit the Digital Pi script. For some reason when I add Google Tag Manager code to the template, its breaking the video. Nothing will display until I remove the GTM code, specifically the JavaScript for it. I see no JS error being triggered when I inspect the page in Chrome. I was wondering if you or anybody else has had this issue.
Thanks!
Please supply your URL.
Here is the page in question, video template . It might actually show the video the first time you visit but if you refresh, it wont show it again.
Thanks for any insight you might be able to give!
Well, you have two separate problems here:
See, the YT IFRAME API has a very primitive event model.
Rather than using a standard event stack like
// could be in remote file1.js
window.addEventListener("youTubeIframeAPIReady", function doSomething(){
// do something
})
// could be in another remote file2.js
window.addEventListener("youTubeIframeAPIReady", function doSomethingElse(){
// do something else (after doing something)
})
instead it supports only a single function to kick off event handling:
function onYouTubeIframeAPIReady {
// do all things
}
Therefore every time a script contains a global onYouTubeIframeAPIReady function, it overwrites any previous event listeners.
When you have scripts loading in a non-deterministic order (as happens w/GTM unless you use Tag Sequencing -- a good thing to learn) then this means you don't know which event listener will be created last, so you don't know which one will be the active one. But even if you can make sure the scripts load in order 1-2-3 that still means only 3's copy of onYouTubeIframeAPIReady will run.
This is all actually quite solvable -- you have to use a wrapper event stack on top of onYouTubeIframeAPIReady and make sure your scripts all cooperate by adding to that stack. But it won't be solved unless you change your scripts accordingly, and right now they do the overwrite thing.
Thank you Sanford! I will see if I can implement your ideas here.