Tracking YouTube videos in JS format

Extended Google Analytics measuremet.

Javascript format

It is necessary to call a _gaYouTubePlayerReady method after you create a video. The example of created video follows:

<div id="player-id"></div>
<script type="text/javascript">
    var tag = document.createElement('script');
    tag.src = "http://www.youtube.com/player_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    
    var player;
    function onYouTubePlayerAPIReady() {
        player = new YT.Player('player-id', {
            height: '349',
            width: '425',
            videoId: 'sMwQN43fwoQ',
            events: {
                onReady': _gaYouTubePlayerReady
            }
        });
    };
</script>

Try it!

You can try measuring the events of YouTube videos here. Use a plugin GA debug for Chrome to view sent parameters.