UNPKG

x-track

Version:
2 lines (1 loc) 11.7 kB
class TP{constructor(t){this.entry_time=[],this.hidden_time=[],this.split_time=[],this.stay_time=0,this.bTracked=!1,this.trackCb=t&&t.trackCb?t.trackCb:function(){},this.init()}countEntryTime(){this.entry_time.push((new Date).getTime())}countHiddenTime(){this.hidden_time.push((new Date).getTime())}getAnalysisData(){var e=this.entry_time,i=this.hidden_time;for(let t=0;t<e.length;t++){var n=+(i[t]-e[t]).toFixed();this.split_time.push(n)}let o=0;for(let t=0;t<this.split_time.length;t++)o+=this.split_time[t];return o=+o.toFixed(),this.stay_time=o,{split_time:this.split_time,stay_time:this.stay_time}}sendAnalysis(){var t=this.getAnalysisData();t.stay_time&&this.trackCb(t)}initPageChange(){window.addEventListener("pushstate",()=>{this.setTP()}),window.addEventListener("replacestate",()=>{this.setTP()}),window.addEventListener("popstate",()=>{this.setTP()}),window.addEventListener("hashchange",()=>{this.setTP()})}setTP(){this.countHiddenTime(),this.sendAnalysis()}initCloseWindow(){let e=this;this.addEventListener(window,"beforeunload",function(t){!1===e.bTracked&&(e.bTracked=!0,e.setTP())},!1)}initChangeVisible(){let t=this;var e,i;void 0!==document.hidden?(e="hidden",i="visibilitychange"):void 0!==document.msHidden?(e="msHidden",i="msvisibilitychange"):void 0!==document.webkitHidden&&(e="webkitHidden",i="webkitvisibilitychange"),this.addEventListener(document,i,function(){document[e]?t.countHiddenTime():t.countEntryTime()},!1)}addEventListener(t,e,i,n){t.addEventListener?t.addEventListener(e,i,n):t.attachEvent("on"+e,i)}initStateEvent(){function t(i){let n=window.history[i];return function(){var t=n.apply(this,arguments);let e=new Event(i.toLowerCase());return e.arguments=arguments,window.dispatchEvent(e),t}}window.history.pushState=t("pushState"),window.history.replaceState=t("replaceState")}init(){this.initStateEvent(),this.countEntryTime(),this.initChangeVisible(),this.initPageChange(),this.initCloseWindow()}}!function(){var d,i,p,m;function r(t){try{return t.defaultView&&t.defaultView.frameElement||null}catch(t){return null}}function a(t){this.time=t.time,this.target=t.target,this.rootBounds=n(t.rootBounds),this.boundingClientRect=n(t.boundingClientRect),this.intersectionRect=n(t.intersectionRect||u()),this.isIntersecting=!!t.intersectionRect;var e=this.boundingClientRect,t=e.width*e.height,e=this.intersectionRect,e=e.width*e.height;this.intersectionRatio=t?Number((e/t).toFixed(4)):this.isIntersecting?1:0}function t(t,e){var i,n,o,e=e||{};if("function"!=typeof t)throw new Error("callback must be a function");if(e.root&&1!=e.root.nodeType&&9!=e.root.nodeType)throw new Error("root must be a Document or Element");this._checkForIntersections=(i=this._checkForIntersections.bind(this),n=this.THROTTLE_TIMEOUT,o=null,function(){o=o||setTimeout(function(){i(),o=null},n)}),this._callback=t,this._observationTargets=[],this._queuedEntries=[],this._rootMarginValues=this._parseRootMargin(e.rootMargin),this.thresholds=this._initThresholds(e.threshold),this.root=e.root||null,this.rootMargin=this._rootMarginValues.map(function(t){return t.value+t.unit}).join(" "),this._monitoringDocuments=[],this._monitoringUnsubscribes=[]}function s(t,e,i,n){"function"==typeof t.addEventListener?t.addEventListener(e,i,n||!1):"function"==typeof t.attachEvent&&t.attachEvent("on"+e,i)}function h(t,e,i,n){"function"==typeof t.removeEventListener?t.removeEventListener(e,i,n||!1):"function"==typeof t.detatchEvent&&t.detatchEvent("on"+e,i)}function g(t){var e;try{e=t.getBoundingClientRect()}catch(t){}return e?e.width&&e.height?e:{top:e.top,right:e.right,bottom:e.bottom,left:e.left,width:e.right-e.left,height:e.bottom-e.top}:u()}function u(){return{top:0,bottom:0,left:0,right:0,width:0,height:0}}function n(t){return!t||"x"in t?t:{top:t.top,y:t.top,bottom:t.bottom,left:t.left,x:t.left,right:t.right,width:t.width,height:t.height}}function f(t,e){var i=e.top-t.top,t=e.left-t.left;return{top:i,left:t,height:e.height,width:e.width,bottom:i+e.height,right:t+e.width}}function o(t,e){for(var i=e;i;){if(i==t)return!0;i=b(i)}return!1}function b(t){var e=t.parentNode;return 9==t.nodeType&&t!=d?r(t):(e=e&&e.assignedSlot?e.assignedSlot.parentNode:e)&&11==e.nodeType&&e.host?e.host:e}function c(t){return t&&9===t.nodeType}"object"==typeof window&&("IntersectionObserver"in window&&"IntersectionObserverEntry"in window&&"intersectionRatio"in window.IntersectionObserverEntry.prototype?"isIntersecting"in window.IntersectionObserverEntry.prototype||Object.defineProperty(window.IntersectionObserverEntry.prototype,"isIntersecting",{get:function(){return 0<this.intersectionRatio}}):(d=function(){for(var t=window.document,e=r(t);e;)e=r(t=e.ownerDocument);return t}(),i=[],m=p=null,t.prototype.THROTTLE_TIMEOUT=100,t.prototype.POLL_INTERVAL=null,t.prototype.USE_MUTATION_OBSERVER=!0,t._setupCrossOriginUpdater=function(){return p=p||function(t,e){m=t&&e?f(t,e):u(),i.forEach(function(t){t._checkForIntersections()})}},t._resetCrossOriginUpdater=function(){m=p=null},t.prototype.observe=function(e){if(!this._observationTargets.some(function(t){return t.element==e})){if(!e||1!=e.nodeType)throw new Error("target must be an Element");this._registerInstance(),this._observationTargets.push({element:e,entry:null}),this._monitorIntersections(e.ownerDocument),this._checkForIntersections()}},t.prototype.unobserve=function(e){this._observationTargets=this._observationTargets.filter(function(t){return t.element!=e}),this._unmonitorIntersections(e.ownerDocument),0==this._observationTargets.length&&this._unregisterInstance()},t.prototype.disconnect=function(){this._observationTargets=[],this._unmonitorAllIntersections(),this._unregisterInstance()},t.prototype.takeRecords=function(){var t=this._queuedEntries.slice();return this._queuedEntries=[],t},t.prototype._initThresholds=function(t){t=t||[0];return(t=!Array.isArray(t)?[t]:t).sort().filter(function(t,e,i){if("number"!=typeof t||isNaN(t)||t<0||1<t)throw new Error("threshold must be a number between 0 and 1 inclusively");return t!==i[e-1]})},t.prototype._parseRootMargin=function(t){t=(t||"0px").split(/\s+/).map(function(t){t=/^(-?\d*\.?\d+)(px|%)$/.exec(t);if(!t)throw new Error("rootMargin must be specified in pixels or percent");return{value:parseFloat(t[1]),unit:t[2]}});return t[1]=t[1]||t[0],t[2]=t[2]||t[0],t[3]=t[3]||t[1],t},t.prototype._monitorIntersections=function(e){var i,n,o,t=e.defaultView;t&&-1==this._monitoringDocuments.indexOf(e)&&(i=this._checkForIntersections,o=n=null,this.POLL_INTERVAL?n=t.setInterval(i,this.POLL_INTERVAL):(s(t,"resize",i,!0),s(e,"scroll",i,!0),this.USE_MUTATION_OBSERVER&&"MutationObserver"in t&&(o=new t.MutationObserver(i)).observe(e,{attributes:!0,childList:!0,characterData:!0,subtree:!0})),this._monitoringDocuments.push(e),this._monitoringUnsubscribes.push(function(){var t=e.defaultView;t&&(n&&t.clearInterval(n),h(t,"resize",i,!0)),h(e,"scroll",i,!0),o&&o.disconnect()}),t=this.root&&(this.root.ownerDocument||this.root)||d,e==t||(t=r(e))&&this._monitorIntersections(t.ownerDocument))},t.prototype._unmonitorIntersections=function(n){var o,t,e=this._monitoringDocuments.indexOf(n);-1!=e&&(o=this.root&&(this.root.ownerDocument||this.root)||d,this._observationTargets.some(function(t){if((e=t.element.ownerDocument)==n)return!0;for(;e&&e!=o;){var e,i=r(e);if((e=i&&i.ownerDocument)==n)return!0}return!1})||(t=this._monitoringUnsubscribes[e],this._monitoringDocuments.splice(e,1),this._monitoringUnsubscribes.splice(e,1),t(),n==o||(t=r(n))&&this._unmonitorIntersections(t.ownerDocument)))},t.prototype._unmonitorAllIntersections=function(){var t=this._monitoringUnsubscribes.slice(0);this._monitoringDocuments.length=0;for(var e=this._monitoringUnsubscribes.length=0;e<t.length;e++)t[e]()},t.prototype._checkForIntersections=function(){var h,c;!this.root&&p&&!m||(h=this._rootIsInDom(),c=h?this._getRootRect():u(),this._observationTargets.forEach(function(t){var e=t.element,i=g(e),n=this._rootContainsTarget(e),o=t.entry,r=h&&n&&this._computeTargetAndRootIntersection(e,i,c),s=null;this._rootContainsTarget(e)?p&&!this.root||(s=c):s=u();r=t.entry=new a({time:window.performance&&performance.now&&performance.now(),target:e,boundingClientRect:i,rootBounds:s,intersectionRect:r});o?h&&n?this._hasCrossedThreshold(o,r)&&this._queuedEntries.push(r):o&&o.isIntersecting&&this._queuedEntries.push(r):this._queuedEntries.push(r)},this),this._queuedEntries.length&&this._callback(this.takeRecords(),this))},t.prototype._computeTargetAndRootIntersection=function(t,e,i){if("none"!=window.getComputedStyle(t).display){for(var n=e,o=b(t),r=!1;!r&&o;){var s,h,c,a,u=null,l=1==o.nodeType?window.getComputedStyle(o):{};if("none"==l.display)return null;if(o==this.root||9==o.nodeType?(r=!0,o==this.root||o==d?p&&!this.root?!m||0==m.width&&0==m.height?n=u=o=null:u=m:u=i:(h=(s=b(o))&&g(s),c=s&&this._computeTargetAndRootIntersection(s,h,i),h&&c?(o=s,u=f(h,c)):n=o=null)):o!=(a=o.ownerDocument).body&&o!=a.documentElement&&"visible"!=l.overflow&&(u=g(o)),u&&(s=u,h=n,u=l=a=c=void 0,c=Math.max(s.top,h.top),a=Math.min(s.bottom,h.bottom),l=Math.max(s.left,h.left),u=Math.min(s.right,h.right),h=a-c,n=0<=(s=u-l)&&0<=h?{top:c,bottom:a,left:l,right:u,width:s,height:h}:null),!n)break;o=o&&b(o)}return n}},t.prototype._getRootRect=function(){var t,e;return e=this.root&&!c(this.root)?g(this.root):(t=(e=c(this.root)?this.root:d).documentElement,e=e.body,{top:0,left:0,right:t.clientWidth||e.clientWidth,width:t.clientWidth||e.clientWidth,bottom:t.clientHeight||e.clientHeight,height:t.clientHeight||e.clientHeight}),this._expandRectByRootMargin(e)},t.prototype._expandRectByRootMargin=function(i){var t=this._rootMarginValues.map(function(t,e){return"px"==t.unit?t.value:t.value*(e%2?i.width:i.height)/100}),t={top:i.top-t[0],right:i.right+t[1],bottom:i.bottom+t[2],left:i.left-t[3]};return t.width=t.right-t.left,t.height=t.bottom-t.top,t},t.prototype._hasCrossedThreshold=function(t,e){var i=t&&t.isIntersecting?t.intersectionRatio||0:-1,n=e.isIntersecting?e.intersectionRatio||0:-1;if(i!==n)for(var o=0;o<this.thresholds.length;o++){var r=this.thresholds[o];if(r==i||r==n||r<i!=r<n)return!0}},t.prototype._rootIsInDom=function(){return!this.root||o(d,this.root)},t.prototype._rootContainsTarget=function(t){var e=this.root&&(this.root.ownerDocument||this.root)||d;return o(e,t)&&(!this.root||e==t.ownerDocument)},t.prototype._registerInstance=function(){i.indexOf(this)<0&&i.push(this)},t.prototype._unregisterInstance=function(){var t=i.indexOf(this);-1!=t&&i.splice(t,1)},window.IntersectionObserver=t,window.IntersectionObserverEntry=a))}(),IntersectionObserver.prototype.THROTTLE_TIMEOUT=300;class Exposure{constructor(t={}){this._observer=null,this.trackCb=t&&t.trackCb?t.trackCb:function(){},this.observerOpt=Object.assign({root:null,rootMargin:"0px",threshold:1},t.observerOpt||{}),this.init()}init(){const o=this;let r={};this._observer=new IntersectionObserver(function(t,n){t.forEach(t=>{let e=null,i=t.target.getAttribute("exposure-id");try{e=JSON.parse(t.target.getAttribute("exposure-data"))}catch(t){e=null}e?t.isIntersecting?r[i]=setTimeout(function(){o.track(e),n.unobserve(t.target),r[i]=null},5e3):r[i]&&(clearTimeout(r[i]),r[i]=null):n.unobserve(t.target)})},o.observerOpt)}add(t){this._observer&&this._observer.observe(t)}remove(t){this._observer&&this._observer.unobserve(t)}track(t){this.trackCb(t)}}class Click{constructor(t={}){this.trackCb=t&&t.trackCb?t.trackCb:function(){}}add(t){let e=null;try{e=JSON.parse(t.getAttribute("click-data"))}catch(t){e=null}e&&t.addEventListener("click",()=>{this.track(e)})}remove(t){t.removeEventListener("click",()=>{this.track(trackData)})}track(t){this.trackCb(t)}}export{Click,Exposure,TP};