Created
July 26, 2012 15:30
-
-
Save jshaw/3182727 to your computer and use it in GitHub Desktop.
YouTube Video Playing and Finished Event Handling
This file contains 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
var tag = document.createElement('script'); | |
tag.src = "//www.youtube.com/iframe_api"; | |
var firstScriptTag = document.getElementsByTagName('script')[0]; | |
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); | |
var player; | |
function onYouTubeIframeAPIReady() { | |
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