Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Track Wistia videos using Vertster
/*
* Tracks a video's play progress via Vertster
*
* Usage:
*
* Vswap.trackWistiaVideo(560, 'wistia_123', 63.0);
*
* Note: This currently only works with a Wistia video embed but could be easily adapted to
* work with YouTube or other video players.
*/
Vswap.trackWistiaVideo = function(vertsterId, playerId, videoDuration) {
// wistia player states
var PLAYING = 1;
var ENDED = 0;
// keep track of what events have been sent to vertster (we should only send each one once)
var events = {
play: { triggered: false, conditions: function(s,t){ return(s == PLAYING) } },
'video-progress-25': { triggered: false, conditions: function(s,t){ return(t/videoDuration >= 0.25) } },
'video-progress-50': { triggered: false, conditions: function(s,t){ return(t/videoDuration >= 0.5) } },
'video-progress-75': { triggered: false, conditions: function(s,t){ return(t/videoDuration >= 0.75) } },
'video-progress-100': { triggered: false, conditions: function(s,t){ return(s == ENDED) } }
};
// poll the player and determine what events to send
var pollPlayer = function() {
var video = document[playerId];
try {
var state = video.getCurrentState();
var time = video.getCurrentTime();
} catch(e) {
return;
}
// check each event to see if it's been triggered
for (var event in events) {
if (!events[event].triggered && events[event].conditions(state, time)) {
// shoot the info to the vertster hivemind
events[event].triggered = true;
Vswap.track.conversion(vertsterId + '-' + event, 0);
}
}
};
// poll the player once a second
setInterval(pollPlayer, 1000);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment