Skip to content

Instantly share code, notes, and snippets.

@jshaw
Created July 26, 2012 15:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jshaw/3182727 to your computer and use it in GitHub Desktop.
Save jshaw/3182727 to your computer and use it in GitHub Desktop.
YouTube Video Playing and Finished Event Handling
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