Skip to content

Instantly share code, notes, and snippets.

@jshaw
Created July 25, 2012 20:42
Embed
What would you like to do?
Player Event Listeners and Analytics for Vimeo
Highwire.prototype._trackVideoPlays = function(){
// Gets reference to the Vimeo video player
var f = $('#VimeoPlayer'),
url = f.attr('src').split('?')[0];
// Listen for messages from the player
if (window.addEventListener){
window.addEventListener('message', onMessageReceived, false);
} else {
window.attachEvent('onmessage', onMessageReceived, false);
}
// Handle messages received from the player
function onMessageReceived(e) {
if (e.origin == 'http://player.vimeo.com'){
var data = $.parseJSON(e.data);
switch (data.event) {
case 'ready':
onReady();
break;
case 'play':
onPlay();
break;
case 'finish':
onFinish();
break;
}
}
}
// Helper function for sending a message to the player
function post(action, value) {
var data = { method: action };
if (value) {
data.value = value;
}
f[0].contentWindow.postMessage(JSON.stringify(data), url);
}
// Adds listener to the play and video finish events.
function onReady() {
post('addEventListener', 'play');
post('addEventListener', 'finish');
}
function onPlay() {
_gaq.push(['_trackEvent', 'Video', 'Play']);
}
function onFinish() {
_gaq.push(['_trackEvent', 'Video', 'Finish']);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment