-
Notifications
You must be signed in to change notification settings - Fork 4
/
engagement-timer.min.js
8 lines (8 loc) · 5.08 KB
/
engagement-timer.min.js
1
2
3
4
5
6
7
8
!function(a,b){"use strict";function c(a,b){this._interval=b,this._fn=a,this.tick()}function d(b){if(!b.each&&!b.every)throw new Error("Requires opts.each or opts.every.");if((b.idleAfter||b.engagementEvents)&&!(b.idleAfter&&b.engagementEvents&&b.engagementEvents.length))throw new Error("Configure opts.idleAfter & opts.engagementEvents for idling.");if(!(this instanceof d))return new d(b);if(this._context=b.context||a,"string"==typeof this._context&&(this._context=a.querySelector(b.context)),!this._context)throw new Error("Unable to find context "+b.context);this._max=1e3*b.max||1/0,this._min=1e3*b.min||0,this._every=(b.every||[]).map(function(a){return b.min?b.min%a+a:0}),this._every.initialValues=f(b.every||[]),this._each=f(b.each||[]),this._each.initialValues=this._each.slice(0),this._minInterval=1e3*h(this._every.initialValues.concat(this._each.initialValues)),this._idleAfter=1e3*b.idleAfter+1||null,this._lastTick=b.startTime,this._trackedTime=this._lastTick?+new Date-this._lastTick:0,this._tickElapsed=0,this._running=!1,this._offset=0,this._events={},this._cache={};var c,e=k(function(){this._running||this.start(),this._resetIdleTimeout()}.bind(this),this._idleTimeout/2),g=function(a){this._context.addEventListener(a,e)}.bind(this);b.engagementEvents&&b.engagementEvents.forEach(g),b.idleOnVisibilityChange&&j(function(a){a&&this._running?(this.pause(),c=!0):a||this._running||!c||this.start()}.bind(this)),this._trackedTime&&setTimeout(this._tick.bind(this),0)}function e(a,b){return a>b?1:b>a?-1:0}function f(a){return a.reduce(function(a,b){var c=Number(b);return c&&a.push(c),a},[]).sort(e)}function g(a,b){if(a=Math.abs(a),(b=Math.abs(b))>a)return g(b,a);for(;;){if(0===b)return a;if(0===(a%=b))return b;b%=a}}function h(a){if(1===a.length)return a[0];var b=a.pop();return a.reduce(function(a,b){return g(a,b)},b)}function i(a){return void 0!==a}function j(c){var d,e;i(a.hidden)?(e="hidden",d="visibilitychange"):i(a.mozHidden)?(e="mozHidden",d="mozvisibilitychange"):i(a.msHidden)?(e="msHidden",d="msvisibilitychange"):i(a.webkitHidden)&&(e="webkitHidden",d="webkitvisibilitychange"),d?a.addEventListener(d,function(){c(a[e])}):(b.addEventListener("blur",function(){c(!0)}),b.addEventListener("focus",function(){c(!1)}))}function k(a,b){var c,d,e,f=null,g=0,h=function(){g=new Date,f=null,e=a.apply(c,d)};return function(){var i=new Date;g||(g=i);var j=b-(i-g);return c=this,d=arguments,j<=0?(clearTimeout(f),f=null,g=i,e=a.apply(c,d)):f||(f=setTimeout(h,j)),e}}b.EngagementTimer=b.EngagementTimer||d,c.prototype.clear=function(){this._cleared=!0},c.prototype.tick=function(){setTimeout(function(){this._cleared||(this._fn(),this.tick())}.bind(this),this._interval)},d.prototype._startTimer=function(){this._Interval=new c(function(){this._tick(),this._Interval.clear(),this._Interval=new c(this._tick.bind(this),this._minInterval),this._tickElapsed=0}.bind(this),Math.min(this._minInterval-this._tickElapsed,this._minInterval))},d.prototype._tick=function(){var a=+new Date;if(this._trackedTime+=a-this._lastTick,this._lastTick=a,!(this._trackedTime<this._min))return this._trackedTime>this._max?void this.destroy():void this._checkMarks()},d.prototype.on=function(a,b){this._events[a]||(this._events[a]=[]),this._events[a].push(b)},d.prototype.reset=function(){this._each=this._each.initialValues.slice(0),this._every=this._every.initialValues.slice(0),this._lastTick=+new Date,this._tickRemainder=0,this._trackedTime=0,this._cache={},this.emit("reset",{data:{timestamp:+new Date}})},d.prototype.start=function(){var a=+new Date;this._running||(!this._idleTimer&&this._idleAfter&&this._resetIdleTimeout(),this._running=!0,this._startTimer(),this._lastTick=a-this._tickElapsed,this.emit("start",{data:{timestamp:a}}))},d.prototype.pause=function(){var a=+new Date;this._tickElapsed=a-this._lastTick,this._trackedTime+=this._tickElapsed,this._Interval&&this._Interval.clear(),clearTimeout(this._idleTimer),this._running=!1,this.emit("pause",{data:{timestamp:+new Date}})},d.prototype.emit=function(a,b){this._events[a]&&this._events[a].forEach(function(a){setTimeout(function(){a(b)},0)})},d.prototype.destroy=function(){this._Interval&&this._Interval.clear(),clearTimeout(this._idleTimeout)},d.prototype._checkMarks=function(){for(var a,b,c,d,f=Math.floor(this._trackedTime/1e3),g=[],h=0;this._each.length;){if((d=this._each.shift())>f){this._each.unshift(d);break}g.push(d)}for(;h<this._every.length;)if(d=this._every.initialValues[h],b=this._every[h],c=0,h+=1,b<=f){for(a=Math.floor((f-b)/d);c<a;)c+=1,g.push(d*c+b);this._every[h-1]=d*c}g.sort(e).forEach(this._checkMark.bind(this))},d.prototype._checkMark=function(a){this._cache[a]||(this._cache[a]=!0,this.emit("interval",{data:{time:a}}))},d.prototype._resetIdleTimeout=function(){clearTimeout(this._idleTimer),this._idleTimer=setTimeout(function(){this.pause(),this.emit("idle",{data:{timestamp:+new Date}}),this._idleTimer=null}.bind(this),this._idleAfter)}}(document,window);
/*
* v2.1.1
* Created by the Google Analytics consultants at http://www.lunametrics.com/
* Written by @notdanwilkerson
* Documentation: https://www.lunametrics.com/labs/recipes/engagement-timer/
* Licensed under the MIT License
*/