@datarangers/sdk-javascript 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/index-base.min.js +6218 -1
- package/es/index.min.js +9165 -1
- package/es/plugin/ab.js +1114 -1
- package/es/plugin/autotrack.js +1608 -1
- package/es/plugin/duration.js +100 -1
- package/es/plugin/route.js +605 -1
- package/es/plugin/stay.js +476 -1
- package/lib/index-base.min.js +6223 -1
- package/lib/index.min.js +9170 -1
- package/lib/plugin/ab.js +1116 -1
- package/lib/plugin/autotrack.js +1610 -1
- package/lib/plugin/duration.js +102 -1
- package/lib/plugin/route.js +607 -1
- package/lib/plugin/stay.js +478 -1
- package/package.json +1 -1
- package/types/types.d.ts +2 -0
package/es/plugin/autotrack.js
CHANGED
|
@@ -1 +1,1608 @@
|
|
|
1
|
-
var t=function(){return t=Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t},t.apply(this,arguments)};function e(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var o,i,r=n.call(t),a=[];try{for(;(void 0===e||e-- >0)&&!(o=r.next()).done;)a.push(o.value)}catch(t){i={error:t}}finally{try{o&&!o.done&&(n=r.return)&&n.call(r)}finally{if(i)throw i.error}}return a}function n(){for(var t=[],n=0;n<arguments.length;n++)t=t.concat(e(arguments[n]));return t}var o=function(t){return Array.isArray(t)};Date.now(),Number((""+Math.random()).slice(2,8));function i(t,e){if(void 0===e&&(e="list"),!t)return!1;if(e&&"list"===e){if(["LI","TR","DL"].includes(t.nodeName))return!0;if(t.dataset&&t.dataset.hasOwnProperty("teaIdx"))return!0;if(t.hasAttribute&&t.hasAttribute("data-tea-idx"))return!0}else{if(["A","BUTTON"].includes(t.nodeName))return!0;if(t.dataset&&t.dataset.hasOwnProperty("teaContainer"))return!0;if(t.hasAttribute&&t.hasAttribute("data-tea-container"))return!0;if(t.hasAttribute&&u(t,"ss"))return!0}return!1}var r=function(t,e){return!!u(t,e)};function a(t){for(var e=t;e&&!i(e,"container");){if("HTML"===e.nodeName||"BODY"===e.nodeName)return t;e=e.parentElement}return e||t}function s(t){for(var e=t;e&&e.parentNode;){if(e.hasAttribute("data-tea-ignore"))return!0;if("HTML"===e.nodeName||"body"===e.nodeName)return!1;e=e.parentNode}return!1}var c=function(t,e){return t.hasAttribute?t.hasAttribute(e):t.attributes?!(!t.attributes[e]||!t.attributes[e].specified):void 0},u=function(t,e){if("string"==typeof e)return c(t,e);if(o(e)){for(var n=!1,i=0;i<e.length;i++){if(c(t,e[i])){n=!0;break}}return n}},l=function(t,e){var n={};if("string"==typeof e)c(t,e)&&(n[e]=t.getAttribute(e));else if(o(e))for(var i=0;i<e.length;i++){c(t,e[i])&&(n[e[i]]=t.getAttribute(e[i]))}return n};var p=function(){function e(e,n,o){var a=this;this.clickEvent=function(t){(function(t,e){if(1!==t.nodeType)return!1;if(!e.svg&&function(t){if("svg"===t.tagName.toLowerCase())return!0;for(var e=t.parentElement,n=!1;e;)"svg"===e.tagName.toLowerCase()?(e=null,n=!0):e=e.parentElement;return n}(t))return!1;if(["HTML","BODY"].includes(t.tagName.toUpperCase()))return!1;var n=t;return!!("none"!==n.style.display&&(i(n,"container")||e.track_attr&&r(n,e.track_attr)||function(t){if(t.children.length){var e=t.children;return![].slice.call(e).some(function(t){return t.children.length>0})}return!0}(n)))})(t.target,a.options)&&a.eventHandel({eventType:"dom",eventName:"click"},t)},this.changeEvent=function(t){a.eventHandel({eventType:"dom",eventName:"change"},t)},this.submitEvent=function(t){a.eventHandel({eventType:"dom",eventName:"submit"},t)},this.getPageViewEvent=function(e,n){n&&"pushState"===n&&a.eventHandel({eventType:"dom",eventName:"beat"},t({beat_type:0},e)),a.eventHandel({eventType:"dom",eventName:"page_view"},e)},this.getPageLoadEvent=function(t){a.eventHandel({eventType:"dom",eventName:"page_statistics"},{lcp:t})},this.config=o.getConfig().eventConfig,this.collect=n,this.options=e,this.beatTime=e.beat,this.statistics=!1}return e.prototype.init=function(t){this.eventHandel=t;var e=this.config.mode;this.addListener(e)},e.prototype.addListener=function(t){var e=this;if("proxy-capturing"===t&&(this.config.click&&window.document.addEventListener("click",this.clickEvent,!0),this.config.change&&window.document.addEventListener("change",this.changeEvent,!0),this.config.submit&&window.document.addEventListener("submit",this.submitEvent,!0),this.config.pv&&this.collect.on("route-change",function(t){var n=t.config,o=t.name;e.getPageViewEvent(n,o)}),this.config.beat)){try{"complete"===document.readyState?this.beatEvent(this.beatTime):window.addEventListener("load",function(){e.beatEvent(e.beatTime)});var n=0,o=null;window.addEventListener("scroll",function(){clearTimeout(o),o=setTimeout(i,500),n=document.documentElement.scrollTop||document.body.scrollTop});var i=function(){(document.documentElement.scrollTop||document.body.scrollTop)==n&&e.eventHandel({eventType:"dom",eventName:"beat"},{beat_type:1})}}catch(t){}try{var r=window.performance&&window.performance.getEntriesByType("paint");if(r&&r.length){var a=new PerformanceObserver(function(t){var n=t.getEntries(),o=n[n.length-1],i=o.renderTime||o.loadTime;e.statistics||(e.getPageLoadEvent(i),e.statistics=!0)});a.observe({entryTypes:["largest-contentful-paint"]}),setTimeout(function(){e.statistics||(e.getPageLoadEvent(r[0].startTime||0),e.statistics=!0)},2e3)}else this.getPageLoadEvent(0)}catch(t){this.getPageLoadEvent(0)}}},e.prototype.removeListener=function(){window.document.removeEventListener("click",this.clickEvent,!0),window.document.removeEventListener("change",this.changeEvent,!0),window.document.removeEventListener("submit",this.submitEvent,!0)},e.prototype.beatEvent=function(t){var e,n=this;try{var o;this.eventHandel({eventType:"dom",eventName:"beat"},{beat_type:3}),this.beatTime&&(o=setInterval(function(){n.eventHandel({eventType:"dom",eventName:"beat"},{beat_type:2})},t)),e=function(){n.eventHandel({eventType:"dom",eventName:"beat",eventSend:"becon"},{beat_type:0}),n.beatTime&&clearInterval(o)},navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)?window.addEventListener("pagehide",e,!1):window.addEventListener("beforeunload",e,!1)}catch(t){}},e}(),h={eventConfig:{mode:"proxy-capturing",submit:!1,click:!0,change:!1,pv:!0,beat:!0,hashTag:!1,impr:!1},scoutConfig:{mode:"xpath"}},d=function(){function t(t,e){this.config=t,this.config.eventConfig=Object.assign(this.config.eventConfig,e)}return t.prototype.getConfig=function(){return this.config},t.prototype.setConfig=function(t){return this.config=t},t}();function v(t){for(var e=[];null!==t.parentElement;)e.push(t),t=t.parentElement;var n=[],o=[];return e.forEach(function(t){var e=function(t){if(null===t)return{str:"",index:0};var e=0,n=t.parentElement;if(n)for(var o=n.children,r=0;r<o.length&&o[r]!==t;r++)o[r].nodeName===t.nodeName&&e++;return{str:[t.nodeName.toLowerCase(),i(t,"list")?"[]":""].join(""),index:e}}(t),r=e.str,a=e.index;n.unshift(r),o.unshift(a)}),{element_path:"/"+n.join("/"),positions:o}}function f(t,e,n,o){var i={},s=function(t){if(t){var e=t.getBoundingClientRect(),n=e.width,o=e.height;return{left:e.left,top:e.top,element_width:n,element_height:o}}}(e),c=function(t,e){void 0===t&&(t={}),void 0===e&&(e={});var n=t.clientX,o=t.clientY,i=e.left,r=e.top,a=n-i>=0?n-i:0,s=o-r>=0?o-r:0;return{touch_x:Math.floor(a),touch_y:Math.floor(s)}}(t,s),u=s.element_width,p=s.element_height,h=c.touch_x,d=c.touch_y,f=v(e),m=f.element_path,g=f.positions,b=function(t){var e=a(t),n=[];return function t(e){var o=function(t){var e="";return 3===t.nodeType?e=t.textContent.trim():t.dataset&&t.dataset.hasOwnProperty("teaTitle")||t.hasAttribute("ata-tea-title")?e=t.getAttribute("data-tea-title"):t.hasAttribute("title")?e=t.getAttribute("title"):"INPUT"===t.nodeName&&["button","submit"].includes(t.getAttribute("type"))?e=t.getAttribute("value"):"IMG"===t.nodeName&&t.getAttribute("alt")&&(e=t.getAttribute("alt")),e.slice(0,200)}(e);if(o&&-1===n.indexOf(o)&&n.push(o),e.childNodes.length>0)for(var i=e.childNodes,r=0;r<i.length;r++)8!==i[r].nodeType&&t(i[r])}(e),n}(e),y=window.performance.timing.navigationStart,_=Date.now()-y,E=g.map(function(t){return""+t}),w=null;if(window.TEAVisualEditor&&window.TEAVisualEditor.getOriginXpath&&(w=window.TEAVisualEditor.getOriginXpath({xpath:m,positions:E})),i.element_path=w&&w.xpath||m,i.positions=w&&w.positions||E,o&&!o.text&&(i.texts=b,i.element_title=function(t){if(!t)return"";var e="";return t.textContent?e=t.textContent.trim():t.innerText&&(e=t.innerText.trim()),"input"!==t.tagName&&"INPUT"!==t.tagName||(e=t.value||""),e}(e)),i.element_id=e.getAttribute("id")||"",i.element_class_name=e.getAttribute("class")||"",i.element_type=e.nodeType,i.element_width=Math.floor(u),i.element_height=Math.floor(p),i.touch_x=h,i.touch_y=d,i.page_manual_key="",i.elememt_manual_key="",i.since_page_start_ms=_,i.page_start_ms=y,i.page_path=location.pathname,i.page_host=location.host,n.track_attr&&r(e,n.track_attr)){var T=l(e,n.track_attr);for(var x in T)i[x]=T[x]}if(n.custom_attr)try{if(r(e,n.custom_attr)){T=l(e,n.custom_attr),x=decodeURIComponent(T[n.custom_attr]);var O=JSON.parse(x);if(Object.keys(O))for(var L in O)i[L]=O[L]}}catch(t){console.log("custom attr error")}var S=a(e);return"A"===S.tagName&&(i.href=S.getAttribute("href")),!1!==n.src&&("IMG"!==e.tagName||e.getAttribute("src").includes("base64")||(i.src=e.getAttribute("src"))),i}var m,g=function(e,n,o,i,r){return t({event:e},f(n,o,i,r),{is_html:1,page_key:window.location.href,page_title:document.title})},b=function(){function e(t,e){this.ignore={text:!1},this.initConfig=t,this.options=e,this.eventName=e&&"tea"===e.custom?{click:"__bav_click",page:"__bav_page",beat:"__bav_beat",static:"__bav_page_statistics",exposure:"__bav_page_exposure"}:{click:"bav2b_click",page:"bav2b_page",beat:"bav2b_beat",static:"bav2b_page_statistics",exposure:"bav2b_exposure",scroll:"$bav2b_slide"},e&&!1===e.text&&(this.ignore.text=!0),e&&e.exposure&&e.exposure.eventName&&(this.eventName.exposure=e.exposure.eventName)}return e.prototype.handleEvent=function(e,n){try{if(s(e.target))return null;var o="bav2b_click";switch(n){case"click":return o=this.eventName.click,g(o,e,e.target,this.options,this.ignore);case"exposure":return o=this.eventName.exposure,g(o,e,e.target,this.options,this.ignore);case"change":return t({},g(o="bav2b_change",e,e.target,this.options),function(t,e){try{if("bav2b_change"===t)return e.hasAttribute("data-tea-track")?{value:e.value}:{}}catch(t){return{}}}(o,e.target));case"submit":return g(o="bav2b_submit",e,e.target,this.options)}}catch(t){return console.error(t),null}},e.prototype.handleViewEvent=function(t){t.event=this.eventName.page,t.page_title=document.title,t.page_total_width=window.innerWidth,t.page_total_height=window.innerHeight;try{var e=window.sessionStorage.getItem("_tea_cache_duration");if(e){var n=JSON.parse(e);t.refer_page_duration_ms=n?n.duration:""}t.scroll_width=document.documentElement.scrollLeft?document.documentElement.scrollLeft+window.innerWidth:window.innerWidth,t.scroll_height=document.documentElement.scrollTop?document.documentElement.scrollTop+window.innerHeight:window.innerHeight,t.page_start_ms=window.performance.timing.navigationStart}catch(t){console.log("page event error "+JSON.stringify(t))}return t},e.prototype.handleStatisticsEvent=function(t){var e={};e.event=this.eventName.static,e.is_html=1,e.page_key=location.href,e.refer_page_key=document.referrer||"",e.page_title=document.title,e.page_manual_key=this.initConfig.autotrack.page_manual_key||"",e.refer_page_manual_key="";try{var n=t.lcp,o=window.performance.timing,i=o.loadEventEnd-o.navigationStart;e.page_init_cost_ms=parseInt(n||(i>0?i:0)),e.page_start_ms=o.navigationStart}catch(t){console.log("page_statistics event error "+JSON.stringify(t))}return e},e.prototype.handleBeadtEvent=function(t){t.event=this.eventName.beat,t.page_key=window.location.href,t.is_html=1,t.page_title=document.title,t.page_manual_key=this.initConfig.autotrack.page_manual_key||"";try{t.page_viewport_width=window.innerWidth,t.page_viewport_height=window.innerHeight,t.page_total_width=document.documentElement.scrollWidth,t.page_total_height=document.documentElement.scrollHeight,t.scroll_width=document.documentElement.scrollLeft+window.innerWidth,t.scroll_height=document.documentElement.scrollTop+window.innerHeight,t.since_page_start_ms=Date.now()-window.performance.timing.navigationStart,t.page_start_ms=window.performance.timing.navigationStart}catch(t){console.log("beat event error "+JSON.stringify(t))}return t},e.prototype.handleExposureEvent=function(t,e){if(s(e.target))return null;var n=g(t.event||this.eventName.exposure,e,e.target||e,this.options,this.ignore);if(n.$exposure_type=t.exposureType,this.options.exposure.callback){var o=this.options.exposure.callback(n);return o||Object.keys(o).length?o:(console.warn("exposure callback must return data!"),n)}return n},e.prototype.handleScrollEvent=function(t,e){var n=g(t.event||this.eventName.scroll,e,e.target||e,this.options,this.ignore);if(n=Object.assign(n,t.params),!this.options.scroll.callback||(n=this.options.scroll.callback(n))||Object.keys(n).length)return n;console.warn("scroll callback must return data!")},e}(),y=function(){function t(t){this.collect=t}return t.prototype.send=function(t,e){var n=e.event;delete e.event,t&&"becon"===t.eventSend?this.collect.beconEvent(n,e):this.collect.event(n,e)},t.prototype.get=function(t,e){var n=Object.assign({headers:{"content-type":"application/json"},method:"GET"},e);fetch(t,n)},t.prototype.post=function(t,e){var n=Object.assign({headers:{"content-type":"application/json"},method:"POST"},e);fetch(t,n)},t}(),_=function(){function t(t,e){this._instance=null,this._intersection=t,this.config=e,this._intersection&&this.init()}return t.prototype.init=function(){var t=this;if(window.MutationObserver)try{if(this._instance=new MutationObserver(function(e){e.forEach(function(e){"attributes"===e.type&&t.attributeChangeObserve(e),"childList"===e.type&&t.modifyNodeObserve(e)})}),!document||!document.body)return void console.warn("please use sdk api init after body element");var e=!1!==this.config.autotrack.exposure.attributes;this._instance.observe(document.body,{childList:!0,attributes:e,subtree:!0,attributeOldValue:!1})}catch(t){console.log("your browser cannot support MutationObserver,so cannot report exposure event, please update")}else console.log("your browser cannot support MutationObserver,so cannot report exposure event, please update")},t.prototype.attributeChangeObserve=function(t){t.target.hasAttribute("data-exposure")?this.exposureAdd(t,"mutation"):this.exposureRemove(t)},t.prototype.modifyNodeObserve=function(t){var e=this;Array.prototype.forEach.call(t.addedNodes,function(t){1===t.nodeType&&t.hasAttribute("data-exposure")&&e.exposureAdd(t,"intersect"),e.mapChild(t,e.exposureAdd.bind(e))}),Array.prototype.forEach.call(t.removedNodes,function(t){1===t.nodeType&&t.hasAttribute("data-exposure")&&e.exposureRemove(t),e.mapChild(t,e.exposureRemove.bind(e))})},t.prototype.mapChild=function(t,e){var n=this;1===t.nodeType&&t.children.length&&Array.prototype.forEach.call(t.children,function(t){1===t.nodeType&&t.hasAttribute("data-exposure")&&e(t),n.mapChild(t,e)})},t.prototype.exposureAdd=function(t,e){try{this._intersection&&this._intersection.exposureAdd(t,e)}catch(t){console.log("intersection error",JSON.stringify(t.message))}},t.prototype.exposureRemove=function(t){try{this._intersection&&this._intersection.exposureRemove(t)}catch(t){console.log("intersection error",JSON.stringify(t.message))}},t._exposure_observer=null,t}(),E=function t(e){return e?(e^16*Math.random()>>e/4).toString(10):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,t)},w=function(){return E().replace(/-/g,"").slice(0,19)};!function(t){t[t.EXPOSURE_ONCE=0]="EXPOSURE_ONCE",t[t.LIFECYCLE_SHOW_NEW=3]="LIFECYCLE_SHOW_NEW",t[t.RESUME_FORM_PAGE=6]="RESUME_FORM_PAGE",t[t.RESUME_FORM_BACK=7]="RESUME_FORM_BACK",t[t.NOT_EXPOSURE=-1]="NOT_EXPOSURE"}(m||(m={}));var T,x=function(){function t(e,n,o){var i=this;this.backStatus=!1,this.instance=this.buildObserver(),this.collect=e,this.observeMap=t._observer_map,n.autotrack.exposure.ratio?this.ratio=n.autotrack.exposure.ratio:0===n.autotrack.exposure.ratio?this.ratio=0:this.ratio=.5,this.timeLimit=n.autotrack.exposure.stay||0,this.exposureType=m.NOT_EXPOSURE,this.eventHandle=o,this.hashMap=new Map,this.backStatus=!1,this.lastState=null,this.collect.on("re-start-sdk",function(){i.visibilitychange()}),this.addListen()}return t.prototype.buildObserver=function(){var e=this;if(t._observer_instance)return console.log("your browser cannot support IntersectionObserver, so cannot report exposure event, please update"),null;if(window.IntersectionObserver){for(var n=[],o=0;o<=1;o+=.01)n.push(o);n.push(1);try{t._observer_instance=new IntersectionObserver(function(t){t.forEach(function(t){var n=t.target.observeId,o=e.observeMap.get(n),i=(t.target,t.isIntersecting,t.intersectionRatio);if(o.intersectionRatio=i,i<=.1&&(t.target.backStatus=!1),e.observeMap.set(n,o),i<e.ratio)return o.isIntersecting=!1,o.added=!1,o.exposured=!1,o.startTime=Date.now(),void e.observeMap.set(n,o);if(!o.added){if(o.startTime=Date.now(),o.added=!0,o.isIntersecting=!0,e.timeLimit){var r=setTimeout(function(){try{if(o.isIntersecting&&!o.exposured&&e.observeMap.get(n)){var i=e.observeMap.get(n).intersectionRatio;e.exposureEvent(t,i,n,o)}}catch(t){console.log("IntersectionObserver setTimeout error,msg: "+JSON.stringify(t))}},e.timeLimit);o.wait=r}e.observeMap.set(n,o)}Date.now()-o.startTime>=e.timeLimit&&!o.exposured&&e.exposureEvent(t,i,n,o)})},{threshold:n})}catch(t){console.log("IntersectionObserver error,msg: "+JSON.stringify(t))}return t._observer_instance}return console.log("your browser cannot support IntersectionObserver, so cannot report exposure event, please update"),null},t.prototype.exposureAdd=function(t,e){var n="mutation"===e?t.target:t,o=n.observeId,i=w();if(o||this.observeMap.has(o)){var r=this.observeMap.get(o);if(r&&n!==r.instance)this.unobserve(n),this.observeMap.delete(o),n.observeId=i,n.visible=!1,this.observeMap.set(i,{instance:n.cloneNode(!0),isIntersecting:!1}),this.observe(n);else{if(!0===n.visible)return;this.handleBoundingExposure(n)}}else n.observeId=i,n.visible=!1,this.observeMap.set(i,{instance:n.cloneNode(!0),isIntersecting:!1}),this.observe(n)},t.prototype.exposureRemove=function(t){this.observeMap.has(t.observeId)&&(this.observeMap.delete(t.observeId),this.unobserve(t),t.observeId="",t.visible=!1)},t.prototype.exposureEvent=function(t,e,n,o){if(e>=this.ratio){if("0"===t.target.style.opacity||"hidden"===t.target.style.visibility)return;if(!0===o.exposured)return;o.startWait&&clearTimeout(o.startWait);var i=t.target.getAttribute("data-exposure-event");this.eventHandle({eventType:"dom",eventName:"exposure",event:i,exposureType:this.getExposureType(t.target)},t),t.target.visible=!0,t.target.hasExposure=!0,o.startTime=Date.now(),o.exposured=!0}else o.startTime=Date.now(),o.exposured=!1;this.observeMap.set(n,o)},t.prototype.observe=function(t){this.instance&&this.instance.observe(t)},t.prototype.unobserve=function(t){this.instance&&this.instance.unobserve(t)},t.prototype.getExposureDomToExposure=function(){var t=this;Array.prototype.forEach.call(document.querySelectorAll("[data-exposure]"),function(e){t.handleBoundingExposure(e)})},t.prototype.visibilitychange=function(){var t=this,e=null;"visible"===document.visibilityState?this.timeLimit?e=setTimeout(function(){t.getExposureDomToExposure()},this.timeLimit):this.getExposureDomToExposure():(clearTimeout(e),this.customType=void 0)},t.prototype.handleBoundingExposure=function(t){if(this.getBoundingRatio(t)>=this.ratio){t.visible=!0;var e=t.getAttribute&&t.getAttribute("data-exposure-event");this.eventHandle({eventType:"dom",eventName:"exposure",event:e,exposureType:this.customType||m.RESUME_FORM_BACK},t)}},t.prototype.getBoundingRatio=function(t){var e=t.getBoundingClientRect(),n=e.top,o=e.left,i=e.width,r=e.height,a=window.innerWidth||document.documentElement.clientWidth,s=window.innerHeight||document.documentElement.clientHeight;return n>=0&&o>=0&&n<=s&&o<=a?(i>a?a:i)*(s-n)/(i*r):0},t.prototype.getExposureType=function(t){return t.visible?!1!==t.backStatus&&(this.backStatus||this.customType||2===window.performance.navigation.type)?this.exposureType=m.RESUME_FORM_PAGE:this.exposureType=m.LIFECYCLE_SHOW_NEW:(t.hasExposure,!1!==t.backStatus&&(this.backStatus||this.customType||2===window.performance.navigation.type)?this.exposureType=m.RESUME_FORM_PAGE:this.exposureType=m.EXPOSURE_ONCE),this.exposureType},t.prototype.addListen=function(){var t=this;this.collect.on("set-exposure-type",function(e){t.customType=e});var e,o,i,r,a=this.visibilitychange.bind(this);if(e=document,o="visibilitychange",i=a,r=!1,e.addEventListener?e.addEventListener(o,i,r):e.attachEven?e.attachEven("on"+o,i):e["on"+o]=i,window.addEventListener("hashchange",function(e){var n=t.hashMap.get(e.oldURL);n&&location.href===n.prev?(t.backStatus=!0,t.exposureType=m.RESUME_FORM_PAGE):(t.backStatus=!1,t.hashMap.set(location.href,{current:e.newURL,prev:e.oldURL}))}),this.collect.bridgeReport){var s=window.history.pushState;history.pushState=function(e){for(var o=[],i=1;i<arguments.length;i++)o[i-1]=arguments[i];"function"==typeof history.onpushstate&&history.onpushstate({state:e});var r=s.call.apply(s,n([history,e],o));return t.lastState=e,r};var c=history.replaceState;history.replaceState=function(e){for(var o=[],i=1;i<arguments.length;i++)o[i-1]=arguments[i];"function"==typeof history.onreplacestate&&history.onreplacestate({state:e});var r=c.call.apply(c,n([history,e],o));return t.lastState=e,r}}else this.collect.on("STATE_CHANGE",function(e){t.lastState=e});return window.addEventListener("popstate",function(e){var n=e.state;n&&n.current&&t.lastState&&t.lastState.back&&n.current===t.lastState.back?(t.backStatus=!0,t.exposureType=m.RESUME_FORM_PAGE):t.backStatus=!1}),function(){!function(t,e,n,o){t.addEventListener?t.removeEventListener(e,n,o):t.attachEven&&t.detachEven("on"+e,n)}(document,"visibilitychange",a,!1)}},t._observer_instance=null,t._observer_map=new Map,t}(),O=function(){function t(t,e,n){e.autotrack&&e.autotrack.exposure&&(this._intersection=new x(t,e,n),this._observer=new _(this._intersection,e),this._intersection&&this._observer?this.initObserver():console.log("your browser version cannot support exposure event, please update~"))}return t.prototype.initObserver=function(){var t=this;Array.prototype.forEach.call(document.querySelectorAll("[data-exposure]"),function(e){t._intersection.exposureAdd(e,"intersect")})},t}();!function(t){t[t.SCROLL_UP=1]="SCROLL_UP",t[t.SCROLL_DOWN=2]="SCROLL_DOWN",t[t.SCROLL_LEFT=3]="SCROLL_LEFT",t[t.SCROLL_RIGHT=4]="SCROLL_RIGHT",t[t.NOT_SCROLL=-1]="NOT_SCROLL"}(T||(T={}));var L=function(){function t(t,e){this.distance=30,t.autotrack&&t.autotrack.scroll&&(t.autotrack.scroll.distance&&(this.distance=t.autotrack.scroll.distance),this.eventHandle=e,this.addLinstenr(),this.mutation())}return t.prototype.addLinstenr=function(){var t=this;Array.prototype.forEach.call(document.querySelectorAll("[data-scroll]"),function(e){t.scrollHandle(e)})},t.prototype.mutation=function(){var t=this;if(window.MutationObserver)try{var e=new MutationObserver(function(e){e.forEach(function(e){"childList"===e.type&&t.modifyNodeObserve(e)})});if(!document||!document.body)return void console.warn("please use sdk api init after body element");e.observe(document.body,{childList:!0,attributes:!0,subtree:!0,attributeOldValue:!1})}catch(t){console.log("your browser cannot support MutationObserver")}else console.log("your browser cannot support MutationObserver")},t.prototype.modifyNodeObserve=function(t){var e=this;Array.prototype.forEach.call(t.addedNodes,function(t){1===t.nodeType&&t.hasAttribute("data-scroll")&&e.scrollHandle(t),e.mapChild(t,e.scrollHandle.bind(e))}),Array.prototype.forEach.call(t.removedNodes,function(t){1===t.nodeType&&t.hasAttribute("data-scroll")&&e.scrollHandle(t,"remove"),e.mapChild(t,e.scrollHandle.bind(e))})},t.prototype.mapChild=function(t,e){var n=this;1===t.nodeType&&t.children.length&&Array.prototype.forEach.call(t.children,function(t){1===t.nodeType&&t.hasAttribute("data-scroll")&&e(t),n.mapChild(t,e)})},t.prototype.scrollHandle=function(t,e){var n=this;try{var o=t.scrollTop,i=t.scrollLeft,r=null,a=0,s=0,c=function(e){clearTimeout(r),r=setTimeout(function(){var r=t.scrollTop,p=t.scrollLeft,h=e.target&&e.target.getAttribute("data-scroll-event")||"";1===l||2===l?r===c&&(s=c-o,Math.abs(s)>=n.distance&&n.distance&&n.eventHandle({eventType:"dom",eventName:"scroll",event:h,params:{$direction:l,$offsetY:s,$offsetX:a}},e)):3!==l&&4!==l||p===u&&(a=u-i,Math.abs(a)>=n.distance&&n.distance&&n.eventHandle({eventType:"dom",eventName:"scroll",event:h,params:{$direction:l,$offsetY:s,$offsetX:a}},e)),o=c,i=u},100);var c=t.scrollTop,u=t.scrollLeft,l=T.NOT_SCROLL;c>o?l=T.SCROLL_DOWN:c<o?l=T.SCROLL_UP:u>i?l=T.SCROLL_LEFT:u<i&&(l=T.SCROLL_RIGHT)};return"remove"===e?t.removeEventListener("scroll",c,!1):t.addEventListener("scroll",c,!1),function(){t.removeEventListener("scroll",c,!1)}}catch(t){console.warn("scroll event error",JSON.stringify(t))}},t}(),S={hashTag:!1,impr:!1},N=function(){function t(){}return t.prototype.apply=function(t,e){if(this.autoTrackStart=!1,this.collect=t,this.config=e,e.autotrack&&(!e.autotrack||!e.autotrack.collect_url||e.autotrack.collect_url())){var n=t.Types;this.ready(n.Autotrack),this.collect.emit(n.AutotrackReady)}},t.prototype.ready=function(t){this.collect.set(t);var e=this.config.autotrack;e="object"==typeof e?e:{},e=Object.assign(S,e),this.destroyed=!1,this.options=e,this.Config=new d(h,this.options),this.Exposure=new O(this.collect,this.config,this.handle.bind(this)),this.Scroll=new L(this.config,this.handle.bind(this)),this.Listener=new p(e,this.collect,this.Config),this.EventHandle=new b(this.config,e),this.Request=new y(this.collect),this.autoTrackStart=!0,this.init()},t.prototype.init=function(){this.Listener.init(this.handle.bind(this))},t.prototype.handle=function(t,e){try{if(this.config.autotrack.collect_url&&!this.config.autotrack.collect_url())return}catch(t){}this.collect.sdkStop||"dom"===t.eventType&&this.handleDom(t,e)},t.prototype.handleDom=function(t,e){try{var n=t.eventName;if("click"===n||"change"===n||"submit"===n)(a=this.EventHandle.handleEvent(e,n))&&this.Request.send({eventType:"custom"},a);else if("page_view"===n||"page_statistics"===n){var o=void 0;(o="page_view"===n?this.EventHandle.handleViewEvent(e):this.EventHandle.handleStatisticsEvent(e))&&this.Request.send({eventType:"custom"},o)}else if("beat"===n){var i=this.EventHandle.handleBeadtEvent(e),r=t.eventSend;i&&this.Request.send({eventType:"custom",eventSend:r},i)}else if("exposure"===n){(a=this.EventHandle.handleExposureEvent(t,e))&&this.Request.send({eventType:"custom"},a)}else if("scroll"===n){var a;(a=this.EventHandle.handleScrollEvent(t,e))&&this.Request.send({eventType:"custom"},a)}}catch(t){console.log("handel dom event error "+JSON.stringify(t))}},t.prototype.destroy=function(){if(!this.autoTrackStart)return console.warn("engine is undefined, make sure you have called autoTrack.start()");this.autoTrackStart=!1,this.Listener.removeListener()},t}();export{N as default};
|
|
1
|
+
/******************************************************************************
|
|
2
|
+
Copyright (c) Microsoft Corporation.
|
|
3
|
+
|
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
+
purpose with or without fee is hereby granted.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
+
***************************************************************************** */
|
|
15
|
+
|
|
16
|
+
var __assign = function() {
|
|
17
|
+
__assign = Object.assign || function __assign(t) {
|
|
18
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
19
|
+
s = arguments[i];
|
|
20
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
21
|
+
}
|
|
22
|
+
return t;
|
|
23
|
+
};
|
|
24
|
+
return __assign.apply(this, arguments);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function __read(o, n) {
|
|
28
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
29
|
+
if (!m) return o;
|
|
30
|
+
var i = m.call(o), r, ar = [], e;
|
|
31
|
+
try {
|
|
32
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
33
|
+
}
|
|
34
|
+
catch (error) { e = { error: error }; }
|
|
35
|
+
finally {
|
|
36
|
+
try {
|
|
37
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
38
|
+
}
|
|
39
|
+
finally { if (e) throw e.error; }
|
|
40
|
+
}
|
|
41
|
+
return ar;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @deprecated */
|
|
45
|
+
function __spread() {
|
|
46
|
+
for (var ar = [], i = 0; i < arguments.length; i++)
|
|
47
|
+
ar = ar.concat(__read(arguments[i]));
|
|
48
|
+
return ar;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
52
|
+
var e = new Error(message);
|
|
53
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
|
|
57
|
+
var isArray = function isArray(obj) {
|
|
58
|
+
return Array.isArray(obj);
|
|
59
|
+
};
|
|
60
|
+
(function () {
|
|
61
|
+
var lastEventId = +Date.now() + Number(("" + Math.random()).slice(2, 8));
|
|
62
|
+
return function () {
|
|
63
|
+
lastEventId += 1;
|
|
64
|
+
return lastEventId;
|
|
65
|
+
};
|
|
66
|
+
})();
|
|
67
|
+
var beforePageUnload = function beforePageUnload(fn) {
|
|
68
|
+
var isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
|
|
69
|
+
if (isiOS) {
|
|
70
|
+
window.addEventListener("pagehide", fn, false);
|
|
71
|
+
} else {
|
|
72
|
+
window.addEventListener("beforeunload", fn, false);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
var addListener = function addListener(target, type, handler, pop) {
|
|
76
|
+
if (target.addEventListener) {
|
|
77
|
+
target.addEventListener(type, handler, pop);
|
|
78
|
+
} else if (target.attachEven) {
|
|
79
|
+
target.attachEven("on" + type, handler);
|
|
80
|
+
} else {
|
|
81
|
+
target["on" + type] = handler;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
var removeListener = function removeListener(target, type, handler, pop) {
|
|
85
|
+
if (target.addEventListener) {
|
|
86
|
+
target.removeEventListener(type, handler, pop);
|
|
87
|
+
} else if (target.attachEven) {
|
|
88
|
+
target.detachEven("on" + type, handler);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
|
|
93
|
+
function isNeedElement(element, type) {
|
|
94
|
+
if (type === void 0) {
|
|
95
|
+
type = 'list';
|
|
96
|
+
}
|
|
97
|
+
if (!element) return false;
|
|
98
|
+
if (type && type === 'list') {
|
|
99
|
+
if (['LI', 'TR', 'DL'].includes(element.nodeName)) return true;
|
|
100
|
+
if (element.dataset && element.dataset.hasOwnProperty('teaIdx')) return true;
|
|
101
|
+
if (element.hasAttribute && element.hasAttribute('data-tea-idx')) return true;
|
|
102
|
+
} else {
|
|
103
|
+
if (['A', 'BUTTON'].includes(element.nodeName)) return true;
|
|
104
|
+
if (element.dataset && element.dataset.hasOwnProperty('teaContainer')) return true;
|
|
105
|
+
if (element.hasAttribute && element.hasAttribute('data-tea-container')) return true;
|
|
106
|
+
if (element.hasAttribute && hasAttributes(element, 'ss')) return true;
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
var isAttrFilter = function isAttrFilter(element, attrs) {
|
|
111
|
+
if (hasAttributes(element, attrs)) return true;
|
|
112
|
+
return false;
|
|
113
|
+
};
|
|
114
|
+
function getContainer(element) {
|
|
115
|
+
var current = element;
|
|
116
|
+
while (current && !isNeedElement(current, 'container')) {
|
|
117
|
+
if (current.nodeName === 'HTML' || current.nodeName === 'BODY') {
|
|
118
|
+
return element;
|
|
119
|
+
}
|
|
120
|
+
current = current.parentElement;
|
|
121
|
+
}
|
|
122
|
+
return current || element;
|
|
123
|
+
}
|
|
124
|
+
function getNodeText(node) {
|
|
125
|
+
var text = '';
|
|
126
|
+
if (node.nodeType === 3) {
|
|
127
|
+
text = node.textContent.trim();
|
|
128
|
+
} else if (node['dataset'] && node['dataset'].hasOwnProperty('teaTitle')) {
|
|
129
|
+
text = node['getAttribute']('data-tea-title');
|
|
130
|
+
} else if (node['hasAttribute']('ata-tea-title')) {
|
|
131
|
+
text = node['getAttribute']('data-tea-title');
|
|
132
|
+
} else if (node['hasAttribute']('title')) {
|
|
133
|
+
text = node['getAttribute']('title');
|
|
134
|
+
} else if (node.nodeName === 'INPUT' && ['button', 'submit'].includes(node['getAttribute']('type'))) {
|
|
135
|
+
text = node['getAttribute']('value');
|
|
136
|
+
} else if (node.nodeName === 'IMG' && node['getAttribute']('alt')) {
|
|
137
|
+
text = node['getAttribute']('alt');
|
|
138
|
+
}
|
|
139
|
+
return text.slice(0, 200);
|
|
140
|
+
}
|
|
141
|
+
function getText(element) {
|
|
142
|
+
var ele = getContainer(element);
|
|
143
|
+
var textArr = [];
|
|
144
|
+
(function _get(node) {
|
|
145
|
+
var text = getNodeText(node);
|
|
146
|
+
if (text && textArr.indexOf(text) === -1) {
|
|
147
|
+
textArr.push(text);
|
|
148
|
+
}
|
|
149
|
+
if (node.childNodes.length > 0) {
|
|
150
|
+
var childNodes = node.childNodes;
|
|
151
|
+
for (var i = 0; i < childNodes.length; i++) {
|
|
152
|
+
if (childNodes[i].nodeType !== 8) {
|
|
153
|
+
_get(childNodes[i]);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
})(ele);
|
|
158
|
+
return textArr;
|
|
159
|
+
}
|
|
160
|
+
var getElementText = function getElementText(element) {
|
|
161
|
+
if (!element) return '';
|
|
162
|
+
var elementText = '';
|
|
163
|
+
if (element.textContent) {
|
|
164
|
+
elementText = element.textContent.trim();
|
|
165
|
+
} else if (element.innerText) {
|
|
166
|
+
elementText = element.innerText.trim();
|
|
167
|
+
}
|
|
168
|
+
if (element.tagName === 'input' || element.tagName === 'INPUT') {
|
|
169
|
+
elementText = element.value || '';
|
|
170
|
+
}
|
|
171
|
+
return elementText;
|
|
172
|
+
};
|
|
173
|
+
function ignore(element) {
|
|
174
|
+
var _element = element;
|
|
175
|
+
while (_element && _element.parentNode) {
|
|
176
|
+
if (_element.hasAttribute('data-tea-ignore')) return true;
|
|
177
|
+
if (_element.nodeName === 'HTML' || _element.nodeName === 'body') return false;
|
|
178
|
+
_element = _element.parentNode;
|
|
179
|
+
}
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
var hasAttribute = function hasAttribute(ele, attr) {
|
|
183
|
+
if (ele.hasAttribute) {
|
|
184
|
+
return ele.hasAttribute(attr);
|
|
185
|
+
} else if (ele.attributes) {
|
|
186
|
+
return !!(ele.attributes[attr] && ele.attributes[attr].specified);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
var hasAttributes = function hasAttributes(ele, attrs) {
|
|
190
|
+
if (typeof attrs === 'string') {
|
|
191
|
+
return hasAttribute(ele, attrs);
|
|
192
|
+
} else if (isArray(attrs)) {
|
|
193
|
+
var result = false;
|
|
194
|
+
for (var i = 0; i < attrs.length; i++) {
|
|
195
|
+
var testResult = hasAttribute(ele, attrs[i]);
|
|
196
|
+
if (testResult) {
|
|
197
|
+
result = true;
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
var getAttributes = function getAttributes(ele, attrs) {
|
|
205
|
+
var result = {};
|
|
206
|
+
if (typeof attrs === 'string') {
|
|
207
|
+
if (hasAttribute(ele, attrs)) {
|
|
208
|
+
result[attrs] = ele.getAttribute(attrs);
|
|
209
|
+
}
|
|
210
|
+
} else {
|
|
211
|
+
if (isArray(attrs)) {
|
|
212
|
+
for (var i = 0; i < attrs.length; i++) {
|
|
213
|
+
var testResult = hasAttribute(ele, attrs[i]);
|
|
214
|
+
if (testResult) {
|
|
215
|
+
result[attrs[i]] = ele.getAttribute(attrs[i]);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return result;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
|
|
224
|
+
var elementLevel = function elementLevel(element) {
|
|
225
|
+
if (element.children.length) {
|
|
226
|
+
var childElement = element.children;
|
|
227
|
+
if ([].slice.call(childElement).some(function (element) {
|
|
228
|
+
return element.children.length > 0;
|
|
229
|
+
})) {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
return true;
|
|
235
|
+
};
|
|
236
|
+
var isSVG = function isSVG(element) {
|
|
237
|
+
if (element.tagName.toLowerCase() === 'svg') {
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
var parent = element.parentElement;
|
|
241
|
+
var flag = false;
|
|
242
|
+
while (parent) {
|
|
243
|
+
if (parent.tagName.toLowerCase() === 'svg') {
|
|
244
|
+
parent = null;
|
|
245
|
+
flag = true;
|
|
246
|
+
} else {
|
|
247
|
+
parent = parent.parentElement;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return flag;
|
|
251
|
+
};
|
|
252
|
+
function isTrack(node, options) {
|
|
253
|
+
if (node.nodeType !== 1) {
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
if (!options.svg && isSVG(node)) {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
if (['HTML', 'BODY'].includes(node.tagName.toUpperCase())) {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
var element = node;
|
|
263
|
+
if (element.style.display === 'none') {
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
if (isNeedElement(element, 'container')) {
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
if (options.track_attr) {
|
|
270
|
+
if (isAttrFilter(element, options.track_attr)) {
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (!elementLevel(element)) {
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
|
|
281
|
+
var Listener = /** @class */function () {
|
|
282
|
+
function Listener(options, collect, Config) {
|
|
283
|
+
var _this = this;
|
|
284
|
+
this.clickEvent = function (e) {
|
|
285
|
+
if (isTrack(e['target'], _this.options)) {
|
|
286
|
+
_this.eventHandel({
|
|
287
|
+
eventType: 'dom',
|
|
288
|
+
eventName: 'click'
|
|
289
|
+
}, e);
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
this.changeEvent = function (e) {
|
|
293
|
+
_this.eventHandel({
|
|
294
|
+
eventType: 'dom',
|
|
295
|
+
eventName: 'change'
|
|
296
|
+
}, e);
|
|
297
|
+
};
|
|
298
|
+
this.submitEvent = function (e) {
|
|
299
|
+
_this.eventHandel({
|
|
300
|
+
eventType: 'dom',
|
|
301
|
+
eventName: 'submit'
|
|
302
|
+
}, e);
|
|
303
|
+
};
|
|
304
|
+
this.getPageViewEvent = function (eventData, name) {
|
|
305
|
+
if (name && name === 'pushState') {
|
|
306
|
+
_this.eventHandel({
|
|
307
|
+
eventType: 'dom',
|
|
308
|
+
eventName: 'beat'
|
|
309
|
+
}, __assign({
|
|
310
|
+
beat_type: 0
|
|
311
|
+
}, eventData));
|
|
312
|
+
}
|
|
313
|
+
_this.eventHandel({
|
|
314
|
+
eventType: 'dom',
|
|
315
|
+
eventName: 'page_view'
|
|
316
|
+
}, eventData);
|
|
317
|
+
};
|
|
318
|
+
this.getPageLoadEvent = function (lcp) {
|
|
319
|
+
_this.eventHandel({
|
|
320
|
+
eventType: 'dom',
|
|
321
|
+
eventName: 'page_statistics'
|
|
322
|
+
}, {
|
|
323
|
+
lcp: lcp
|
|
324
|
+
});
|
|
325
|
+
};
|
|
326
|
+
this.config = Config.getConfig().eventConfig;
|
|
327
|
+
this.collect = collect;
|
|
328
|
+
this.options = options;
|
|
329
|
+
this.beatTime = options.beat;
|
|
330
|
+
this.statistics = false;
|
|
331
|
+
}
|
|
332
|
+
Listener.prototype.init = function (eventHandel) {
|
|
333
|
+
this.eventHandel = eventHandel;
|
|
334
|
+
var mode = this.config.mode;
|
|
335
|
+
this.addListener(mode);
|
|
336
|
+
};
|
|
337
|
+
Listener.prototype.addListener = function (mode) {
|
|
338
|
+
var _this = this;
|
|
339
|
+
// 注册事件捕获
|
|
340
|
+
if (mode === 'proxy-capturing') {
|
|
341
|
+
if (this.config.click) {
|
|
342
|
+
window.document.addEventListener('click', this.clickEvent, true);
|
|
343
|
+
}
|
|
344
|
+
if (this.config.change) {
|
|
345
|
+
window.document.addEventListener('change', this.changeEvent, true);
|
|
346
|
+
}
|
|
347
|
+
if (this.config.submit) {
|
|
348
|
+
window.document.addEventListener('submit', this.submitEvent, true);
|
|
349
|
+
}
|
|
350
|
+
if (this.config.pv) {
|
|
351
|
+
this.collect.on('route-change', function (info) {
|
|
352
|
+
var config = info.config,
|
|
353
|
+
name = info.name;
|
|
354
|
+
_this.getPageViewEvent(config, name);
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
if (this.config.beat) {
|
|
358
|
+
try {
|
|
359
|
+
if (document.readyState === 'complete') {
|
|
360
|
+
this.beatEvent(this.beatTime);
|
|
361
|
+
} else {
|
|
362
|
+
window.addEventListener('load', function () {
|
|
363
|
+
_this.beatEvent(_this.beatTime);
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
var t1_1 = 0;
|
|
367
|
+
var t2_1 = 0;
|
|
368
|
+
var timer_1 = null; // 定时器
|
|
369
|
+
window.addEventListener('scroll', function () {
|
|
370
|
+
clearTimeout(timer_1);
|
|
371
|
+
timer_1 = setTimeout(isScrollEnd_1, 500);
|
|
372
|
+
t1_1 = document.documentElement.scrollTop || document.body.scrollTop;
|
|
373
|
+
});
|
|
374
|
+
var isScrollEnd_1 = function isScrollEnd_1() {
|
|
375
|
+
t2_1 = document.documentElement.scrollTop || document.body.scrollTop;
|
|
376
|
+
if (t2_1 == t1_1) {
|
|
377
|
+
_this.eventHandel({
|
|
378
|
+
eventType: 'dom',
|
|
379
|
+
eventName: 'beat'
|
|
380
|
+
}, {
|
|
381
|
+
beat_type: 1
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
} catch (e) {}
|
|
386
|
+
try {
|
|
387
|
+
var entryList = window.performance && window.performance.getEntriesByType('paint');
|
|
388
|
+
if (entryList && entryList.length) {
|
|
389
|
+
var observer = new PerformanceObserver(function (entryList) {
|
|
390
|
+
var entries = entryList.getEntries();
|
|
391
|
+
var lastEntry = entries[entries.length - 1];
|
|
392
|
+
var lcp = lastEntry['renderTime'] || lastEntry['loadTime'];
|
|
393
|
+
if (!_this.statistics) {
|
|
394
|
+
_this.getPageLoadEvent(lcp);
|
|
395
|
+
_this.statistics = true;
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
observer.observe({
|
|
399
|
+
entryTypes: ['largest-contentful-paint']
|
|
400
|
+
});
|
|
401
|
+
// 没触发2S后强制触发
|
|
402
|
+
setTimeout(function () {
|
|
403
|
+
if (_this.statistics) return;
|
|
404
|
+
_this.getPageLoadEvent(entryList[0].startTime || 0);
|
|
405
|
+
_this.statistics = true;
|
|
406
|
+
}, 2000);
|
|
407
|
+
} else {
|
|
408
|
+
this.getPageLoadEvent(0);
|
|
409
|
+
}
|
|
410
|
+
} catch (e) {
|
|
411
|
+
this.getPageLoadEvent(0);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
Listener.prototype.removeListener = function () {
|
|
417
|
+
window.document.removeEventListener('click', this.clickEvent, true);
|
|
418
|
+
window.document.removeEventListener('change', this.changeEvent, true);
|
|
419
|
+
window.document.removeEventListener('submit', this.submitEvent, true);
|
|
420
|
+
};
|
|
421
|
+
Listener.prototype.beatEvent = function (beatTime) {
|
|
422
|
+
var _this = this;
|
|
423
|
+
try {
|
|
424
|
+
this.eventHandel({
|
|
425
|
+
eventType: 'dom',
|
|
426
|
+
eventName: 'beat'
|
|
427
|
+
}, {
|
|
428
|
+
beat_type: 3
|
|
429
|
+
});
|
|
430
|
+
var beaInterval_1;
|
|
431
|
+
if (this.beatTime) {
|
|
432
|
+
beaInterval_1 = setInterval(function () {
|
|
433
|
+
_this.eventHandel({
|
|
434
|
+
eventType: 'dom',
|
|
435
|
+
eventName: 'beat'
|
|
436
|
+
}, {
|
|
437
|
+
beat_type: 2
|
|
438
|
+
});
|
|
439
|
+
}, beatTime);
|
|
440
|
+
}
|
|
441
|
+
beforePageUnload(function () {
|
|
442
|
+
_this.eventHandel({
|
|
443
|
+
eventType: 'dom',
|
|
444
|
+
eventName: 'beat',
|
|
445
|
+
eventSend: 'becon'
|
|
446
|
+
}, {
|
|
447
|
+
beat_type: 0
|
|
448
|
+
});
|
|
449
|
+
if (_this.beatTime) {
|
|
450
|
+
clearInterval(beaInterval_1);
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
} catch (e) {}
|
|
454
|
+
};
|
|
455
|
+
return Listener;
|
|
456
|
+
}();
|
|
457
|
+
|
|
458
|
+
// Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
|
|
459
|
+
var defaultConfig = {
|
|
460
|
+
eventConfig: {
|
|
461
|
+
mode: "proxy-capturing",
|
|
462
|
+
submit: false,
|
|
463
|
+
click: true,
|
|
464
|
+
change: false,
|
|
465
|
+
pv: true,
|
|
466
|
+
beat: true,
|
|
467
|
+
hashTag: false,
|
|
468
|
+
impr: false
|
|
469
|
+
},
|
|
470
|
+
scoutConfig: {
|
|
471
|
+
mode: "xpath"
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
var Config = /** @class */function () {
|
|
475
|
+
function Config(config, options) {
|
|
476
|
+
this.config = config;
|
|
477
|
+
this.config.eventConfig = Object.assign(this.config.eventConfig, options);
|
|
478
|
+
}
|
|
479
|
+
Config.prototype.getConfig = function () {
|
|
480
|
+
return this.config;
|
|
481
|
+
};
|
|
482
|
+
Config.prototype.setConfig = function (config) {
|
|
483
|
+
return this.config = config;
|
|
484
|
+
};
|
|
485
|
+
return Config;
|
|
486
|
+
}();
|
|
487
|
+
|
|
488
|
+
// Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
|
|
489
|
+
function getPositionData(target) {
|
|
490
|
+
if (!target) {
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
var rect = target.getBoundingClientRect();
|
|
494
|
+
var width = rect.width,
|
|
495
|
+
height = rect.height,
|
|
496
|
+
left = rect.left,
|
|
497
|
+
top = rect.top;
|
|
498
|
+
return {
|
|
499
|
+
left: left,
|
|
500
|
+
top: top,
|
|
501
|
+
element_width: width,
|
|
502
|
+
element_height: height
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
function getEventData$1(event, extData) {
|
|
506
|
+
if (event === void 0) {
|
|
507
|
+
event = {};
|
|
508
|
+
}
|
|
509
|
+
if (extData === void 0) {
|
|
510
|
+
extData = {};
|
|
511
|
+
}
|
|
512
|
+
var clientX = event.clientX,
|
|
513
|
+
clientY = event.clientY;
|
|
514
|
+
var left = extData.left,
|
|
515
|
+
top = extData.top;
|
|
516
|
+
var touchX = clientX - left >= 0 ? clientX - left : 0;
|
|
517
|
+
var touchY = clientY - top >= 0 ? clientY - top : 0;
|
|
518
|
+
return {
|
|
519
|
+
touch_x: Math.floor(touchX),
|
|
520
|
+
touch_y: Math.floor(touchY)
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
function getXpath(target) {
|
|
524
|
+
var targetList = [];
|
|
525
|
+
while (target.parentElement !== null) {
|
|
526
|
+
targetList.push(target);
|
|
527
|
+
target = target.parentElement;
|
|
528
|
+
}
|
|
529
|
+
var xpathArr = [];
|
|
530
|
+
var positions = [];
|
|
531
|
+
targetList.forEach(function (cur) {
|
|
532
|
+
var _a = getXpathIndex(cur),
|
|
533
|
+
str = _a.str,
|
|
534
|
+
index = _a.index;
|
|
535
|
+
xpathArr.unshift(str);
|
|
536
|
+
positions.unshift(index);
|
|
537
|
+
});
|
|
538
|
+
return {
|
|
539
|
+
element_path: "/" + xpathArr.join('/'),
|
|
540
|
+
positions: positions
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
function getXpathIndex(dom) {
|
|
544
|
+
if (dom === null) {
|
|
545
|
+
return {
|
|
546
|
+
str: '',
|
|
547
|
+
index: 0
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
var index = 0;
|
|
551
|
+
var parent = dom.parentElement;
|
|
552
|
+
if (parent) {
|
|
553
|
+
var childrens = parent.children;
|
|
554
|
+
for (var i = 0; i < childrens.length; i++) {
|
|
555
|
+
if (childrens[i] === dom) break;
|
|
556
|
+
if (childrens[i].nodeName === dom.nodeName) {
|
|
557
|
+
index++;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
var tag = [dom.nodeName.toLowerCase(), isNeedElement(dom, 'list') ? '[]' : ''].join('');
|
|
562
|
+
return {
|
|
563
|
+
str: tag,
|
|
564
|
+
index: index
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
|
|
569
|
+
function getElementData(event, element, options, ignore) {
|
|
570
|
+
var elementData = {};
|
|
571
|
+
var positionData = getPositionData(element);
|
|
572
|
+
var eventData = getEventData$1(event, positionData);
|
|
573
|
+
var element_width = positionData.element_width,
|
|
574
|
+
element_height = positionData.element_height;
|
|
575
|
+
var touch_x = eventData.touch_x,
|
|
576
|
+
touch_y = eventData.touch_y;
|
|
577
|
+
var _a = getXpath(element),
|
|
578
|
+
element_path = _a.element_path,
|
|
579
|
+
positions = _a.positions;
|
|
580
|
+
var texts = getText(element);
|
|
581
|
+
var page_start_ms = window.performance.timing.navigationStart;
|
|
582
|
+
var since_page_start_ms = Date.now() - page_start_ms;
|
|
583
|
+
var _position = positions.map(function (item) {
|
|
584
|
+
return "" + item;
|
|
585
|
+
});
|
|
586
|
+
var elementObj = null;
|
|
587
|
+
if (window.TEAVisualEditor && window.TEAVisualEditor.getOriginXpath) {
|
|
588
|
+
elementObj = window.TEAVisualEditor.getOriginXpath({
|
|
589
|
+
xpath: element_path,
|
|
590
|
+
positions: _position
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
elementData.element_path = elementObj && elementObj.xpath || element_path;
|
|
594
|
+
elementData.positions = elementObj && elementObj.positions || _position;
|
|
595
|
+
if (ignore && !ignore.text) {
|
|
596
|
+
elementData.texts = texts;
|
|
597
|
+
elementData.element_title = getElementText(element);
|
|
598
|
+
}
|
|
599
|
+
elementData.element_id = element.getAttribute('id') || '';
|
|
600
|
+
elementData.element_class_name = element.getAttribute('class') || '';
|
|
601
|
+
elementData.element_type = element.nodeType;
|
|
602
|
+
elementData.element_width = Math.floor(element_width);
|
|
603
|
+
elementData.element_height = Math.floor(element_height);
|
|
604
|
+
elementData.touch_x = touch_x;
|
|
605
|
+
elementData.touch_y = touch_y;
|
|
606
|
+
elementData.page_manual_key = '';
|
|
607
|
+
elementData.elememt_manual_key = '';
|
|
608
|
+
elementData.since_page_start_ms = since_page_start_ms;
|
|
609
|
+
elementData.page_start_ms = page_start_ms;
|
|
610
|
+
elementData.page_path = location.pathname;
|
|
611
|
+
elementData.page_host = location.host;
|
|
612
|
+
if (options.track_attr) {
|
|
613
|
+
if (isAttrFilter(element, options.track_attr)) {
|
|
614
|
+
var attrData = getAttributes(element, options.track_attr);
|
|
615
|
+
for (var attr in attrData) {
|
|
616
|
+
elementData[attr] = attrData[attr];
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
if (options.custom_attr) {
|
|
621
|
+
try {
|
|
622
|
+
if (isAttrFilter(element, options.custom_attr)) {
|
|
623
|
+
var attrData = getAttributes(element, options.custom_attr);
|
|
624
|
+
var attr = decodeURIComponent(attrData[options.custom_attr]);
|
|
625
|
+
var attrObj = JSON.parse(attr);
|
|
626
|
+
if (Object.keys(attrObj)) {
|
|
627
|
+
for (var key in attrObj) {
|
|
628
|
+
elementData[key] = attrObj[key];
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
} catch (e) {
|
|
633
|
+
console.log('custom attr error');
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
var containerNoode = getContainer(element);
|
|
637
|
+
if (containerNoode.tagName === 'A') {
|
|
638
|
+
elementData.href = containerNoode.getAttribute('href');
|
|
639
|
+
}
|
|
640
|
+
if (options.src !== false) {
|
|
641
|
+
if (element.tagName === 'IMG' && !element.getAttribute('src').includes('base64')) {
|
|
642
|
+
elementData.src = element.getAttribute('src');
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
return elementData;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
// Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
|
|
649
|
+
var getEventData = function getEventData(event, e, element, options, ignore) {
|
|
650
|
+
return __assign(__assign({
|
|
651
|
+
event: event
|
|
652
|
+
}, getElementData(e, element, options, ignore)), {
|
|
653
|
+
is_html: 1,
|
|
654
|
+
page_key: window.location.href,
|
|
655
|
+
page_title: document.title
|
|
656
|
+
});
|
|
657
|
+
};
|
|
658
|
+
var getExtraEventData = function getExtraEventData(event, element) {
|
|
659
|
+
try {
|
|
660
|
+
if (event === 'bav2b_change') {
|
|
661
|
+
if (element.hasAttribute('data-tea-track')) {
|
|
662
|
+
return {
|
|
663
|
+
value: element.value
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
return {};
|
|
667
|
+
}
|
|
668
|
+
} catch (err) {
|
|
669
|
+
return {};
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
var EventHandle = /** @class */function () {
|
|
673
|
+
function EventHandle(initConfig, options) {
|
|
674
|
+
this.ignore = {
|
|
675
|
+
text: false
|
|
676
|
+
};
|
|
677
|
+
this.initConfig = initConfig;
|
|
678
|
+
this.options = options;
|
|
679
|
+
this.eventName = options && options.custom === 'tea' ? {
|
|
680
|
+
click: '__bav_click',
|
|
681
|
+
page: '__bav_page',
|
|
682
|
+
beat: '__bav_beat',
|
|
683
|
+
"static": '__bav_page_statistics',
|
|
684
|
+
exposure: '__bav_page_exposure'
|
|
685
|
+
} : {
|
|
686
|
+
click: 'bav2b_click',
|
|
687
|
+
page: 'bav2b_page',
|
|
688
|
+
beat: 'bav2b_beat',
|
|
689
|
+
"static": 'bav2b_page_statistics',
|
|
690
|
+
exposure: 'bav2b_exposure',
|
|
691
|
+
scroll: '$bav2b_slide'
|
|
692
|
+
};
|
|
693
|
+
if (options && options.text === false) {
|
|
694
|
+
this.ignore.text = true;
|
|
695
|
+
}
|
|
696
|
+
if (options && options.exposure && options.exposure.eventName) {
|
|
697
|
+
this.eventName['exposure'] = options.exposure.eventName;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
EventHandle.prototype.handleEvent = function (e, eventType) {
|
|
701
|
+
try {
|
|
702
|
+
if (ignore(e.target)) {
|
|
703
|
+
return null;
|
|
704
|
+
}
|
|
705
|
+
var event_1 = 'bav2b_click';
|
|
706
|
+
switch (eventType) {
|
|
707
|
+
case 'click':
|
|
708
|
+
event_1 = this.eventName['click'];
|
|
709
|
+
return getEventData(event_1, e, e.target, this.options, this.ignore);
|
|
710
|
+
case 'exposure':
|
|
711
|
+
event_1 = this.eventName['exposure'];
|
|
712
|
+
return getEventData(event_1, e, e.target, this.options, this.ignore);
|
|
713
|
+
case 'change':
|
|
714
|
+
event_1 = 'bav2b_change';
|
|
715
|
+
return __assign(__assign({}, getEventData(event_1, e, e.target, this.options)), getExtraEventData(event_1, e.target));
|
|
716
|
+
case 'submit':
|
|
717
|
+
event_1 = 'bav2b_submit';
|
|
718
|
+
return getEventData(event_1, e, e.target, this.options);
|
|
719
|
+
}
|
|
720
|
+
} catch (err) {
|
|
721
|
+
console.error(err);
|
|
722
|
+
return null;
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
EventHandle.prototype.handleViewEvent = function (data) {
|
|
726
|
+
data.event = this.eventName['page'];
|
|
727
|
+
data.page_title = document.title;
|
|
728
|
+
data.page_total_width = window.innerWidth;
|
|
729
|
+
data.page_total_height = window.innerHeight;
|
|
730
|
+
try {
|
|
731
|
+
var cache = window.sessionStorage.getItem('_tea_cache_duration');
|
|
732
|
+
if (cache) {
|
|
733
|
+
var duration = JSON.parse(cache);
|
|
734
|
+
data.refer_page_duration_ms = duration ? duration.duration : '';
|
|
735
|
+
}
|
|
736
|
+
data.scroll_width = document.documentElement.scrollLeft ? document.documentElement.scrollLeft + window.innerWidth : window.innerWidth;
|
|
737
|
+
data.scroll_height = document.documentElement.scrollTop ? document.documentElement.scrollTop + window.innerHeight : window.innerHeight;
|
|
738
|
+
data.page_start_ms = window.performance.timing.navigationStart;
|
|
739
|
+
} catch (e) {
|
|
740
|
+
console.log("page event error " + JSON.stringify(e));
|
|
741
|
+
}
|
|
742
|
+
return data;
|
|
743
|
+
};
|
|
744
|
+
EventHandle.prototype.handleStatisticsEvent = function (data) {
|
|
745
|
+
var _data = {};
|
|
746
|
+
_data['event'] = this.eventName['static'];
|
|
747
|
+
_data['is_html'] = 1;
|
|
748
|
+
_data['page_key'] = location.href;
|
|
749
|
+
_data['refer_page_key'] = document.referrer || '';
|
|
750
|
+
_data['page_title'] = document.title;
|
|
751
|
+
_data['page_manual_key'] = this.initConfig.autotrack.page_manual_key || '';
|
|
752
|
+
_data['refer_page_manual_key'] = '';
|
|
753
|
+
try {
|
|
754
|
+
var lcp = data.lcp;
|
|
755
|
+
var timing = window.performance.timing;
|
|
756
|
+
var init_cos = timing.loadEventEnd - timing.navigationStart;
|
|
757
|
+
_data['page_init_cost_ms'] = parseInt(lcp || (init_cos > 0 ? init_cos : 0));
|
|
758
|
+
_data['page_start_ms'] = timing.navigationStart;
|
|
759
|
+
} catch (e) {
|
|
760
|
+
console.log("page_statistics event error " + JSON.stringify(e));
|
|
761
|
+
}
|
|
762
|
+
return _data;
|
|
763
|
+
};
|
|
764
|
+
EventHandle.prototype.handleBeadtEvent = function (data) {
|
|
765
|
+
data.event = this.eventName['beat'];
|
|
766
|
+
data.page_key = window.location.href;
|
|
767
|
+
data.is_html = 1;
|
|
768
|
+
data.page_title = document.title;
|
|
769
|
+
data.page_manual_key = this.initConfig.autotrack.page_manual_key || '';
|
|
770
|
+
try {
|
|
771
|
+
data.page_viewport_width = window.innerWidth;
|
|
772
|
+
data.page_viewport_height = window.innerHeight;
|
|
773
|
+
data.page_total_width = document.documentElement.scrollWidth;
|
|
774
|
+
data.page_total_height = document.documentElement.scrollHeight;
|
|
775
|
+
data.scroll_width = document.documentElement.scrollLeft + window.innerWidth;
|
|
776
|
+
data.scroll_height = document.documentElement.scrollTop + window.innerHeight;
|
|
777
|
+
data.since_page_start_ms = Date.now() - window.performance.timing.navigationStart;
|
|
778
|
+
data.page_start_ms = window.performance.timing.navigationStart;
|
|
779
|
+
} catch (e) {
|
|
780
|
+
console.log("beat event error " + JSON.stringify(e));
|
|
781
|
+
}
|
|
782
|
+
return data;
|
|
783
|
+
};
|
|
784
|
+
EventHandle.prototype.handleExposureEvent = function (eventInfo, e) {
|
|
785
|
+
if (ignore(e.target)) {
|
|
786
|
+
return null;
|
|
787
|
+
}
|
|
788
|
+
var data = getEventData(eventInfo.event || this.eventName['exposure'], e, e.target || e, this.options, this.ignore);
|
|
789
|
+
data.$exposure_type = eventInfo.exposureType;
|
|
790
|
+
if (this.options.exposure.callback) {
|
|
791
|
+
var callData = this.options.exposure.callback(data);
|
|
792
|
+
if (!callData && !Object.keys(callData).length) {
|
|
793
|
+
console.warn('exposure callback must return data!');
|
|
794
|
+
return data;
|
|
795
|
+
}
|
|
796
|
+
return callData;
|
|
797
|
+
}
|
|
798
|
+
return data;
|
|
799
|
+
};
|
|
800
|
+
EventHandle.prototype.handleScrollEvent = function (eventInfo, e) {
|
|
801
|
+
var data = getEventData(eventInfo.event || this.eventName['scroll'], e, e.target || e, this.options, this.ignore);
|
|
802
|
+
data = Object.assign(data, eventInfo.params);
|
|
803
|
+
if (this.options.scroll.callback) {
|
|
804
|
+
data = this.options.scroll.callback(data);
|
|
805
|
+
if (!data && !Object.keys(data).length) {
|
|
806
|
+
console.warn('scroll callback must return data!');
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
return data;
|
|
811
|
+
};
|
|
812
|
+
return EventHandle;
|
|
813
|
+
}();
|
|
814
|
+
|
|
815
|
+
// Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
|
|
816
|
+
var Request = /** @class */function () {
|
|
817
|
+
function Request(collect) {
|
|
818
|
+
this.collect = collect;
|
|
819
|
+
}
|
|
820
|
+
Request.prototype.send = function (eventInfo, data) {
|
|
821
|
+
var event = data['event'];
|
|
822
|
+
delete data['event'];
|
|
823
|
+
if (eventInfo && eventInfo.eventSend === 'becon') {
|
|
824
|
+
this.collect.beconEvent(event, data);
|
|
825
|
+
} else {
|
|
826
|
+
this.collect.event(event, data);
|
|
827
|
+
}
|
|
828
|
+
};
|
|
829
|
+
Request.prototype.get = function (url, options) {
|
|
830
|
+
var reqOptions = {
|
|
831
|
+
headers: {
|
|
832
|
+
'content-type': 'application/json'
|
|
833
|
+
},
|
|
834
|
+
method: 'GET'
|
|
835
|
+
};
|
|
836
|
+
var myOptions = Object.assign(reqOptions, options);
|
|
837
|
+
fetch(url, myOptions);
|
|
838
|
+
};
|
|
839
|
+
Request.prototype.post = function (url, options) {
|
|
840
|
+
var reqOptions = {
|
|
841
|
+
headers: {
|
|
842
|
+
'content-type': 'application/json'
|
|
843
|
+
},
|
|
844
|
+
method: 'POST'
|
|
845
|
+
};
|
|
846
|
+
var myOptions = Object.assign(reqOptions, options);
|
|
847
|
+
fetch(url, myOptions);
|
|
848
|
+
};
|
|
849
|
+
return Request;
|
|
850
|
+
}();
|
|
851
|
+
|
|
852
|
+
// Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
|
|
853
|
+
// 监视曝光的上层类 处理动态dom
|
|
854
|
+
var Observer = /** @class */function () {
|
|
855
|
+
function Observer(intersection, config) {
|
|
856
|
+
this._instance = null;
|
|
857
|
+
this._intersection = intersection;
|
|
858
|
+
this.config = config;
|
|
859
|
+
if (!this._intersection) return;
|
|
860
|
+
this.init();
|
|
861
|
+
}
|
|
862
|
+
// 初始化mutation对象观察动态dom
|
|
863
|
+
Observer.prototype.init = function () {
|
|
864
|
+
var _this = this;
|
|
865
|
+
if (window['MutationObserver']) {
|
|
866
|
+
try {
|
|
867
|
+
this._instance = new MutationObserver(function (mutations) {
|
|
868
|
+
mutations.forEach(function (mutation) {
|
|
869
|
+
// 更新dom节点可能是update,只是属性改变,需要监听
|
|
870
|
+
if (mutation.type === 'attributes') {
|
|
871
|
+
_this.attributeChangeObserve(mutation);
|
|
872
|
+
}
|
|
873
|
+
// dom节点变化
|
|
874
|
+
if (mutation.type === 'childList') {
|
|
875
|
+
_this.modifyNodeObserve(mutation);
|
|
876
|
+
}
|
|
877
|
+
});
|
|
878
|
+
});
|
|
879
|
+
if (!document || !document.body) {
|
|
880
|
+
console.warn('please use sdk api init after body element');
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
var attributes = this.config.autotrack.exposure.attributes === false ? false : true;
|
|
884
|
+
this._instance.observe(document.body, {
|
|
885
|
+
childList: true,
|
|
886
|
+
attributes: attributes,
|
|
887
|
+
subtree: true,
|
|
888
|
+
attributeOldValue: false
|
|
889
|
+
});
|
|
890
|
+
} catch (e) {
|
|
891
|
+
console.log('your browser cannot support MutationObserver,so cannot report exposure event, please update');
|
|
892
|
+
}
|
|
893
|
+
} else {
|
|
894
|
+
console.log('your browser cannot support MutationObserver,so cannot report exposure event, please update');
|
|
895
|
+
}
|
|
896
|
+
};
|
|
897
|
+
// 监听dom属性变化添加或删除节点曝光监听
|
|
898
|
+
Observer.prototype.attributeChangeObserve = function (mutation) {
|
|
899
|
+
var dom = mutation.target;
|
|
900
|
+
if (dom.hasAttribute('data-exposure')) {
|
|
901
|
+
this.exposureAdd(mutation, 'mutation');
|
|
902
|
+
} else {
|
|
903
|
+
this.exposureRemove(mutation);
|
|
904
|
+
}
|
|
905
|
+
};
|
|
906
|
+
// 监听dom变化添加或删除节点曝光监听
|
|
907
|
+
Observer.prototype.modifyNodeObserve = function (mutation) {
|
|
908
|
+
var _this = this;
|
|
909
|
+
// 不能为文本节点&&要有auto-exp属性
|
|
910
|
+
Array.prototype.forEach.call(mutation.addedNodes, function (node) {
|
|
911
|
+
if (node.nodeType === 1 && node.hasAttribute('data-exposure')) {
|
|
912
|
+
_this.exposureAdd(node, 'intersect');
|
|
913
|
+
}
|
|
914
|
+
_this.mapChild(node, _this.exposureAdd.bind(_this));
|
|
915
|
+
});
|
|
916
|
+
// 遍历子节点的删除
|
|
917
|
+
Array.prototype.forEach.call(mutation.removedNodes, function (node) {
|
|
918
|
+
if (node.nodeType === 1 && node.hasAttribute('data-exposure')) {
|
|
919
|
+
_this.exposureRemove(node);
|
|
920
|
+
}
|
|
921
|
+
_this.mapChild(node, _this.exposureRemove.bind(_this));
|
|
922
|
+
});
|
|
923
|
+
};
|
|
924
|
+
// 递归后代节点
|
|
925
|
+
Observer.prototype.mapChild = function (node, action) {
|
|
926
|
+
var _this = this;
|
|
927
|
+
if (node.nodeType !== 1) {
|
|
928
|
+
return;
|
|
929
|
+
}
|
|
930
|
+
if (!node.children.length) {
|
|
931
|
+
return;
|
|
932
|
+
}
|
|
933
|
+
Array.prototype.forEach.call(node.children, function (item) {
|
|
934
|
+
if (item.nodeType === 1 && item.hasAttribute('data-exposure')) {
|
|
935
|
+
action(item);
|
|
936
|
+
}
|
|
937
|
+
_this.mapChild(item, action);
|
|
938
|
+
});
|
|
939
|
+
};
|
|
940
|
+
// 添加进入曝光队列
|
|
941
|
+
Observer.prototype.exposureAdd = function (dom, type) {
|
|
942
|
+
try {
|
|
943
|
+
this._intersection && this._intersection.exposureAdd(dom, type);
|
|
944
|
+
} catch (e) {
|
|
945
|
+
console.log('intersection error', JSON.stringify(e.message));
|
|
946
|
+
}
|
|
947
|
+
};
|
|
948
|
+
// 从曝光队列中移除
|
|
949
|
+
Observer.prototype.exposureRemove = function (dom) {
|
|
950
|
+
try {
|
|
951
|
+
this._intersection && this._intersection.exposureRemove(dom);
|
|
952
|
+
} catch (e) {
|
|
953
|
+
console.log('intersection error', JSON.stringify(e.message));
|
|
954
|
+
}
|
|
955
|
+
};
|
|
956
|
+
Observer._exposure_observer = null;
|
|
957
|
+
return Observer;
|
|
958
|
+
}();
|
|
959
|
+
|
|
960
|
+
var b = function b(a) {
|
|
961
|
+
return a ? (a ^ Math.random() * 16 >> a / 4).toString(10) : ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, b);
|
|
962
|
+
};
|
|
963
|
+
var localWebId = function localWebId() {
|
|
964
|
+
return b().replace(/-/g, '').slice(0, 19);
|
|
965
|
+
};
|
|
966
|
+
|
|
967
|
+
// Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
|
|
968
|
+
var ExposureTypeE;
|
|
969
|
+
(function (ExposureTypeE) {
|
|
970
|
+
ExposureTypeE[ExposureTypeE["EXPOSURE_ONCE"] = 0] = "EXPOSURE_ONCE";
|
|
971
|
+
ExposureTypeE[ExposureTypeE["LIFECYCLE_SHOW_NEW"] = 3] = "LIFECYCLE_SHOW_NEW";
|
|
972
|
+
ExposureTypeE[ExposureTypeE["RESUME_FORM_PAGE"] = 6] = "RESUME_FORM_PAGE";
|
|
973
|
+
ExposureTypeE[ExposureTypeE["RESUME_FORM_BACK"] = 7] = "RESUME_FORM_BACK";
|
|
974
|
+
ExposureTypeE[ExposureTypeE["NOT_EXPOSURE"] = -1] = "NOT_EXPOSURE";
|
|
975
|
+
})(ExposureTypeE || (ExposureTypeE = {}));
|
|
976
|
+
var Intersection = /** @class */function () {
|
|
977
|
+
function Intersection(collect, config, eventHandle) {
|
|
978
|
+
var _this = this;
|
|
979
|
+
this.backStatus = false;
|
|
980
|
+
this.instance = this.buildObserver();
|
|
981
|
+
this.collect = collect;
|
|
982
|
+
this.observeMap = Intersection._observer_map;
|
|
983
|
+
if (config.autotrack.exposure.ratio) {
|
|
984
|
+
this.ratio = config.autotrack.exposure.ratio;
|
|
985
|
+
} else if (config.autotrack.exposure.ratio === 0) {
|
|
986
|
+
this.ratio = 0;
|
|
987
|
+
} else {
|
|
988
|
+
this.ratio = 0.5;
|
|
989
|
+
}
|
|
990
|
+
// 元素出现的时长,有值则等待,无值则立刻曝光
|
|
991
|
+
this.timeLimit = config.autotrack.exposure.stay || 0;
|
|
992
|
+
this.exposureType = ExposureTypeE.NOT_EXPOSURE;
|
|
993
|
+
this.eventHandle = eventHandle;
|
|
994
|
+
this.hashMap = new Map();
|
|
995
|
+
this.backStatus = false;
|
|
996
|
+
this.lastState = null;
|
|
997
|
+
this.collect.on('re-start-sdk', function () {
|
|
998
|
+
_this.visibilitychange();
|
|
999
|
+
});
|
|
1000
|
+
this.addListen();
|
|
1001
|
+
}
|
|
1002
|
+
// dom元素出现在视窗,出现回调;
|
|
1003
|
+
Intersection.prototype.buildObserver = function () {
|
|
1004
|
+
var _this = this;
|
|
1005
|
+
if (!Intersection._observer_instance) {
|
|
1006
|
+
if (window['IntersectionObserver']) {
|
|
1007
|
+
var threshold = [];
|
|
1008
|
+
for (var i = 0; i <= 1.0; i += 0.01) {
|
|
1009
|
+
threshold.push(i);
|
|
1010
|
+
}
|
|
1011
|
+
threshold.push(1);
|
|
1012
|
+
try {
|
|
1013
|
+
Intersection._observer_instance = new IntersectionObserver(function (entries) {
|
|
1014
|
+
entries.forEach(function (entry) {
|
|
1015
|
+
var elememtMapKey = entry.target['observeId'];
|
|
1016
|
+
var exposureDom = _this.observeMap.get(elememtMapKey);
|
|
1017
|
+
var target = entry.target,
|
|
1018
|
+
isIntersecting = entry.isIntersecting,
|
|
1019
|
+
intersectionRatio = entry.intersectionRatio;
|
|
1020
|
+
exposureDom.intersectionRatio = intersectionRatio;
|
|
1021
|
+
if (intersectionRatio <= 0.1) {
|
|
1022
|
+
// 离开曝光区
|
|
1023
|
+
entry.target['backStatus'] = false;
|
|
1024
|
+
}
|
|
1025
|
+
_this.observeMap.set(elememtMapKey, exposureDom);
|
|
1026
|
+
if (intersectionRatio < _this.ratio) {
|
|
1027
|
+
exposureDom.isIntersecting = false;
|
|
1028
|
+
exposureDom.added = false;
|
|
1029
|
+
exposureDom.exposured = false;
|
|
1030
|
+
exposureDom.startTime = Date.now();
|
|
1031
|
+
_this.observeMap.set(elememtMapKey, exposureDom);
|
|
1032
|
+
return;
|
|
1033
|
+
}
|
|
1034
|
+
if (!exposureDom.added) {
|
|
1035
|
+
exposureDom.startTime = Date.now();
|
|
1036
|
+
// 元素交互时处理一次,为false或者undefined的代表没有处理过
|
|
1037
|
+
exposureDom.added = true;
|
|
1038
|
+
exposureDom.isIntersecting = true;
|
|
1039
|
+
if (_this.timeLimit) {
|
|
1040
|
+
var exposureTimeout = setTimeout(function () {
|
|
1041
|
+
try {
|
|
1042
|
+
if (exposureDom.isIntersecting && !exposureDom.exposured && _this.observeMap.get(elememtMapKey)) {
|
|
1043
|
+
var intersectionRatio_1 = _this.observeMap.get(elememtMapKey).intersectionRatio;
|
|
1044
|
+
_this.exposureEvent(entry, intersectionRatio_1, elememtMapKey, exposureDom);
|
|
1045
|
+
// this.eventHandle({ eventType: 'dom', eventName: 'exposure' }, entry)
|
|
1046
|
+
}
|
|
1047
|
+
} catch (e) {
|
|
1048
|
+
console.log("IntersectionObserver setTimeout error\uFF0Cmsg: " + JSON.stringify(e));
|
|
1049
|
+
}
|
|
1050
|
+
}, _this.timeLimit);
|
|
1051
|
+
exposureDom.wait = exposureTimeout;
|
|
1052
|
+
}
|
|
1053
|
+
_this.observeMap.set(elememtMapKey, exposureDom);
|
|
1054
|
+
}
|
|
1055
|
+
if (Date.now() - exposureDom.startTime >= _this.timeLimit && !exposureDom.exposured) {
|
|
1056
|
+
_this.exposureEvent(entry, intersectionRatio, elememtMapKey, exposureDom);
|
|
1057
|
+
}
|
|
1058
|
+
});
|
|
1059
|
+
}, {
|
|
1060
|
+
threshold: threshold
|
|
1061
|
+
});
|
|
1062
|
+
} catch (e) {
|
|
1063
|
+
console.log("IntersectionObserver error\uFF0Cmsg: " + JSON.stringify(e));
|
|
1064
|
+
}
|
|
1065
|
+
return Intersection._observer_instance;
|
|
1066
|
+
} else {
|
|
1067
|
+
console.log('your browser cannot support IntersectionObserver, so cannot report exposure event, please update');
|
|
1068
|
+
return null;
|
|
1069
|
+
}
|
|
1070
|
+
} else {
|
|
1071
|
+
console.log('your browser cannot support IntersectionObserver, so cannot report exposure event, please update');
|
|
1072
|
+
return null;
|
|
1073
|
+
}
|
|
1074
|
+
};
|
|
1075
|
+
// 添加进入曝光队列
|
|
1076
|
+
Intersection.prototype.exposureAdd = function (dom, type) {
|
|
1077
|
+
var trueDom = type === 'mutation' ? dom.target : dom;
|
|
1078
|
+
var elememtMapKey = trueDom['observeId'];
|
|
1079
|
+
var domId = localWebId();
|
|
1080
|
+
if (!elememtMapKey && !this.observeMap.has(elememtMapKey)) {
|
|
1081
|
+
trueDom['observeId'] = domId;
|
|
1082
|
+
trueDom['visible'] = false;
|
|
1083
|
+
this.observeMap.set(domId, {
|
|
1084
|
+
instance: trueDom.cloneNode(true),
|
|
1085
|
+
isIntersecting: false
|
|
1086
|
+
});
|
|
1087
|
+
this.observe(trueDom);
|
|
1088
|
+
} else {
|
|
1089
|
+
var exposureDom = this.observeMap.get(elememtMapKey);
|
|
1090
|
+
if (exposureDom && trueDom !== exposureDom.instance) {
|
|
1091
|
+
// 有id,但实例变了
|
|
1092
|
+
this.unobserve(trueDom);
|
|
1093
|
+
this.observeMap["delete"](elememtMapKey);
|
|
1094
|
+
trueDom['observeId'] = domId;
|
|
1095
|
+
trueDom['visible'] = false;
|
|
1096
|
+
this.observeMap.set(domId, {
|
|
1097
|
+
instance: trueDom.cloneNode(true),
|
|
1098
|
+
isIntersecting: false
|
|
1099
|
+
});
|
|
1100
|
+
this.observe(trueDom);
|
|
1101
|
+
} else {
|
|
1102
|
+
if (trueDom['visible'] === true) return;
|
|
1103
|
+
this.handleBoundingExposure(trueDom);
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
};
|
|
1107
|
+
// 从曝光队列中移除
|
|
1108
|
+
Intersection.prototype.exposureRemove = function (dom) {
|
|
1109
|
+
if (this.observeMap.has(dom['observeId'])) {
|
|
1110
|
+
this.observeMap["delete"](dom['observeId']);
|
|
1111
|
+
this.unobserve(dom);
|
|
1112
|
+
dom['observeId'] = '';
|
|
1113
|
+
dom['visible'] = false;
|
|
1114
|
+
}
|
|
1115
|
+
};
|
|
1116
|
+
Intersection.prototype.exposureEvent = function (entry, intersectionRatio, elememtMapKey, exposureDom) {
|
|
1117
|
+
if (intersectionRatio >= this.ratio) {
|
|
1118
|
+
if (entry.target.style.opacity === '0' || entry.target.style.visibility === 'hidden') return;
|
|
1119
|
+
// if (entry.target.visible === true || exposureDom.exposured === true) return;
|
|
1120
|
+
if (exposureDom.exposured === true) return;
|
|
1121
|
+
if (exposureDom.startWait) {
|
|
1122
|
+
// 元素有定时器
|
|
1123
|
+
clearTimeout(exposureDom.startWait);
|
|
1124
|
+
}
|
|
1125
|
+
var event_1 = entry.target.getAttribute('data-exposure-event');
|
|
1126
|
+
this.eventHandle({
|
|
1127
|
+
eventType: 'dom',
|
|
1128
|
+
eventName: 'exposure',
|
|
1129
|
+
event: event_1,
|
|
1130
|
+
exposureType: this.getExposureType(entry.target)
|
|
1131
|
+
}, entry);
|
|
1132
|
+
entry.target.visible = true;
|
|
1133
|
+
entry.target.hasExposure = true;
|
|
1134
|
+
exposureDom.startTime = Date.now();
|
|
1135
|
+
exposureDom.exposured = true;
|
|
1136
|
+
} else {
|
|
1137
|
+
exposureDom.startTime = Date.now();
|
|
1138
|
+
exposureDom.exposured = false;
|
|
1139
|
+
// entry.target.visible = false;
|
|
1140
|
+
}
|
|
1141
|
+
this.observeMap.set(elememtMapKey, exposureDom);
|
|
1142
|
+
};
|
|
1143
|
+
Intersection.prototype.observe = function (dom) {
|
|
1144
|
+
this.instance && this.instance.observe(dom);
|
|
1145
|
+
};
|
|
1146
|
+
Intersection.prototype.unobserve = function (dom) {
|
|
1147
|
+
this.instance && this.instance.unobserve(dom);
|
|
1148
|
+
};
|
|
1149
|
+
Intersection.prototype.getExposureDomToExposure = function () {
|
|
1150
|
+
var _this = this;
|
|
1151
|
+
Array.prototype.forEach.call(document.querySelectorAll('[data-exposure]'), function (dom) {
|
|
1152
|
+
_this.handleBoundingExposure(dom);
|
|
1153
|
+
});
|
|
1154
|
+
};
|
|
1155
|
+
Intersection.prototype.visibilitychange = function () {
|
|
1156
|
+
var _this = this;
|
|
1157
|
+
var visTime = null;
|
|
1158
|
+
if (document.visibilityState === 'visible') {
|
|
1159
|
+
// 重置时间
|
|
1160
|
+
if (this.timeLimit) {
|
|
1161
|
+
visTime = setTimeout(function () {
|
|
1162
|
+
_this.getExposureDomToExposure();
|
|
1163
|
+
}, this.timeLimit);
|
|
1164
|
+
} else {
|
|
1165
|
+
this.getExposureDomToExposure();
|
|
1166
|
+
}
|
|
1167
|
+
} else {
|
|
1168
|
+
clearTimeout(visTime);
|
|
1169
|
+
// 页面隐藏了,可能是最小化,新开webview,等等
|
|
1170
|
+
this.customType = undefined;
|
|
1171
|
+
}
|
|
1172
|
+
};
|
|
1173
|
+
Intersection.prototype.handleBoundingExposure = function (dom) {
|
|
1174
|
+
// 处理非IntersectionObserver触发条件下的曝光
|
|
1175
|
+
if (this.getBoundingRatio(dom) >= this.ratio) {
|
|
1176
|
+
dom['visible'] = true;
|
|
1177
|
+
var event_2 = dom.getAttribute && dom.getAttribute('data-exposure-event');
|
|
1178
|
+
this.eventHandle({
|
|
1179
|
+
eventType: 'dom',
|
|
1180
|
+
eventName: 'exposure',
|
|
1181
|
+
event: event_2,
|
|
1182
|
+
exposureType: this.customType || ExposureTypeE.RESUME_FORM_BACK
|
|
1183
|
+
}, dom);
|
|
1184
|
+
}
|
|
1185
|
+
};
|
|
1186
|
+
Intersection.prototype.getBoundingRatio = function (dom) {
|
|
1187
|
+
var _a = dom.getBoundingClientRect(),
|
|
1188
|
+
top = _a.top,
|
|
1189
|
+
left = _a.left,
|
|
1190
|
+
width = _a.width,
|
|
1191
|
+
height = _a.height;
|
|
1192
|
+
var viewportWidth = window.innerWidth || document.documentElement.clientWidth;
|
|
1193
|
+
var viewportHeight = window.innerHeight || document.documentElement.clientHeight;
|
|
1194
|
+
var elementArea = width * height;
|
|
1195
|
+
if (top >= 0 && left >= 0 && top <= viewportHeight && left <= viewportWidth) {
|
|
1196
|
+
// 首先判断元素是否能看见
|
|
1197
|
+
var viewWidth = width > viewportWidth ? viewportWidth : width;
|
|
1198
|
+
var viewHeight = viewportHeight - top;
|
|
1199
|
+
var viewArea = viewWidth * viewHeight;
|
|
1200
|
+
return viewArea / elementArea;
|
|
1201
|
+
}
|
|
1202
|
+
return 0;
|
|
1203
|
+
};
|
|
1204
|
+
Intersection.prototype.getExposureType = function (target) {
|
|
1205
|
+
if (target.visible) {
|
|
1206
|
+
// 多页面的重复曝光
|
|
1207
|
+
if (target.backStatus !== false && (this.backStatus || this.customType || window.performance.navigation.type === 2)) {
|
|
1208
|
+
this.exposureType = ExposureTypeE.RESUME_FORM_PAGE;
|
|
1209
|
+
} else {
|
|
1210
|
+
this.exposureType = ExposureTypeE.LIFECYCLE_SHOW_NEW;
|
|
1211
|
+
}
|
|
1212
|
+
} else {
|
|
1213
|
+
if (target.hasExposure) {
|
|
1214
|
+
// 单页面的重复
|
|
1215
|
+
if (target.backStatus !== false && (this.backStatus || this.customType || window.performance.navigation.type === 2)) {
|
|
1216
|
+
this.exposureType = ExposureTypeE.RESUME_FORM_PAGE;
|
|
1217
|
+
} else {
|
|
1218
|
+
this.exposureType = ExposureTypeE.EXPOSURE_ONCE;
|
|
1219
|
+
}
|
|
1220
|
+
} else {
|
|
1221
|
+
if (target.backStatus !== false && (this.backStatus || this.customType || window.performance.navigation.type === 2)) {
|
|
1222
|
+
this.exposureType = ExposureTypeE.RESUME_FORM_PAGE;
|
|
1223
|
+
} else {
|
|
1224
|
+
this.exposureType = ExposureTypeE.EXPOSURE_ONCE;
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
// this.exposureType = ExposureTypeE.EXPOSURE_ONCE
|
|
1228
|
+
}
|
|
1229
|
+
return this.exposureType;
|
|
1230
|
+
};
|
|
1231
|
+
Intersection.prototype.addListen = function () {
|
|
1232
|
+
var _this = this;
|
|
1233
|
+
this.collect.on('set-exposure-type', function (type) {
|
|
1234
|
+
_this.customType = type;
|
|
1235
|
+
});
|
|
1236
|
+
var change = this.visibilitychange.bind(this);
|
|
1237
|
+
addListener(document, 'visibilitychange', change, false);
|
|
1238
|
+
window.addEventListener("hashchange", function (hash) {
|
|
1239
|
+
var current = _this.hashMap.get(hash.oldURL);
|
|
1240
|
+
if (current && location.href === current.prev) {
|
|
1241
|
+
_this.backStatus = true;
|
|
1242
|
+
_this.exposureType = ExposureTypeE.RESUME_FORM_PAGE;
|
|
1243
|
+
} else {
|
|
1244
|
+
_this.backStatus = false;
|
|
1245
|
+
_this.hashMap.set(location.href, {
|
|
1246
|
+
current: hash.newURL,
|
|
1247
|
+
prev: hash.oldURL
|
|
1248
|
+
});
|
|
1249
|
+
}
|
|
1250
|
+
});
|
|
1251
|
+
if (this.collect.bridgeReport) {
|
|
1252
|
+
var oldPushState_1 = window.history.pushState;
|
|
1253
|
+
history.pushState = function (state) {
|
|
1254
|
+
var args = [];
|
|
1255
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
1256
|
+
args[_i - 1] = arguments[_i];
|
|
1257
|
+
}
|
|
1258
|
+
if (typeof history['onpushstate'] === 'function') {
|
|
1259
|
+
history['onpushstate']({
|
|
1260
|
+
state: state
|
|
1261
|
+
});
|
|
1262
|
+
}
|
|
1263
|
+
var ret = oldPushState_1.call.apply(oldPushState_1, __spread([history, state], args));
|
|
1264
|
+
_this.lastState = state;
|
|
1265
|
+
return ret;
|
|
1266
|
+
};
|
|
1267
|
+
var oldReplaceState_1 = history.replaceState;
|
|
1268
|
+
history.replaceState = function (state) {
|
|
1269
|
+
var args = [];
|
|
1270
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
1271
|
+
args[_i - 1] = arguments[_i];
|
|
1272
|
+
}
|
|
1273
|
+
if (typeof history['onreplacestate'] === 'function') {
|
|
1274
|
+
history['onreplacestate']({
|
|
1275
|
+
state: state
|
|
1276
|
+
});
|
|
1277
|
+
}
|
|
1278
|
+
var ret = oldReplaceState_1.call.apply(oldReplaceState_1, __spread([history, state], args));
|
|
1279
|
+
_this.lastState = state;
|
|
1280
|
+
return ret;
|
|
1281
|
+
};
|
|
1282
|
+
} else {
|
|
1283
|
+
this.collect.on('STATE_CHANGE', function (state) {
|
|
1284
|
+
_this.lastState = state;
|
|
1285
|
+
});
|
|
1286
|
+
}
|
|
1287
|
+
window.addEventListener('popstate', function (state) {
|
|
1288
|
+
var currentState = state.state;
|
|
1289
|
+
if (currentState && currentState.current && _this.lastState && _this.lastState.back && currentState.current === _this.lastState.back) {
|
|
1290
|
+
// 用户点击了“退回”按钮
|
|
1291
|
+
_this.backStatus = true;
|
|
1292
|
+
_this.exposureType = ExposureTypeE.RESUME_FORM_PAGE;
|
|
1293
|
+
} else {
|
|
1294
|
+
_this.backStatus = false;
|
|
1295
|
+
}
|
|
1296
|
+
});
|
|
1297
|
+
return function () {
|
|
1298
|
+
removeListener(document, 'visibilitychange', change, false);
|
|
1299
|
+
};
|
|
1300
|
+
};
|
|
1301
|
+
Intersection._observer_instance = null;
|
|
1302
|
+
Intersection._observer_map = new Map();
|
|
1303
|
+
return Intersection;
|
|
1304
|
+
}();
|
|
1305
|
+
|
|
1306
|
+
// Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
|
|
1307
|
+
var Exposure = /** @class */function () {
|
|
1308
|
+
function Exposure(collect, config, eventHandle) {
|
|
1309
|
+
if (!config.autotrack || !config.autotrack.exposure) return;
|
|
1310
|
+
this._intersection = new Intersection(collect, config, eventHandle);
|
|
1311
|
+
this._observer = new Observer(this._intersection, config);
|
|
1312
|
+
if (this._intersection && this._observer) {
|
|
1313
|
+
this.initObserver();
|
|
1314
|
+
} else {
|
|
1315
|
+
console.log('your browser version cannot support exposure event, please update~');
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
Exposure.prototype.initObserver = function () {
|
|
1319
|
+
var self = this;
|
|
1320
|
+
Array.prototype.forEach.call(document.querySelectorAll('[data-exposure]'), function (dom) {
|
|
1321
|
+
self._intersection.exposureAdd(dom, 'intersect');
|
|
1322
|
+
});
|
|
1323
|
+
};
|
|
1324
|
+
return Exposure;
|
|
1325
|
+
}();
|
|
1326
|
+
|
|
1327
|
+
var ScrollTypeE;
|
|
1328
|
+
(function (ScrollTypeE) {
|
|
1329
|
+
ScrollTypeE[ScrollTypeE["SCROLL_UP"] = 1] = "SCROLL_UP";
|
|
1330
|
+
ScrollTypeE[ScrollTypeE["SCROLL_DOWN"] = 2] = "SCROLL_DOWN";
|
|
1331
|
+
ScrollTypeE[ScrollTypeE["SCROLL_LEFT"] = 3] = "SCROLL_LEFT";
|
|
1332
|
+
ScrollTypeE[ScrollTypeE["SCROLL_RIGHT"] = 4] = "SCROLL_RIGHT";
|
|
1333
|
+
ScrollTypeE[ScrollTypeE["NOT_SCROLL"] = -1] = "NOT_SCROLL";
|
|
1334
|
+
})(ScrollTypeE || (ScrollTypeE = {}));
|
|
1335
|
+
var ScrollSlide = /** @class */function () {
|
|
1336
|
+
function ScrollSlide(config, eventHandle) {
|
|
1337
|
+
this.distance = 30;
|
|
1338
|
+
if (!config.autotrack || !config.autotrack.scroll) return;
|
|
1339
|
+
if (config.autotrack.scroll.distance) {
|
|
1340
|
+
this.distance = config.autotrack.scroll.distance;
|
|
1341
|
+
}
|
|
1342
|
+
this.eventHandle = eventHandle;
|
|
1343
|
+
this.addLinstenr();
|
|
1344
|
+
this.mutation();
|
|
1345
|
+
}
|
|
1346
|
+
ScrollSlide.prototype.addLinstenr = function () {
|
|
1347
|
+
var _this = this;
|
|
1348
|
+
Array.prototype.forEach.call(document.querySelectorAll('[data-scroll]'), function (dom) {
|
|
1349
|
+
_this.scrollHandle(dom);
|
|
1350
|
+
});
|
|
1351
|
+
};
|
|
1352
|
+
ScrollSlide.prototype.mutation = function () {
|
|
1353
|
+
var _this = this;
|
|
1354
|
+
if (window['MutationObserver']) {
|
|
1355
|
+
try {
|
|
1356
|
+
var instance = new MutationObserver(function (mutations) {
|
|
1357
|
+
mutations.forEach(function (mutation) {
|
|
1358
|
+
// 更新dom节点可能是update,只是属性改变,需要监听
|
|
1359
|
+
// if (mutation.type === 'attributes') {
|
|
1360
|
+
// this.attributeChangeObserve(mutation);
|
|
1361
|
+
// }
|
|
1362
|
+
// dom节点变化
|
|
1363
|
+
if (mutation.type === 'childList') {
|
|
1364
|
+
_this.modifyNodeObserve(mutation);
|
|
1365
|
+
}
|
|
1366
|
+
});
|
|
1367
|
+
});
|
|
1368
|
+
if (!document || !document.body) {
|
|
1369
|
+
console.warn('please use sdk api init after body element');
|
|
1370
|
+
return;
|
|
1371
|
+
}
|
|
1372
|
+
instance.observe(document.body, {
|
|
1373
|
+
childList: true,
|
|
1374
|
+
attributes: true,
|
|
1375
|
+
subtree: true,
|
|
1376
|
+
attributeOldValue: false
|
|
1377
|
+
});
|
|
1378
|
+
} catch (e) {
|
|
1379
|
+
console.log('your browser cannot support MutationObserver');
|
|
1380
|
+
}
|
|
1381
|
+
} else {
|
|
1382
|
+
console.log('your browser cannot support MutationObserver');
|
|
1383
|
+
}
|
|
1384
|
+
};
|
|
1385
|
+
// 监听dom属性变化添加或删除节点曝光监听
|
|
1386
|
+
// attributeChangeObserve(mutation) {
|
|
1387
|
+
// const dom = mutation.target;
|
|
1388
|
+
// if (dom.hasAttribute('data-scroll')) {
|
|
1389
|
+
// this.scrollHandle(mutation);
|
|
1390
|
+
// }
|
|
1391
|
+
// }
|
|
1392
|
+
// 监听dom变化添加或删除节点曝光监听
|
|
1393
|
+
ScrollSlide.prototype.modifyNodeObserve = function (mutation) {
|
|
1394
|
+
var _this = this;
|
|
1395
|
+
// 不能为文本节点&&要有auto-exp属性
|
|
1396
|
+
Array.prototype.forEach.call(mutation.addedNodes, function (node) {
|
|
1397
|
+
if (node.nodeType === 1 && node.hasAttribute('data-scroll')) {
|
|
1398
|
+
_this.scrollHandle(node);
|
|
1399
|
+
}
|
|
1400
|
+
_this.mapChild(node, _this.scrollHandle.bind(_this));
|
|
1401
|
+
});
|
|
1402
|
+
// 遍历子节点的删除
|
|
1403
|
+
Array.prototype.forEach.call(mutation.removedNodes, function (node) {
|
|
1404
|
+
if (node.nodeType === 1 && node.hasAttribute('data-scroll')) {
|
|
1405
|
+
_this.scrollHandle(node, 'remove');
|
|
1406
|
+
}
|
|
1407
|
+
_this.mapChild(node, _this.scrollHandle.bind(_this));
|
|
1408
|
+
});
|
|
1409
|
+
};
|
|
1410
|
+
// 递归后代节点
|
|
1411
|
+
ScrollSlide.prototype.mapChild = function (node, action) {
|
|
1412
|
+
var _this = this;
|
|
1413
|
+
if (node.nodeType !== 1) {
|
|
1414
|
+
return;
|
|
1415
|
+
}
|
|
1416
|
+
if (!node.children.length) {
|
|
1417
|
+
return;
|
|
1418
|
+
}
|
|
1419
|
+
Array.prototype.forEach.call(node.children, function (item) {
|
|
1420
|
+
if (item.nodeType === 1 && item.hasAttribute('data-scroll')) {
|
|
1421
|
+
action(item);
|
|
1422
|
+
}
|
|
1423
|
+
_this.mapChild(item, action);
|
|
1424
|
+
});
|
|
1425
|
+
};
|
|
1426
|
+
ScrollSlide.prototype.scrollHandle = function (dom, type) {
|
|
1427
|
+
var _this = this;
|
|
1428
|
+
try {
|
|
1429
|
+
var preScrollTop_1 = dom.scrollTop;
|
|
1430
|
+
var preScrollLeft_1 = dom.scrollLeft;
|
|
1431
|
+
var timer_1 = null;
|
|
1432
|
+
var $offsetX_1 = 0;
|
|
1433
|
+
var $offsetY_1 = 0;
|
|
1434
|
+
var scrollFn_1 = function scrollFn_1(e) {
|
|
1435
|
+
clearTimeout(timer_1);
|
|
1436
|
+
timer_1 = setTimeout(function () {
|
|
1437
|
+
var finalScrollTop = dom.scrollTop;
|
|
1438
|
+
var finalScrollLeft = dom.scrollLeft;
|
|
1439
|
+
var event = e.target && e.target['getAttribute']('data-scroll-event') || '';
|
|
1440
|
+
if (scrollDirection === 1 || scrollDirection === 2) {
|
|
1441
|
+
if (finalScrollTop === currentScrollTop) {
|
|
1442
|
+
// 滚动停止了
|
|
1443
|
+
$offsetY_1 = currentScrollTop - preScrollTop_1;
|
|
1444
|
+
if (Math.abs($offsetY_1) >= _this.distance && _this.distance) {
|
|
1445
|
+
_this.eventHandle({
|
|
1446
|
+
eventType: 'dom',
|
|
1447
|
+
eventName: 'scroll',
|
|
1448
|
+
event: event,
|
|
1449
|
+
params: {
|
|
1450
|
+
$direction: scrollDirection,
|
|
1451
|
+
$offsetY: $offsetY_1,
|
|
1452
|
+
$offsetX: $offsetX_1
|
|
1453
|
+
}
|
|
1454
|
+
}, e);
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
} else if (scrollDirection === 3 || scrollDirection === 4) {
|
|
1458
|
+
if (finalScrollLeft === currentScrollLeft) {
|
|
1459
|
+
// 滚动停止了
|
|
1460
|
+
$offsetX_1 = currentScrollLeft - preScrollLeft_1;
|
|
1461
|
+
if (Math.abs($offsetX_1) >= _this.distance && _this.distance) {
|
|
1462
|
+
_this.eventHandle({
|
|
1463
|
+
eventType: 'dom',
|
|
1464
|
+
eventName: 'scroll',
|
|
1465
|
+
event: event,
|
|
1466
|
+
params: {
|
|
1467
|
+
$direction: scrollDirection,
|
|
1468
|
+
$offsetY: $offsetY_1,
|
|
1469
|
+
$offsetX: $offsetX_1
|
|
1470
|
+
}
|
|
1471
|
+
}, e);
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
preScrollTop_1 = currentScrollTop;
|
|
1476
|
+
preScrollLeft_1 = currentScrollLeft;
|
|
1477
|
+
}, 100);
|
|
1478
|
+
var currentScrollTop = dom.scrollTop;
|
|
1479
|
+
var currentScrollLeft = dom.scrollLeft;
|
|
1480
|
+
var scrollDirection = ScrollTypeE.NOT_SCROLL;
|
|
1481
|
+
if (currentScrollTop > preScrollTop_1) {
|
|
1482
|
+
scrollDirection = ScrollTypeE.SCROLL_DOWN;
|
|
1483
|
+
} else if (currentScrollTop < preScrollTop_1) {
|
|
1484
|
+
scrollDirection = ScrollTypeE.SCROLL_UP;
|
|
1485
|
+
} else {
|
|
1486
|
+
if (currentScrollLeft > preScrollLeft_1) {
|
|
1487
|
+
scrollDirection = ScrollTypeE.SCROLL_LEFT;
|
|
1488
|
+
} else if (currentScrollLeft < preScrollLeft_1) {
|
|
1489
|
+
scrollDirection = ScrollTypeE.SCROLL_RIGHT;
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
};
|
|
1493
|
+
if (type === 'remove') {
|
|
1494
|
+
dom.removeEventListener('scroll', scrollFn_1, false);
|
|
1495
|
+
} else {
|
|
1496
|
+
dom.addEventListener('scroll', scrollFn_1, false);
|
|
1497
|
+
}
|
|
1498
|
+
return function () {
|
|
1499
|
+
dom.removeEventListener('scroll', scrollFn_1, false);
|
|
1500
|
+
};
|
|
1501
|
+
} catch (e) {
|
|
1502
|
+
console.warn('scroll event error', JSON.stringify(e));
|
|
1503
|
+
}
|
|
1504
|
+
};
|
|
1505
|
+
return ScrollSlide;
|
|
1506
|
+
}();
|
|
1507
|
+
|
|
1508
|
+
// Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
|
|
1509
|
+
var defaultOpt = {
|
|
1510
|
+
hashTag: false,
|
|
1511
|
+
impr: false
|
|
1512
|
+
};
|
|
1513
|
+
var AutoTrack = /** @class */function () {
|
|
1514
|
+
function AutoTrack() {}
|
|
1515
|
+
AutoTrack.prototype.apply = function (collect, config) {
|
|
1516
|
+
this.autoTrackStart = false;
|
|
1517
|
+
this.collect = collect;
|
|
1518
|
+
this.config = config;
|
|
1519
|
+
if (!config.autotrack) return;
|
|
1520
|
+
if (config.autotrack && config.autotrack.collect_url) {
|
|
1521
|
+
if (!config.autotrack.collect_url()) return;
|
|
1522
|
+
}
|
|
1523
|
+
var Types = collect.Types;
|
|
1524
|
+
this.ready(Types.Autotrack);
|
|
1525
|
+
this.collect.emit(Types.AutotrackReady);
|
|
1526
|
+
};
|
|
1527
|
+
AutoTrack.prototype.ready = function (name) {
|
|
1528
|
+
this.collect.set(name);
|
|
1529
|
+
var options = this.config.autotrack;
|
|
1530
|
+
options = typeof options === 'object' ? options : {};
|
|
1531
|
+
options = Object.assign(defaultOpt, options);
|
|
1532
|
+
this.destroyed = false;
|
|
1533
|
+
this.options = options;
|
|
1534
|
+
this.Config = new Config(defaultConfig, this.options);
|
|
1535
|
+
this.Exposure = new Exposure(this.collect, this.config, this.handle.bind(this));
|
|
1536
|
+
this.Scroll = new ScrollSlide(this.config, this.handle.bind(this));
|
|
1537
|
+
this.Listener = new Listener(options, this.collect, this.Config);
|
|
1538
|
+
this.EventHandle = new EventHandle(this.config, options);
|
|
1539
|
+
this.Request = new Request(this.collect);
|
|
1540
|
+
this.autoTrackStart = true;
|
|
1541
|
+
this.init();
|
|
1542
|
+
// readyToLoadEditor(this, this.config)
|
|
1543
|
+
};
|
|
1544
|
+
AutoTrack.prototype.init = function () {
|
|
1545
|
+
this.Listener.init(this.handle.bind(this));
|
|
1546
|
+
};
|
|
1547
|
+
AutoTrack.prototype.handle = function (eventInfo, data) {
|
|
1548
|
+
try {
|
|
1549
|
+
if (this.config.autotrack.collect_url) {
|
|
1550
|
+
if (!this.config.autotrack.collect_url()) return;
|
|
1551
|
+
}
|
|
1552
|
+
} catch (e) {}
|
|
1553
|
+
if (this.collect.sdkStop) return;
|
|
1554
|
+
if (eventInfo.eventType === 'dom') {
|
|
1555
|
+
this.handleDom(eventInfo, data);
|
|
1556
|
+
}
|
|
1557
|
+
};
|
|
1558
|
+
AutoTrack.prototype.handleDom = function (eventInfo, data) {
|
|
1559
|
+
try {
|
|
1560
|
+
var eventName = eventInfo.eventName;
|
|
1561
|
+
if (eventName === 'click' || eventName === 'change' || eventName === 'submit') {
|
|
1562
|
+
var handleResult = this.EventHandle.handleEvent(data, eventName);
|
|
1563
|
+
handleResult && this.Request.send({
|
|
1564
|
+
eventType: 'custom'
|
|
1565
|
+
}, handleResult);
|
|
1566
|
+
} else if (eventName === 'page_view' || eventName === 'page_statistics') {
|
|
1567
|
+
var pageData = void 0;
|
|
1568
|
+
if (eventName === 'page_view') {
|
|
1569
|
+
pageData = this.EventHandle.handleViewEvent(data);
|
|
1570
|
+
} else {
|
|
1571
|
+
pageData = this.EventHandle.handleStatisticsEvent(data);
|
|
1572
|
+
}
|
|
1573
|
+
pageData && this.Request.send({
|
|
1574
|
+
eventType: 'custom'
|
|
1575
|
+
}, pageData);
|
|
1576
|
+
} else if (eventName === 'beat') {
|
|
1577
|
+
var beatData = this.EventHandle.handleBeadtEvent(data);
|
|
1578
|
+
var eventSend = eventInfo.eventSend;
|
|
1579
|
+
beatData && this.Request.send({
|
|
1580
|
+
eventType: 'custom',
|
|
1581
|
+
eventSend: eventSend
|
|
1582
|
+
}, beatData);
|
|
1583
|
+
} else if (eventName === 'exposure') {
|
|
1584
|
+
var handleResult = this.EventHandle.handleExposureEvent(eventInfo, data);
|
|
1585
|
+
handleResult && this.Request.send({
|
|
1586
|
+
eventType: 'custom'
|
|
1587
|
+
}, handleResult);
|
|
1588
|
+
} else if (eventName === 'scroll') {
|
|
1589
|
+
var handleResult = this.EventHandle.handleScrollEvent(eventInfo, data);
|
|
1590
|
+
handleResult && this.Request.send({
|
|
1591
|
+
eventType: 'custom'
|
|
1592
|
+
}, handleResult);
|
|
1593
|
+
}
|
|
1594
|
+
} catch (e) {
|
|
1595
|
+
console.log("handel dom event error " + JSON.stringify(e));
|
|
1596
|
+
}
|
|
1597
|
+
};
|
|
1598
|
+
AutoTrack.prototype.destroy = function () {
|
|
1599
|
+
if (!this.autoTrackStart) {
|
|
1600
|
+
return console.warn('engine is undefined, make sure you have called autoTrack.start()');
|
|
1601
|
+
}
|
|
1602
|
+
this.autoTrackStart = false;
|
|
1603
|
+
this.Listener.removeListener();
|
|
1604
|
+
};
|
|
1605
|
+
return AutoTrack;
|
|
1606
|
+
}();
|
|
1607
|
+
|
|
1608
|
+
export { AutoTrack as default };
|