Created
March 28, 2010 10:53
-
-
Save tomedme/346699 to your computer and use it in GitHub Desktop.
YouTube Tracking
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 youtube_width = 425; | |
var youtube_height = 345; | |
var youtubevideos = {}; | |
var youtubevideologgers = {}; | |
function onYouTubePlayerReady(playerId) { | |
youtubevideologgers[playerId] = function (state) { | |
if (0 == state) { | |
try { | |
pageTracker._trackEvent('Video', 'watched', youtubevideos[playerId].name); | |
} catch(err) {} | |
} | |
} | |
var ytplayer = document.getElementById(playerId); | |
ytplayer.addEventListener('onStateChange', 'youtubevideologgers.'+ playerId); | |
} | |
var pageTracker = _gat._getTracker('UA-xxxxxx-xx'); // global + init here | |
$(document).ready(function () { | |
// google analytics | |
try { | |
pageTracker._trackPageview(); | |
} catch(err) {} | |
// embed youtube videos automagidynamically | |
$('a[href*="youtube"]').each(function(ii, el) { | |
var video = /v=(.{11})/.exec($(el).attr('href')); | |
var video_id = video[1]; | |
var embed_div_id = video[1].replace(/[^A-Za-z0-9]/g, ''); | |
$(el).wrap('<div id="'+ embed_div_id +'" />'); | |
youtubevideos['ytp'+ video_id] = { name: $(el).html() }; // video name (for stats) | |
swfobject.embedSWF('http://www.youtube.com/v/'+ video_id +'?enablejsapi=1&playerapiid=ytp'+ embed_div_id, | |
embed_div_id, | |
youtube_width, | |
youtube_height, | |
'8', null, null, | |
{ allowScriptAccess: 'always' }, // required! | |
{ id: 'ytp'+ embed_div_id } | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment