@lylb/tracker
Version:
10 lines (9 loc) • 12.2 kB
JavaScript
/**
* [@lylb/tracker]{@link https://git.01lb.vip/front-end-lib/zo-trackers}
* @description 零一数据上报工具
* @namespace Tracker
* @version 1.0.4
* @author wangchunhai818@vip.qq.com
* @copyright 零一数据上报工具 [2024-2024]
* @license MIT
*/import{md5 as e}from"js-md5";var t=function(){return t=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},t.apply(this,arguments)};function n(e,t,n,r){return new(n||(n=Promise))((function(o,i){function a(e){try{c(r.next(e))}catch(e){i(e)}}function s(e){try{c(r.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,s)}c((r=r.apply(e,t||[])).next())}))}function r(e,t){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(s){return function(c){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,s[0]&&(a=0)),a;)try{if(n=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return a.label++,{value:s[1],done:!1};case 5:a.label++,r=s[1],s=[0];continue;case 7:s=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){a=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){a.label=s[1];break}if(6===s[0]&&a.label<o[1]){a.label=o[1],o=s;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(s);break}o[2]&&a.ops.pop(),a.trys.pop();continue}s=t.call(e,a)}catch(e){s=[6,e],r=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,c])}}}"function"==typeof SuppressedError&&SuppressedError;var o=function(e){return null!==e&&"object"==typeof e&&"[object Object]"===f.call(e)},i=function(e,t){if(void 0===t&&(t=new WeakMap),null==e)return e;if(e instanceof Date)return new Date(e);if(e instanceof RegExp)return new RegExp(e);if("object"!=typeof e)return e;if(t.has(e))return t.get(e);var n;for(var r in n=Array.isArray(e)?[]:Object.create(Object.getPrototypeOf(e)),t.set(e,n),e)c.call(e,r)&&(n[r]=i(e[r],t));return n},a=Object.prototype,s=Object.defineProperty,c=a.hasOwnProperty,u=Object.assign,f=a.toString,d=Object.keys,l=JSON.stringify,p=function(e){return"function"==typeof e},h=function(e,t,n){var r=t.shift();if(t.length){var o;if(!c.call(e,r)){if(void 0===n)return;e[r]={}}h(e[r],t,n)}else void 0===n?delete e[r]:(e[r]=n,p(n)&&(o=n,"[object AsyncFunction]"===f.call(o)||function(e){return p(e)&&p(e.then)&&p(e.catch)}(n)?e[r]="":s(e,r,{get:n,enumerable:!0,configurable:!0})))},v=function(){function e(e){void 0===e&&(e=2),this.tasks=[],this.runCount=0,this.maxExecution=e}return e.prototype.add=function(e){var t=this;return new Promise((function(n,r){t.tasks.push({task:e,resolve:n,reject:r}),t._run()}))},e.prototype._run=function(){for(var e=this;this.runCount<this.maxExecution&&this.tasks.length>0;){var t=this.tasks.shift(),n=t.task,r=t.resolve,o=t.reject;this.runCount++,n().then(r,o).finally((function(){e.runCount--,e._run()}))}},e}(),w=function(){function e(e){this.$data={},o(e)&&(this.$data=i(e))}return e.prototype.get=function(){for(var e=this,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return t.length>0?t.reduce((function(t,n){var r=e.$get(e.$data,n);return void 0!==r&&(t[n]=r),t}),{}):this.$cloneData()},e.prototype.$get=function(e,t){if(!o(e)){var n=t.split(".").filter((function(e){return e}));if(0!==n.length){var r=n.shift();return c.call(e,r)?n.length?this.$get(e[r],n.join(".")):e[r]:void 0}}},e.prototype.set=function(e,t){var n=e.split(".").filter((function(e){return e}));if(!n.length)return this;h(this.$data,n,t)},e.prototype.$cloneData=function(){return i(this.$data)},e}(),g=function(){function e(e){var t,n=this;this._reBlockId=10,this._version="1.0.4",this._final=["app_id","app_secret","t","pages","sign","device_info","block_id"],this.create=(t=!0,function(e){if(!t)throw new Error("The create method can only be called once.");if(t=!1,!e.appId||!e.appSecret)throw new Error("The appId and appSecret is required and must be a non-empty string.");n.params.set("app_id",e.appId),n.params.set("app_secret",e.appSecret),n.params.set("project",e.project),n.params.set("version",e.version||n._version),n.$log("version",n._version)}),this._task=new v(e.taskCount||2),this.params=new w,this._enviroment=e.enviroment,this._requestUrl=e.url,this._maxRequest=e.maxRequest||5,this._debuger=e.debuger||!1,this.mergeFinal(e.final||[]),this.$setDeviceInfo(),this.$watchJsError(!!e.isAddJsError),this.$watchPageStay(!!e.isAddPageStay)}return e.prototype.$setDeviceInfo=function(){return n(this,void 0,void 0,(function(){var e,t;return r(this,(function(n){switch(n.label){case 0:return[4,this._enviroment.getDeviceInfo()];case 1:return e=n.sent(),this.params.set("device_info",e||{}),[4,this._enviroment.getDeviceFingerprintId()];case 2:return t=n.sent(),this.params.set("block_id",t),[2]}}))}))},e.prototype.$watchJsError=function(e){var t=this;e&&this._enviroment.watchJsError((function(e){var n=t.$createParams({data:e,event:"js_error",event_name:"javascript异常捕获"});t.addTask(n)}))},e.prototype.$watchPageStay=function(e){var t=this;e&&(this._enviroment.watchRouteChange((function(e){d(e).forEach((function(n){t.params.set("pages.".concat(n),e[n])}))})),setInterval((function(){t.addTask(t.params)}),2e3))},e.prototype.register=function(e){var t=this;o(e)&&d(e).forEach((function(n){t._final.includes(n)?t.$log("register-warn","The ".concat(n," field is not allowed to be modified."),"warn"):t.params.set(n,e[n])}))},e.prototype.clearRegister=function(e){var t=this;if(!Array.isArray(e))throw new Error("The keys must be an array");e.forEach((function(e){t._final.includes(e)?t.$log("clearRegister-warn","The ".concat(e," field is not allowed to be modified."),"warn"):t.params.set(e)}))},e.prototype.requestError=function(e){var t=this.$createParams({data:e,event:"req_error",event_name:"接口请求异常"});this.addTask(t)},e.prototype.event=function(e){for(var t=[],n=1;n<arguments.length;n++)t[n-1]=arguments[n];var r=this.$createParams({event:e}),o=t.shift();"string"==typeof o?(r.set("event_name",o),t[0]&&r.set("data",t[0])):r.set("data",o),this.addTask(r)},e.prototype.setProfile=function(e){o(e)&&this.params.set("users",u({},this.params.get("users"),e))},e.prototype.addTask=function(e){var t=new w(e.get());t.set("t",this.$time),t.set("sign"),this.$send(t.get())},e.prototype.$send=function(e,t){var n=this;void 0===t&&(t=1),1===t&&this.$log("request-send",e),this._task.add(this._enviroment.request.bind(this,this._requestUrl,e)).catch((function(r){n.$log("request-error",r),++t>n._maxRequest||n.$send(e,t)}))},e.prototype.mergeFinal=function(e){var t;this._final=(t=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return e.reduce((function(e,t){return e.concat(t)}),[])}(this._final,e),Array.from(new Set(t)))},e.prototype.$createParams=function(e){var t=new w(this.params.get());return d(e).forEach((function(n){t.set(n,e[n])})),t},Object.defineProperty(e.prototype,"$time",{get:function(){return(new Date).getTime()},enumerable:!1,configurable:!0}),e.prototype.$log=function(e,t,n){if(void 0===n&&(n="log"),!this._debuger)return this.$log=function(){};o(t)&&(t=l(t,null,4)),console[n]("zo-tracker:".concat(e),t)},e.prototype.getBlockId=function(){var e=this;return new Promise((function(t){if(--e._reBlockId<0)return e._reBlockId=10,t("");var o=e.params.get("block_id");c.call(o,"block_id")?t(o.block_id):setTimeout((function(){return n(e,void 0,void 0,(function(){var e;return r(this,(function(n){switch(n.label){case 0:return[4,this.getBlockId()];case 1:return e=n.sent(),t(e),[2]}}))}))}),100)}))},e}(),m=function(){return new Promise((function(e,n){try{var r=wx.getDeviceInfo(),o=wx.getAppBaseInfo(),i=wx.getWindowInfo();e(t(t(t({},r),o),i))}catch(e){n(e)}}))},y=function(t){return new Promise((function(n){var r=t.split(".").map((function(e){return("000"+e).slice(-4)})).join("");if(Number(r)<200160001)n(e(r));else{var o=wx.createOffscreenCanvas({width:300,height:300,type:"2d"}).getContext("2d");o.font="12px sans-serif",o.textAlign="left",o.textBaseline="top",o.fillStyle="#ff0000",o.fillText("FINGERPRINTID",0,10),o.fillStyle="#ff00ff",o.fillRect(0,90,100,10),n(e(o.getImageData(0,0,300,300).data))}}))},b=Object.freeze({__proto__:null,getDeviceFingerprintId:function(){return new Promise((function(t){return n(void 0,void 0,void 0,(function(){var n,o,i,a,s,c,u,f,d,p,h,v,w,g,b,_,E,x,k,$,P,I,j,S,T,D;return r(this,(function(r){switch(r.label){case 0:return[4,m()];case 1:return n=r.sent(),o=n.brand,i=void 0===o?"":o,a=n.memorySize,s=void 0===a?2048:a,c=n.model,u=void 0===c?"":c,f=n.safeArea,d=void 0===f?{}:f,p=n.screenTop,h=void 0===p?46:p,v=n.statusBarHeight,w=void 0===v?20:v,g=n.windowHeight,b=void 0===g?1334:g,_=n.windowWidth,E=void 0===_?750:_,x=n.version,k=void 0===x?"":x,$=n.system,P=void 0===$?"":$,I=n.SDKVersion,j=void 0===I?"2.6.1":I,S=e(l(i)+l(s)+l(u)+l(P)+l(k)),T=e(l(d)+l(h)+l(w)+l(b)+l(E)),[4,y(j)];case 2:return D=r.sent(),t(e(S+T+D)),[2]}}))}))}))},getDeviceInfo:m,request:function(e,t){return new Promise((function(n,r){wx.request({url:e,method:"POST",data:t,success:function(e){200===e.statusCode?n(e):r(e)},fail:function(e){r(e)}})}))},watchJsError:function(e){return wx.onError((function(t){e(t)})),wx.onPageNotFound((function(t){e(t)})),function(){wx.offError(),wx.offPageNotFound()}},watchRouteChange:function(e){return wx.onAppRoute((function(t){setTimeout((function(){e(t)}),5)})),function(){wx.offAppRoute()}}}),_=function(){return new Promise((function(e){var t=window.navigator;e({deviceInfo:{appName:t.appName,appCodeName:t.appCodeName,appVersion:t.appVersion,userAgent:t.userAgent,platform:t.platform,product:t.product,productSub:t.productSub,userAgentData:t.userAgentData,connection:t.connection,virtualKeyboard:t.virtualKeyboard,language:t.language}})}))},E=function(){return new Promise((function(t){var n=document.createElement("canvas");n.width=300,n.height=300;var r=n.getContext("2d");r.font="12px sans-serif",r.textAlign="left",r.textBaseline="top",r.fillStyle="#ff0000",r.fillText("FINGERPRINTID",0,10),r.fillStyle="#ff00ff",r.fillRect(0,90,100,10),t(e(r.getImageData(0,0,300,300).data))}))},x=Object.freeze({__proto__:null,getDeviceFingerprintId:function(){return new Promise((function(t){return n(void 0,void 0,void 0,(function(){var n,o,i,a,s,c,u,f,d,p,h,v,w,g,m,y,b,x,k,$,P,I,j,S;return r(this,(function(r){switch(r.label){case 0:return[4,_()];case 1:return n=r.sent(),o=n.brand,i=void 0===o?"":o,a=n.memorySize,s=void 0===a?2048:a,c=n.model,u=void 0===c?"":c,f=n.safeArea,d=void 0===f?{}:f,p=n.screenTop,h=void 0===p?46:p,v=n.statusBarHeight,w=void 0===v?20:v,g=n.windowHeight,m=void 0===g?1334:g,y=n.windowWidth,b=void 0===y?750:y,x=n.version,k=void 0===x?"":x,$=n.system,P=void 0===$?"":$,I=e(l(i)+l(s)+l(u)+l(P)+l(k)),j=e(l(d)+l(h)+l(w)+l(m)+l(b)),[4,E()];case 2:return S=r.sent(),t(e(I+j+S)),[2]}}))}))}))},getDeviceInfo:_,request:function(e,t){return new Promise((function(n,r){fetch(e,{method:"POST",headers:{"Content-Type":"application/json; charset=UTF-8"},body:l(t)}).then((function(e){200!==e.status?r(e):n(e)})).catch((function(e){r(e)}))}))},watchJsError:function(e){return window.addEventListener("error",e),window.addEventListener("unhandledrejection",e),window.addEventListener("unload",e),function(){window.removeEventListener("error",e),window.removeEventListener("unhandledrejection",e),window.removeEventListener("unload",e)}},watchRouteChange:function(e){return window.addEventListener("popstate",e),window.addEventListener("hashchange",e),window.addEventListener("replaceState",e),window.addEventListener("pushState",e),function(){window.removeEventListener("popstate",e),window.removeEventListener("hashchange",e),window.removeEventListener("replaceState",e),window.removeEventListener("pushState",e)}}});export{g as Tracker,b as weapp,x as web};