Skip to content

Instantly share code, notes, and snippets.

@foliovision
Last active October 12, 2022 12:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save foliovision/2c266598333ecbd1f151 to your computer and use it in GitHub Desktop.
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, '');
// We assume the player is in a DIV with class having post-{number} at the start, in the middle of it or page-id-{number} on body tag
var post_id = root.closest('[class^=post-], [class*=\\ post-], [class*=\\ page-id-]').attr('class').match(/(?:post|page-id)-(\d+)/)[1];
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_player.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