Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample code for FV Player which detects if the user really watched the video to the end (seeking to the end won't suffice) and then sends the post ID using Ajax.
flowplayer( function(api,root) {
var root = jQuery(root);
api.bind('progress', function(e,api,current) {
fv_track(e,api,current);
}).bind('finish ready ', function(e,api) {
for( var j in fv_ga_events ) {
root.removeData('fv_custom_track_'+fv_ga_events[j]);
}
});
var fv_ga_events = [ 'start', 'first quartile', 'second quartile', 'third quartile', 'complete' ];
function fv_track(e,api,data) {
var video = api.video,
dur = video.duration,
i = 0;
var name = root.attr("title");
if( !name && typeof(video.src) != "undefined" ) name = video.src.split("/").slice(-1)[0].replace(/\.(\w{3,4})(\?.*)?$/i, '');
var post_id = root.parents('[id^=post-]').attr('id').replace(/post-/,'');
if( dur ) {
if( data > 19 * dur/20 ) i = 4;
else if( data > 3 * dur/4 ) i = 3;
else if( data > dur/2 ) i = 2;
else if( data > dur/4 ) i = 1;
}
if( root.data('fv_custom_track_'+fv_ga_events[i]) ) return;
for( var j in fv_ga_events ) { // make sure user triggered the previous quartiles before tracking
if(j == i) break;
if( !root.data('fv_custom_track_'+fv_ga_events[j]) ) return;
}
root.data('fv_custom_track_'+fv_ga_events[i], true);
if( /fv_ga_debug/.test(window.location.href) ) console.log('FV custom tracking: '+e.type+' - '+ "Video "+fv_ga_events[i]+" " + name + " " + post_id + "'")
if( fv_ga_events[i] == 'complete' ) {
jQuery.post(
fv_fp_ajaxurl,
{ 'action' : 'your-ajax-action', 'post_id' : post_id, 'video' : name },
function() {
console.log('Video view tracked for post_id '+post_id);
}
);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.