UNPKG

x-track

Version:
2 lines (1 loc) 11.9 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["x-track"]={})}(this,function(t){"use strict";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=i(t.rootBounds),this.boundingClientRect=i(t.boundingClientRect),this.intersectionRect=i(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 e(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 l(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 i(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 p(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 n(t,e){for(var i=e;i;){if(i==t)return!0;i=f(i)}return!1}function f(t){var e=t.parentNode;return 9==t.nodeType&&t!=m?r(t):(e=e&&e.assignedSlot?e.assignedSlot.parentNode:e)&&11==e.nodeType&&e.host?e.host:e}function o(t){return t&&9===t.nodeType}var m,c,g,b;"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}}):(m=function(){for(var t=window.document,e=r(t);e;)e=r(t=e.ownerDocument);return t}(),c=[],b=g=null,e.prototype.THROTTLE_TIMEOUT=100,e.prototype.POLL_INTERVAL=null,e.prototype.USE_MUTATION_OBSERVER=!0,e._setupCrossOriginUpdater=function(){return g=g||function(t,e){b=t&&e?p(t,e):u(),c.forEach(function(t){t._checkForIntersections()})}},e._resetCrossOriginUpdater=function(){b=g=null},e.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()}},e.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()},e.prototype.disconnect=function(){this._observationTargets=[],this._unmonitorAllIntersections(),this._unregisterInstance()},e.prototype.takeRecords=function(){var t=this._queuedEntries.slice();return this._queuedEntries=[],t},e.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]})},e.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},e.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)||m,e==t||(t=r(e))&&this._monitorIntersections(t.ownerDocument))},e.prototype._unmonitorIntersections=function(n){var o,t,e=this._monitoringDocuments.indexOf(n);-1!=e&&(o=this.root&&(this.root.ownerDocument||this.root)||m,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)))},e.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]()},e.prototype._checkForIntersections=function(){var h,c;!this.root&&g&&!b||(h=this._rootIsInDom(),c=h?this._getRootRect():u(),this._observationTargets.forEach(function(t){var e=t.element,i=l(e),n=this._rootContainsTarget(e),o=t.entry,r=h&&n&&this._computeTargetAndRootIntersection(e,i,c),s=null;this._rootContainsTarget(e)?g&&!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))},e.prototype._computeTargetAndRootIntersection=function(t,e,i){if("none"!=window.getComputedStyle(t).display){for(var n=e,o=f(t),r=!1;!r&&o;){var s,h,c,a,u=null,d=1==o.nodeType?window.getComputedStyle(o):{};if("none"==d.display)return null;if(o==this.root||9==o.nodeType?(r=!0,o==this.root||o==m?g&&!this.root?!b||0==b.width&&0==b.height?n=u=o=null:u=b:u=i:(h=(s=f(o))&&l(s),c=s&&this._computeTargetAndRootIntersection(s,h,i),h&&c?(o=s,u=p(h,c)):n=o=null)):o!=(a=o.ownerDocument).body&&o!=a.documentElement&&"visible"!=d.overflow&&(u=l(o)),u&&(s=u,h=n,u=d=a=c=void 0,c=Math.max(s.top,h.top),a=Math.min(s.bottom,h.bottom),d=Math.max(s.left,h.left),u=Math.min(s.right,h.right),h=a-c,n=0<=(s=u-d)&&0<=h?{top:c,bottom:a,left:d,right:u,width:s,height:h}:null),!n)break;o=o&&f(o)}return n}},e.prototype._getRootRect=function(){var t,e;return e=this.root&&!o(this.root)?l(this.root):(t=(e=o(this.root)?this.root:m).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)},e.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},e.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}},e.prototype._rootIsInDom=function(){return!this.root||n(m,this.root)},e.prototype._rootContainsTarget=function(t){var e=this.root&&(this.root.ownerDocument||this.root)||m;return n(e,t)&&(!this.root||e==t.ownerDocument)},e.prototype._registerInstance=function(){c.indexOf(this)<0&&c.push(this)},e.prototype._unregisterInstance=function(){var t=c.indexOf(this);-1!=t&&c.splice(t,1)},window.IntersectionObserver=e,window.IntersectionObserverEntry=a)),IntersectionObserver.prototype.THROTTLE_TIMEOUT=300;t.Click=class{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)}},t.Exposure=class{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)}},t.TP=class{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()}},Object.defineProperty(t,"__esModule",{value:!0})});