-
-
Save deanrad/cbcf9bd2911dae11f3b74750efb18134 to your computer and use it in GitHub Desktop.
YouTube Video Playing and Finished Event Handling
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
let player | |
let onYouTubeIframeAPIReady | |
const youTubeReady = new Promise(resolve => { | |
var tag = document.createElement('script'); | |
tag.src = "//www.youtube.com/iframe_api"; | |
var firstScriptTag = document.getElementsByTagName('script')[0]; | |
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); | |
onYouTubeIframeAPIReady = resolve | |
}) | |
youTubeReady.then(() =>{ | |
player = new YT.Player('player', { | |
height: '254', | |
width: '451', | |
videoId: '{{ current_phase.video_id }}', | |
events: { | |
'onStateChange': onPlayerStateChange | |
} | |
}); | |
}) | |
function onPlayerReady(event) { | |
event.target.playVideo(); | |
} | |
function onPlayerStateChange(event) { | |
if (event.data == YT.PlayerState.PLAYING) { | |
_gaq.push(['_trackEvent', 'Video', 'Play']); | |
}else if ( event.data == YT.PlayerState.ENDED){ | |
_gaq.push(['_trackEvent', 'Video', 'Finish']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment