Bounteous-Inc / youtube-google-analytics

YouTube Google Analytics Embed Tracking
MIT License
232 stars 89 forks source link

getVideoData is not a function error #36

Closed DanWilkerson closed 8 years ago

DanWilkerson commented 8 years ago

When the script binds to an element that already is an YT player, it breaks. We can fix this by checking to see if YT.get(elementId || 'widget' + n), where n is the count of YouTube iframes we're on. If this is the case, we might be able to attach an event listener.

rmeekers commented 7 years ago

Dan, I still have this issue in a specific situation (this page). What exactly was the fix? The commit only contains an updated comment.

DanWilkerson commented 7 years ago

@rmeekers Should've tagged this: https://github.com/lunametrics/youtube-google-analytics/commit/5162442db198e785e2c9634dba7f5ac27f551ee1#diff-2b793c9228aa48c6a2b22a75d366918bL151

Basically the issue was the code was calling new YT.Player() on all elements that might be YouTube videos, but sometimes YT.Player's had already been created with the element by other code (like yours). Now the script uses YT.get() first to see if a Player has already been initialized on the given DOM element, and uses the existing Player if so.

Try with the latest version and see if it solves your issues.

rmeekers commented 7 years ago

@DanWilkerson as far as I can see, the binding goes fine. The iframe gets detected and then this code is executed:

            if (typeof youTubeIframe.pauseFlag === 'undefined') {
              console.log('addYouTubeEvents_2');
                youTubeIframe.pauseFlag = false;
                player.addEventListener('onStateChange', function(evt) {
                    onStateChangeHandler(evt, youTubeIframe);
                });
            }

I've found some info about the getVideoData() function. It's an unsupported function and it seems that Google doesn't provide it in some cases. I'm not sure why. A Google YouTube developer said that we should not use that function because it's not supported and thus not reliable.

We should use the YouTube API instead by calling http://gdata.youtube.com/feeds/api/videos/$targetVideoId?v=2&alt=jsonc. Suggestion how to implement this?

DanWilkerson commented 7 years ago

@rmeekers That API was deprecated a little while ago and can no longer be used to retrieve title information; we actually switched to using .getVideoURL() a few versions back because of this. Update to the latest version and you'll be all set.

https://github.com/lunametrics/youtube-google-analytics/issues/34

rmeekers commented 7 years ago

@DanWilkerson I still get this error for some videos. Even with the current version of the script. I tried debugging it but can't find out what's going wrong. Any suggestions where I can look?

e.getVideoUrl is not a function (script line 285 https://github.com/lunametrics/youtube-google-analytics/blob/master/src/lunametrics-youtube.gtm.js#L285)

DanWilkerson commented 7 years ago

If you share a page where I can reproduce, I'd be happy to take a look!

rmeekers commented 7 years ago

I've sent it by email (contact form on your website) few days ago.

DanWilkerson commented 7 years ago

I can't seem to see the page any longer, but I did take a look and my guess is that some code on the page is messing with the functionality of the plugin. I'd try having just the video and the YouTube tracking script, then adding back in other stuff until it breaks again.