Skip to content

Instantly share code, notes, and snippets.

@thomaspuppe
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaspuppe/c940d143a3eb748c6c07 to your computer and use it in GitHub Desktop.
Save thomaspuppe/c940d143a3eb748c6c07 to your computer and use it in GitHub Desktop.
(function( vjs ) {
var webtrekkPlugin = function( options ) {
var player = this,
sendEventToWebtrekk = function( trekkString ) {
var messageData = {
'sender': 'videojsWebtrekk',
'message': trekkString
};
window.parent.postMessage( JSON.stringify( messageData ), '*' );
};
// the play event is triggered on several occasions:
// - two(!) times on start (with currentTime undefined and currentTime 0)
// - on resume (after pause).
// - on jumping to another position in the video
// That's why we ose one() for registering the event. (Another solution
// would be to check for that one event which has currentTime===0.)
player.one( 'play', function( e ) {
sendEventToWebtrekk( 'play' );
});
// contentplayback is triggered when the real video content begins.
// It does not matter if an ad was played before or not.
// But: it is triggered again at the end of a video. Hence, "one()".
player.one( 'contentplayback', function( e ) {
sendEventToWebtrekk( 'start' );
});
// one() just to be sure (see above)
player.one( 'ended', function( e ) {
sendEventToWebtrekk( 'complete' );
});
};
vjs.plugin( 'webtrekk', webtrekkPlugin );
}( window.videojs ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment