videojs-event-tracking
Version:
Track events with VideoJS and keep an eye on performance metrics
7 lines • 3.93 kB
JavaScript
/**
* videojs-event-tracking
* @version 1.0.3
* @copyright 2022 spodlecki <s.podlecki@gmail.com>
* @license MIT
*/
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("video.js")):"function"==typeof define&&define.amd?define(["video.js"],t):n.videojsEventTracking=t(n.videojs)}(this,function(n){"use strict";n="default"in n?n.default:n;function t(n){function t(n){o&&clearTimeout(o),i=0,r=!1}var e=this,i=0,o=null,r=!1;e.on("dispose",t),e.on("loadstart",t),e.on("ended",t),e.on("pause",function(){"function"==typeof e.seeking&&e.seeking()||"function"==typeof e.scrubbing&&e.scrubbing()||r||(o=setTimeout(function(){i++,e.trigger("tracking:pause",{pauseCount:i})},300))})}function e(n){function t(n){u=r=o=!1,a=null,c=s=0}function e(){var n;0<(a=+i.duration().toFixed(0))&&(n=(a/4).toFixed(0),r=2*(o=+n),u=3*+n)}var i=this,o=!1,r=!1,u=!1,a=null,s=0,c=0;i.on("dispose",t),i.on("loadstart",t),i.on("tracking:pause",function(){return s++}),i.on("tracking:seek",function(){return c++}),i.on("timeupdate",function(){null===a&&e();var n=+i.currentTime().toFixed(0),t={seekCount:c,pauseCount:s,currentTime:n,duration:a};switch(n){case o:o=!1,i.trigger("tracking:first-quarter",t);break;case r:r=!1,i.trigger("tracking:second-quarter",t);break;case u:u=!1,i.trigger("tracking:third-quarter",t)}}),i.on("ended",function(){var n={seekCount:c,pauseCount:s,currentTime:a,duration:a};i.trigger("tracking:fourth-quarter",n)}),i.on("durationchange",e)}function i(n){t.apply(this,arguments),function(n){function t(){i&&clearTimeout(i),a=0,s=u=r=o=!1}var e=this,i=null,o=!1,r=!1,u=!1,a=0,s=!1;this.on("dispose",t),this.on("loadstart",t),this.on("ended",t),this.on("pause",function(){u=!1,!e.scrubbing()||n.bufferingConfig&&n.bufferingConfig.includeScrub||(o=!0,i=setTimeout(function(){o=!1},200))}),this.on("waiting",function(){!1===u&&!1===o&&0<e.currentTime()&&(u=new Date,r=+e.currentTime().toFixed(0),s=+e.readyState())}),this.on("timeupdate",function(){var n,t=+e.currentTime().toFixed(0);u&&t!==r&&(n=(new Date-u)/1e3,r=u=!1,a++,e.trigger("tracking:buffered",{currentTime:t,readyState:+s,secondsToLoad:+n.toFixed(3),bufferCount:+a}))})}.apply(this,arguments),e.apply(this,arguments),function(n){var t=this,e=!1,i=0,o=0,r=0,u=function(){e=!1,r=o=i=0};this.on("ended",u),this.on("dispose",u),this.on("loadstart",function(){u(),i=new Date}),this.on("loadeddata",function(){o=new Date,r=(o-i)/1e3}),this.on("playing",function(){e||(e=!0,t.trigger("tracking:firstplay",{secondsToLoad:+r.toFixed(3)}))})}.apply(this,arguments),function(n){function t(){i=0}var e=this,i=0;e.on("dispose",t),e.on("loadstart",t),e.on("ended",t),e.on("seeking",function(){var n=+e.currentTime().toFixed(0);i++,e.trigger("tracking:seek",{seekCount:+i,seekTo:n})})}.apply(this,arguments),function(t){var e,i,o,r,u,a,s,c,d,n,f,g;void 0!==t&&"function"==typeof t.performance&&(e=this,c=s=a=u=r=o=i=0,d=[],n=function(){c=s=a=u=r=o=i=0,d=[]},f=function(){var n={pauseCount:o,seekCount:i,bufferCount:r,totalDuration:u,watchedDuration:a,bufferDuration:s,initialLoadTime:c};t.performance.call(e,n)},g=function(){f(),n()},"function"==typeof window.addEventListener&&(window.addEventListener("beforeunload",g),e.on("dispose",function(){window.removeEventListener("beforeunload",g)})),e.on("loadstart",function(){0<u&&f(),n()}),e.on("ended",g),e.on("dispose",g),e.on("timeupdate",function(){var n=+e.currentTime().toFixed(0);d.indexOf(n)<0&&d.push(n),a=d.length}),e.on("loadeddata",function(n,t){u=+e.duration().toFixed(0)}),e.on("tracking:seek",function(n,t){i=t.seekCount}),e.on("tracking:pause",function(n,t){o=t.pauseCount}),e.on("tracking:buffered",function(n,t){r=t.bufferCount,s=+(s+t.secondsToLoad).toFixed(3)}),e.on("tracking:firstplay",function(n,t){c=t.secondsToLoad}))}.apply(this,arguments)}var o=n.registerPlugin||n.plugin;return void 0===(n.getPlugin||n.plugin)("eventTracking")&&o("eventTracking",i),i.VERSION="1.0.3",i});