Thursday, January 19, 2012

[YouTube-API] GA Tracking Suddenly Not working

I'm trying to track the number of play clicks on an iframe embedded youtube video. This was going fine, until I tried to add a new video and discovered that it wasn't working at all... for any videos. Did something in the API change? Here's the code:

var player;
 
var hasTracked = false;
 
var vframe = $('#video-iframe');


 
if(typeof(YT) != 'undefined') {
 initiatePlayer
();
 
}
 
else {
 
var tag = document.createElement('script');
 tag
.src = "https://www.youtube.com/player_api";
 
var firstScriptTag = document.getElementsByTagName('script')[0];
 firstScriptTag
.parentNode.insertBefore(tag, firstScriptTag);
 
}


 
function onYouTubePlayerAPIReady() {
 initiatePlayer
();
 
}


 
function initiatePlayer() {
 console
.log('player initiating')
 player
= new YT.Player('video-iframe', {
 videoId
: vframe.attr('videoid'),
 events
: {
 
'onStateChange': onPlayerStateChange
 
}
 
});
 player
.addEventListener('onStateChange', 'onPlayerStateChange');
 
}


 
function onPlayerStateChange(event) {
 console
.log('it worked!')
 
if(event.data == YT.PlayerState.PLAYING && !hasTracked) {
 hasTracked
= true;
 
}
 
}

And the iframe looks like this:

<iframe width="295" height="230" id="video-iframe" videoid="u8MM2qq1GIV" trackinfo="click_goingback_play_3" src="https://www.youtube.com/embed/u8MM2qq1GIV?rel=0&amp;enablejsapi=1" frameborder="0"></iframe>

The video plays, there are no visible errors, I get the 'player initiating' message, but I never get the 'it worked!' message when I hit play. I swear this used to work! Does anybody have any idea what's going on? Did the Youtube API change? It doesn't work in any browsers.

--
You received this message because you are subscribed to the Google Groups "YouTube APIs Developer Forum" group.
To view this discussion on the web visit https://groups.google.com/d/msg/youtube-api-gdata/-/X5xB6DGzTEQJ.
To post to this group, send email to youtube-api-gdata@googlegroups.com.
To unsubscribe from this group, send email to youtube-api-gdata+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/youtube-api-gdata?hl=en.

No comments:

Post a Comment