Created
July 25, 2012 20:42
-
-
Save jshaw/3178554 to your computer and use it in GitHub Desktop.
Player Event Listeners and Analytics for Vimeo
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
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