forked from oner-team/dt-stat-sdk-wx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdt-stat-sdk-wx.min.js
1 lines (1 loc) · 17.4 KB
/
dt-stat-sdk-wx.min.js
1
function e(e,t,r){if(e[t]){var n=e[t];e[t]=function(e){n.call(this,e),r.call(this,e,t)}}else e[t]=function(e){r.call(this,e,t)}}function t(e){this[a.para.name]=a;var t={};e&&e.path&&(t.$urlPath=e.path),t.$isFirstTime=!!_,e.scene=e.scene||"未取到值",t.$scene=o.getMPScene(e.scene),a._registerParams({$latestScene:t.$scene}),a.para.autoTrack&&a.para.autoTrack.appLaunch&&a.autoTrackCustom("appLaunch",t,"$WXMPLaunch")}function r(e){var t={};v=(new Date).getTime(),e&&e.path&&(t.$urlPath=e.path),e.scene=e.scene||"未取到值",t.$scene=o.getMPScene(e.scene),a._registerParams({$latestScene:t.$scene}),a.para.autoTrack&&a.para.autoTrack.appShow&&a.autoTrackCustom("appShow",t,"$WXMPShow")}function n(){var e=(new Date).getTime(),t={};t.$urlPath=o.getCurrentPath(),v&&e-v>0&&(e-v)/36e5<24&&(t.eventDuration=(e-v)/1e3),a.para.autoTrack&&a.para.autoTrack.appHide&&a.autoTrackCustom("appHide",t,"$WXMPHide")}var i=new RegExp("^[$]+"),o={},a={},s=require("./config.js");a.para=s;var c=Array.prototype,u=Function.prototype,p=Object.prototype,f=c.slice,l=p.toString,h=p.hasOwnProperty,g=["$WXMPLaunch","$WXMPShow","$WXMPHide","$WXMPViewScreen"],d={1001:"发现栏小程序主入口,“最近使用”列表",1005:"顶部搜索框的搜索结果页",1006:"发现栏小程序主入口搜索框的搜索结果页",1007:"单人聊天会话中的小程序消息卡片",1008:"群聊会话中的小程序消息卡片",1011:"扫描二维码",1012:"长按图片识别二维码",1013:"手机相册选取二维码",1014:"小程序模版消息",1017:"前往体验版的入口页",1019:"微信钱包",1020:"公众号 profile 页相关小程序列表",1022:"聊天顶部置顶小程序入口",1023:"安卓系统桌面图标",1024:"小程序 profile 页",1025:"扫描一维码",1026:"附近小程序列表",1027:"顶部搜索框搜索结果页“使用过的小程序”列表",1028:"我的卡包",1029:"卡券详情页",1030:"自动化测试下打开小程序",1031:"长按图片识别一维码",1032:"手机相册选取一维码",1034:"微信支付完成页",1035:"公众号自定义菜单",1036:"App 分享消息卡片",1037:"小程序打开小程序",1038:"从另一个小程序返回",1039:"摇电视",1042:"添加好友搜索框的搜索结果页",1043:"公众号模板消息",1044:"带 shareTicket 的小程序消息卡片(详情)",1045:"朋友圈广告",1046:"朋友圈广告详情页",1047:"扫描小程序码",1048:"长按图片识别小程序码",1049:"手机相册选取小程序码",1052:"卡券的适用门店列表",1053:"搜一搜的结果页",1054:"顶部搜索框小程序快捷入口",1056:"音乐播放器菜单",1057:"钱包中的银行卡详情页",1058:"公众号文章",1059:"体验版小程序绑定邀请页",1064:"微信连Wi-Fi状态栏",1067:"公众号文章广告",1068:"附近小程序列表广告",1069:"移动应用",1071:"钱包中的银行卡列表页",1072:"二维码收款页面",1073:"客服消息列表下发的小程序消息卡片",1074:"公众号会话下发的小程序消息卡片",1077:"摇周边",1078:"连Wi-Fi成功页",1079:"微信游戏中心",1081:"客服消息下发的文字链",1082:"公众号会话下发的文字链",1084:"朋友圈广告原生页",1089:"微信聊天主界面下拉",1090:"长按小程序右上角菜单唤出最近使用历史",1091:"公众号文章商品卡片",1092:"城市服务入口",1095:"小程序广告组件",1096:"聊天记录",1097:"微信支付签约页",1099:"页面内嵌插件",1102:"公众号 profile 页服务预览",1103:"发现栏小程序主入口,“我的小程序”列表",1104:"微信聊天主界面下拉,“我的小程序”栏"},m="直接打开",v=null,_=!1;a.libVersion="1.0.0";var y="object"==typeof y?y:{};if(y.info=function(){if(a.para.show_log&&"object"==typeof console&&console.log)try{return console.log.apply(console,arguments)}catch(e){console.log(arguments[0])}},function(){u.bind;var e=c.forEach,t=c.indexOf,r=Array.isArray,n={},i=o.each=function(t,r,i){if(null==t)return!1;if(e&&t.forEach===e)t.forEach(r,i);else if(t.length===+t.length){for(var o=0,a=t.length;o<a;o++)if(o in t&&r.call(i,t[o],o,t)===n)return!1}else for(var s in t)if(h.call(t,s)&&r.call(i,t[s],s,t)===n)return!1};o.logger=y,o.toCamelCase=function(e){return e.replace(/_([a-z])/g,function(e){return e[1].toUpperCase()})},o.extend=function(e){return i(f.call(arguments,1),function(t){for(var r in t)void 0!==t[r]&&(e[r]=t[r])}),e},o.extend2Lev=function(e){return i(f.call(arguments,1),function(t){for(var r in t)void 0!==t[r]&&(o.isObject(t[r])&&o.isObject(e[r])?o.extend(e[r],t[r]):e[r]=t[r])}),e},o.coverExtend=function(e){return i(f.call(arguments,1),function(t){for(var r in t)void 0!==t[r]&&void 0===e[r]&&(e[r]=t[r])}),e},o.isArray=r||function(e){return"[object Array]"===l.call(e)},o.isFunction=function(e){try{return/^\s*\bfunction\b/.test(e)}catch(e){return!1}},o.isArguments=function(e){return!(!e||!h.call(e,"callee"))},o.toArray=function(e){return e?e.toArray?e.toArray():o.isArray(e)?f.call(e):o.isArguments(e)?f.call(e):o.values(e):[]},o.values=function(e){var t=[];return null==e?t:(i(e,function(e){t[t.length]=e}),t)},o.include=function(e,r){var o=!1;return null==e?o:t&&e.indexOf===t?-1!=e.indexOf(r):(i(e,function(e){if(o||(o=e===r))return n}),o)}}(),o.trim=function(e){return e.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")},o.isObject=function(e){return"[object Object]"==l.call(e)&&null!=e},o.isEmptyObject=function(e){if(o.isObject(e)){for(var t in e)if(h.call(e,t))return!1;return!0}return!1},o.isUndefined=function(e){return void 0===e},o.isString=function(e){return"[object String]"==l.call(e)},o.isDate=function(e){return"[object Date]"==l.call(e)},o.isBoolean=function(e){return"[object Boolean]"==l.call(e)},o.isNumber=function(e){return"[object Number]"==l.call(e)&&/[\d\.]+/.test(String(e))},o.isJSONString=function(e){try{JSON.parse(e)}catch(e){return!1}return!0},o.decodeURIComponent=function(e){var t="";try{t=decodeURIComponent(e)}catch(r){t=e}return t},o.encodeDates=function(e){return o.each(e,function(t,r){o.isDate(t)?e[r]=o.formatDate(t):o.isObject(t)&&(e[r]=o.encodeDates(t))}),e},o.formatDate=function(e){function t(e){return e<10?"0"+e:e}return e.getFullYear()+"-"+t(e.getMonth()+1)+"-"+t(e.getDate())+" "+t(e.getHours())+":"+t(e.getMinutes())+":"+t(e.getSeconds())+"."+t(e.getMilliseconds())},o.searchObjDate=function(e){o.isObject(e)&&o.each(e,function(t,r){o.isObject(t)?o.searchObjDate(e[r]):o.isDate(t)&&(e[r]=o.formatDate(t))})},o.formatString=function(e){return e.length>a.para.max_string_length?(y.info("字符串长度超过限制,已经做截取--"+e),e.slice(0,a.para.max_string_length)):e},o.searchObjString=function(e){o.isObject(e)&&o.each(e,function(t,r){o.isObject(t)?o.searchObjString(e[r]):o.isString(t)&&(e[r]=o.formatString(t))})},o.unique=function(e){for(var t,r=[],n={},i=0;i<e.length;i++)(t=e[i])in n||(n[t]=!0,r.push(t));return r},o.strip_dt_properties=function(e){if(!o.isObject(e))return e;var t=o.strip_empty_properties(e);return o.each(t,function(e,r){if(o.isArray(e)){var n=[];o.each(e,function(t){o.isString(t)||o.isNumber(t)?n.push(t):y.info("您的数据-",e,"的数组里的值必须是字符串,已经将其删除")}),0!==n.length?t[r]=n:(delete t[r],y.info("已经删除空的数组"))}o.isString(e)||o.isNumber(e)||o.isDate(e)||o.isBoolean(e)||o.isArray(e)||(y.info("您的数据-",e,"-格式不满足要求,我们已经将其删除"),delete t[r])}),t},o.strip_empty_properties=function(e){var t={};return o.each(e,function(e,r){null!=e&&(t[r]=e)}),t},o.utf8Encode=function(e){var t,r,n,i="",o=0;for(t=r=0,o=(e=(e+"").replace(/\r\n/g,"\n").replace(/\r/g,"\n")).length,n=0;n<o;n++){var a=e.charCodeAt(n),s=null;a<128?r++:s=a>127&&a<2048?String.fromCharCode(a>>6|192,63&a|128):String.fromCharCode(a>>12|224,a>>6&63|128,63&a|128),null!==s&&(r>t&&(i+=e.substring(t,r)),i+=s,t=r=n+1)}return r>t&&(i+=e.substring(t,e.length)),i},o.base64Encode=function(e){var t,r,n,i,a,s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",c=0,u=0,p="",f=[];if(!e)return e;e=o.utf8Encode(e);do{t=(a=e.charCodeAt(c++)<<16|e.charCodeAt(c++)<<8|e.charCodeAt(c++))>>18&63,r=a>>12&63,n=a>>6&63,i=63&a,f[u++]=s.charAt(t)+s.charAt(r)+s.charAt(n)+s.charAt(i)}while(c<e.length);switch(p=f.join(""),e.length%3){case 1:p=p.slice(0,-2)+"==";break;case 2:p=p.slice(0,-1)+"="}return p},o.getCurrentPath=function(){var e="未取到";try{var t=getCurrentPages();t.length&&(e=t[t.length-1].route)}catch(e){y.info(e)}return e},o.getQueryParam=function(e,t){var r="[\\?&]"+t+"=([^&#]*)",n=new RegExp(r).exec(e);return null===n||n&&"string"!=typeof n[1]&&n[1].length?"":o.decodeURIComponent(n[1])},o.convertObjToParam=function(e){var t=[];for(var r in e)t.push(r+"="+e[r]);return t.join("&")},o.getObjFromQuery=function(e){var t=e.split("?"),r={};return t&&t[1]?(o.each(t[1].split("&"),function(e){var t=e.split("=");t[0]&&t[1]&&(r[t[0]]=t[1])}),r):{}},o.getMPScene=function(e){return"number"==typeof e||"string"==typeof e&&""!==e?(e=String(e))||d[e]:"未取到值"},o.info={userInfo:!1,getGPS:!1,properties:{$lib:"WXMP",$lib_version:String("1.0.0"),$brand:"devtools"},getUserInfo:function(){var e=this.userInfo,t=this.properties;e||wx.getSetting({success:function(t){!e&&t.authSetting&&t.authSetting["scope.userInfo"]&&(e=!0)},complete:function(){wx.getUserInfo({success:function(r){t.$country=r.userInfo.country,t.$province=r.userInfo.province,t.$city=r.userInfo.city,t.$gd=r.userInfo.gender,t.$au=r.userInfo.avatarUrl,t.$nn=r.userInfo.nickName,e=!0}})}})},getLocation:function(){var e=this.properties,t=this.getGPS;wx.getLocation({type:"wgs84",success:function(r){e.$latitude=r.latitude,e.$longitude=r.longitude,e.$speed=r.speed,e.$accuracy=r.accuracy,t=!0}})},getInfo:function(){var e=this.userInfo,t=this.getGPS;e||this.getUserInfo(),!t&&s.getLocation&&this.getLocation()},getSystem:function(){function e(){wx.getSystemInfo({success:function(e){t.$brand=e.brand,t.$model=e.model,t.$screenWidth=Number(e.windowWidth),t.$screenHeight=Number(e.windowHeight),t.$os=e.system.split(" ")[0],t.$osVersion=e.system.split(" ")[1],t.$wxVersion=e.version},complete:function(){a.initialState.systemIsComplete=!0,a.initialState.checkIsComplete(),o.info.getInfo()}})}var t=this.properties;wx.getNetworkType({success:function(e){t.$networkType=e.networkType},complete:e})}},a._=o,a.initialState={queue:[],isComplete:!1,systemIsComplete:!1,storeIsComplete:!1,checkIsComplete:function(){this.systemIsComplete&&this.storeIsComplete&&(this.isComplete=!0,this.queue.length>0&&(o.each(this.queue,function(e){a[e[0]].apply(a,f.call(e[1]))}),a.queue=[]))}},a.prepareData=function(e,t){var r={$distinct_id:this.store.getDistinctId(),$lib:"WXMP",$lib_method:"code",$lib_version:String("1.0.0"),$tm:(new Date).valueOf(),properties:{}};o.extend(r,e),o.isObject(e.properties)&&!o.isEmptyObject(e.properties)&&o.extend(r.properties,e.properties),e.type&&"profile"===e.type.slice(0,7)||(r.properties=o.extend({},o.info.properties,a.store.getProps(),r.properties),"object"==typeof a.store._state&&"number"==typeof a.store._state.first_visit_day_time&&a.store._state.first_visit_day_time>(new Date).getTime()?r.properties.$isFirstDay=!0:r.properties.$isFirstDay=!1),r.properties.$time&&o.isDate(r.properties.$time)?(r.time=1*r.properties.$time,delete r.properties.$time):a.para.use_client_time&&(r.time=1*new Date),r.properties.$urlPath||(r.properties.$urlPath=o.getCurrentPath()),o.searchObjDate(r),o.searchObjString(r),o.each(r.properties,function(e,t){r[t]=e}),delete r.properties,a.send(r,t)},a.store={storageInfo:null,getUUID:function(){return Date.now()+"-"+Math.floor(1e7*Math.random())+"-"+Math.random().toString(16).replace(".","")+"-"+String(31242*Math.random()).replace(".","").slice(0,8)},getStorage:function(){return this.storageInfo?this.storageInfo:(this.storageInfo=wx.getStorageSync("dtwave2018_wechat")||"",this.storageInfo)},_state:{},toState:function(e){var t=null;o.isJSONString(e)?(t=JSON.parse(e)).distinct_id?this._state=t:this.set("distinct_id",this.getUUID()):o.isObject(e)&&(t=e).distinct_id?this._state=t:this.set("distinct_id",this.getUUID())},getFirstId:function(){return this._state.first_id},getDistinctId:function(){return this._state.distinct_id},getProps:function(){return this._state.props||{}},setProps:function(e,t){var r=this._state.props||{};t?this.set("props",e):(o.extend(r,e),this.set("props",r))},set:function(e,t){var r={};"string"==typeof e?r[e]=t:"object"==typeof e&&(r=e),this._state=this._state||{};for(var n in r)this._state[n]=r[n];this.save()},change:function(e,t){this._state[e]=t},save:function(){wx.setStorageSync("dtwave2018_wechat",this._state)},init:function(){var e=this.getStorage();if(e)this.toState(e);else{_=!0;var t=new Date,r=t.getTime();t.setHours(23),t.setMinutes(59),t.setSeconds(60),this.set({distinct_id:this.getUUID(),first_visit_time:r,first_visit_day_time:t.getTime()})}}},a._registerParams=function(e){o.isObject(e)&&!o.isEmptyObject(e)&&(o.info.properties=o.extend(o.info.properties,e))},a.registerParams=function(e){var t=new RegExp("^[/_]+");if(o.isObject(e)&&!o.isEmptyObject(e)){var r={};o.each(e,function(e,n){if(i.test(n)){o="_"+n.replace(i,"");console.warn("将event_name:"+n+"转换为"+o),r[o]=e}else if(t.test(n)){var o=n.replace(t,"_");console.warn("将event_name:"+n+"转换为"+o),r[o]=e}else r["_"+n]=e}),o.info.properties=o.extend(o.info.properties,r)}},a.register=function(e){o.isObject(e)&&!o.isEmptyObject(e)&&a.store.setProps(e)},a.clearAllRegister=function(){a.store.setProps({},!0)},a.openid={getRequest:function(e){wx.login({success:function(t){t.code&&a.para.appid&&a.para.openid_url?wx.request({url:a.para.openid_url+"&code="+t.code+"&appid="+a.para.appid,method:"GET",complete:function(t){o.isObject(t)&&o.isObject(t.data)&&t.data.openid?e(t.data.openid):e()}}):e()}})},getWXStorage:function(){var e=a.store.getStorage();if(e&&o.isObject(e))return e.openid},getOpenid:function(e){if(!a.para.appid)return e(),!1;var t=this.getWXStorage();t?e(t):this.getRequest(e)}},a.initial=function(){this._.info.getSystem(),this.store.init(),o.isObject(this.para.register)&&(o.info.properties=o.extend(o.info.properties,this.para.register))},a.init=function(e){o.isObject(e)&&(a.para=o.extend(a.para,e)),a.initialState.storeIsComplete=!0,a.initialState.checkIsComplete()},a.getPresetProperties=function(){if(o.info&&o.info.properties&&o.info.properties.$lib){var e=o.extend({$urlPath:o.getCurrentPath()},o.info.properties,a.store.getProps());return delete e.$lib,e}return{}},o.autoExeQueue=function(){return{items:[],enqueue:function(e){this.items.push(e),this.start()},dequeue:function(){return this.items.shift()},getCurrentItem:function(){return this.items[0]},isRun:!1,start:function(){this.items.length>0&&!this.isRun&&(this.isRun=!0,this.getCurrentItem().start())},close:function(){this.dequeue(),this.isRun=!1,this.start()}}},a.requestQueue=function(e){this.url=e.url},a.requestQueue.prototype.isEnd=function(){this.received||(this.received=!0,this.close())},a.requestQueue.prototype.start=function(){var e=this;setTimeout(function(){e.isEnd()},a.para.send_timeout),wx.request({url:this.url,method:"GET",complete:function(){e.isEnd()}})},a.dataQueue=o.autoExeQueue(),a.send=function(e){o.info.getInfo();var t="";(e=o.strip_dt_properties(e))._nocache=(String(Math.random())+String(Math.random())+String(Math.random())).slice(2,15),y.info(e),e=JSON.stringify(e),t=-1!==a.para.server_url.indexOf("?")?a.para.server_url+"&data="+encodeURIComponent(o.base64Encode(e)):a.para.server_url+"?data="+encodeURIComponent(o.base64Encode(e));var r=new a.requestQueue({url:t});r.close=function(){a.dataQueue.close()},a.dataQueue.enqueue(r)},a.track=function(e,t,r){if(g.indexOf(e)>=0)t.$isDefaultTrack=!0;else{if(i.test(e)){var n=e.replace(i,"");console.warn("将event_name: "+e+" 转换为 "+n),e=n}if(o.isObject(t)){var a={};o.each(t,function(e,t){var r=t.replace(i,"");a[r]=e,console.warn("将key: "+t+" 转换为 "+r)}),t=a}}this.prepareData({$type:"track",$project_id:this.para.project_id||s.appid,$appid:this.para.appid||s.appid,$event_name:e,properties:t},r)},a.autoTrackCustom=function(e,t,r){var n=a.para.autoTrack[e],i="";a.para.autoTrack&&n&&("function"==typeof n?(i=n(),o.isObject(i)&&o.extend(t,i)):o.isObject(n)&&(o.extend(t,n),a.para.autoTrack[e]=!0),a.track(r,t))},o.each(["track","register","clearAllRegister","autoTrackCustom","registerParams"],function(e){var t=a[e];a[e]=function(){a.initialState.isComplete?t.apply(a,arguments):a.initialState.queue.push([e,arguments])}}),!1!==a.para.autoTrack){var b=App;App=function(i){e(i,"onLaunch",t),e(i,"onShow",r),e(i,"onHide",n),b(i)};var S=Page;Page=function(t){if(e(t,"onLoad",function(e){if(e&&o.isObject(e)){var t=o.extend({},e);if(e.q&&o.extend(t,o.getObjFromQuery(o.decodeURIComponent(e.q))),e.scene){var r=e.scene;r=-1!==(r=o.decodeURIComponent(r)).indexOf("?")?"?"+r.replace(/\?/g,""):"?"+r,o.extend(t,o.getObjFromQuery(r))}}}),e(t,"onShow",function(){var e="系统没有取到值";"object"==typeof this&&("string"==typeof this.route?e=this.route:"string"==typeof this.__route__&&(e=this.__route__));var t={};t.$referrer=m,t.$urlPath=e,this.sensors_mp_load_utm&&(o.extend(t,this.sensors_mp_load_utm),this.sensors_mp_load_utm=null),a.para.onshow?a.para.onshow(sa,e,this):a.autoTrackCustom("pageShow",t,"$WXMPViewScreen"),m=e}),"function"==typeof t.onShareAppMessage){var r=t.onShareAppMessage;t.onShareAppMessage=function(){a.para.autoTrack&&a.para.autoTrack.pageShare&&a.autoTrackCustom("pageShare",{$urlPath:o.getCurrentPath(),$shareDepth:o.getShareDepth()},"$WXMPShare");var e=r.apply(this,arguments);return a.para.allow_amend_share_path&&"string"==typeof e.path&&-1!==e.path.indexOf("/")&&(-1===e.path.indexOf("?")?e.path=e.path+"?":"&"!==e.path.slice(-1)&&(e.path=e.path+"&"),e.path=e.path+"shareinfo="+encodeURIComponent(o.getShareInfo())),e}}S.apply(this,arguments)}}a.initial(),module.exports=a;