diff --git a/dist/anka-tracker.min.js b/dist/anka-tracker.min.js index 103dff4..2c95e85 100644 --- a/dist/anka-tracker.min.js +++ b/dist/anka-tracker.min.js @@ -1,8 +1,8 @@ /** * @anka-dev/tracker. - * Tue Aug 14 2018 15:55:48 GMT+0800 (CST) + * Tue Aug 14 2018 15:57:54 GMT+0800 (CST) * MIT License. */ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.Tracker={})}(this,function(t){"use strict";function e(t,e){function n(){this.constructor=t}for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r]);t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}function n(t,e,n,r){var o,i=arguments.length,a=i<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,e,n,r);else for(var s=t.length-1;s>=0;s--)(o=t[s])&&(a=(i<3?o(a):i>3?o(e,n,a):o(e,n))||a);return i>3&&a&&Object.defineProperty(e,n,a),a}function r(t){return new Promise(function(e,n){wx.request(p({},t,{success:function(t){t.statusCode>=200&&t.statusCode<300?e&&e(t.data):n&&n(t.data)},fail:function(t){n&&n(t)}}))})}function o(t){return new Promise(function(e,n){wx.setStorage(p({},t,{success:function(t){e(t)},fail:function(t){n(t)}}))})}function i(t){return new Promise(function(e,n){wx.getStorage({key:t,success:function(t){e(t.data)},fail:function(t){n(t)}})})}function a(){return new Promise(function(t,e){wx.getSystemInfo({success:function(e){t(e)},fail:function(t){e(t)}})})}function s(){return new Promise(function(t,e){wx.getNetworkType({success:function(e){t(e.networkType)},fail:function(t){e(t)}})})}function c(t){wx.onNetworkStatusChange(function(e){t(e.networkType)})}function u(){return function(t,e,n){return n.writable=!1,n}}const p=Object.assign||function(t){for(var e,n=1;n=48&&o<=57||o>=65&&o<=90||o>=97&&o<=122?n+=e.charAt(r):o<128?n+=y[o]:o<2048?n+=y[192|o>>6]+y[128|63&o]:o<55296||o>=57344?n+=y[224|o>>12]+y[128|o>>6&63]+y[128|63&o]:(r+=1,o=65536+((1023&o)<<10|1023&e.charCodeAt(r)),n+=y[240|o>>18]+y[128|o>>12&63]+y[128|o>>6&63]+y[128|63&o])}return n},isBuffer:function(t){return null!==t&&void 0!==t&&!!(t.constructor&&t.constructor.isBuffer&&t.constructor.isBuffer(t))},isRegExp:function(t){return"[object RegExp]"===Object.prototype.toString.call(t)},merge:function t(e,n,r){if(!n)return e;if("object"!=typeof n){if(Array.isArray(e))e.push(n);else{if("object"!=typeof e)return[e,n];(r.plainObjects||r.allowPrototypes||!d.call(Object.prototype,n))&&(e[n]=!0)}return e}if("object"!=typeof e)return[e].concat(n);var o=e;return Array.isArray(e)&&!Array.isArray(n)&&(o=m(e,r)),Array.isArray(e)&&Array.isArray(n)?(n.forEach(function(n,o){d.call(e,o)?e[o]&&"object"==typeof e[o]?e[o]=t(e[o],n,r):e.push(n):e[o]=n}),e):Object.keys(n).reduce(function(e,o){var i=n[o];return d.call(e,o)?e[o]=t(e[o],i,r):e[o]=i,e},o)}},b=String.prototype.replace,w={default:"RFC3986",formatters:{RFC1738:function(t){return b.call(t,/%20/g,"+")},RFC3986:function(t){return t}},RFC1738:"RFC1738",RFC3986:"RFC3986"},_={brackets:function(t){return t+"[]"},indices:function(t,e){return t+"["+e+"]"},repeat:function(t){return t}},O=Date.prototype.toISOString,k={delimiter:"&",encode:!0,encoder:v.encode,encodeValuesOnly:!1,serializeDate:function(t){return O.call(t)},skipNulls:!1,strictNullHandling:!1},j=function t(e,n,r,o,i,a,s,c,u,p,l,f){var h=e;if("function"==typeof s)h=s(n,h);else if(h instanceof Date)h=p(h);else if(null===h){if(o)return a&&!f?a(n,k.encoder):n;h=""}if("string"==typeof h||"number"==typeof h||"boolean"==typeof h||v.isBuffer(h))return a?[l(f?n:a(n,k.encoder))+"="+l(a(h,k.encoder))]:[l(n)+"="+l(String(h))];var d=[];if(void 0===h)return d;var y;if(Array.isArray(s))y=s;else{var g=Object.keys(h);y=c?g.sort(c):g}for(var m=0;m0?S+P:""},P=Object.prototype.hasOwnProperty,S={allowDots:!1,allowPrototypes:!1,arrayLimit:20,decoder:v.decode,delimiter:"&",depth:5,parameterLimit:1e3,plainObjects:!1,strictNullHandling:!1},x=function(t,e){for(var n={},r=e.ignoreQueryPrefix?t.replace(/^\?/,""):t,o=e.parameterLimit===1/0?void 0:e.parameterLimit,i=r.split(e.delimiter,o),a=0;a=0;--o){var i,a=t[o];if("[]"===a)i=(i=[]).concat(r);else{i=n.plainObjects?Object.create(null):{};var s="["===a.charAt(0)&&"]"===a.charAt(a.length-1)?a.slice(1,-1):a,c=parseInt(s,10);!isNaN(c)&&a!==s&&String(c)===s&&c>=0&&n.parseArrays&&c<=n.arrayLimit?(i=[])[c]=r:i[s]=r}r=i}return r},N=function(t,e,n){if(t){var r=n.allowDots?t.replace(/\.([^.[]+)/g,"[$1]"):t,o=/(\[[^[\]]*])/g,i=/(\[[^[\]]*])/.exec(r),a=i?r.slice(0,i.index):r,s=[];if(a){if(!n.plainObjects&&P.call(Object.prototype,a)&&!n.allowPrototypes)return;s.push(a)}for(var c=0;null!==(i=o.exec(r))&&c=l.FAILED&&t.status<=this.config.retry&&(this.failedQueue.push(t),this.updateStore(),this.run())},t.prototype.pop=function(){var t=this.failedQueue.length,e=this.config.groupMaxLength,n=t-e>=0?this.failedQueue.splice(0,e):this.failedQueue.splice(0,t).concat(this.queue.splice(0,e-t));return this.updateStore(),n},t.prototype.updateStore=function(t){var e=Date.now();(this.store&&e-this.lastStoreUpdate>=500||t&&this.store)&&(this.store.update(this.queue.concat(this.failedQueue)),this.lastStoreUpdate=e)},t.prototype.run=function(){this.executor.run()},t.prototype.suspend=function(t){this.updateStore(!0),this.executor.suspend(t)},t}(),M=function(){function t(){this.status=q.IDLE}return Object.defineProperty(t.prototype,"isIdle",{get:function(){return this.sender&&this.queueManager&&this.status===q.IDLE},enumerable:!0,configurable:!0}),t.prototype.init=function(t,e){this.sender=t,this.queueManager=e},t.prototype.run=function(){this.isIdle&&this.exec()},t.prototype.exec=function(){var t=this,e=this.queueManager.pop();if(!e.length)return void(this.status=q.IDLE);this.status=q.RUNNING,Promise.all(e.map(function(e){return t.sender.send(e)})).then(function(e){e.forEach(function(e){e.status!==l.SUCCESS&&t.queueManager.push(e)})}).then(function(){t.timer=setTimeout(function(){t.exec()},t.queueManager.config.interval)})},t.prototype.suspend=function(t){t?(this.status=q.PAUSE,clearTimeout(this.timer)):this.status===q.PAUSE?(this.status=q.IDLE,this.run()):this.status===q.IDLE&&this.run()},t}(),R=function(){function t(t){this.config=t,this.queueManager=new T(this.config)}return t.prototype.init=function(t){this.queueManager.init(p({},t))},t.prototype.log=function(t){this.queueManager.push(t)},t}(),F={debug:!0,httpMethod:"POST",retry:2,interval:1e3,groupMaxLength:5,timestampKey:"timestamp_ms",queueMaxLength:500,commonData:{},attachActionToUrl:!1,extractOnLaunchOption:!0},H=function(){function t(t){void 0===t&&(t={}),Object.assign(this,F,t)}return t}(),G=function(){function t(t){void 0===t&&(t={}),this.config=new H(t),this.core=new R(this.config),h.DEBUG=this.config.debug,this.core.queueManager.suspend(!0),this.networkDetector=new C,this.commonDataVendor=new L}return t.prototype.init=function(t){if(!this.sender){var e=this.handleNetworkStatusChange.bind(this);this.sender=new E(this.config,t),this.store=new f(this.config),this.core.init({sender:this.sender,store:this.store}),this.networkDetector.getNetworkStatus().then(e,e),this.networkDetector.watchNetworkStatusChange(e),h.log("初始化完成")}},t.prototype.handleNetworkStatusChange=function(t){var e="none"===t||t instanceof Error;this.core.queueManager.suspend(e)},t.prototype.log=function(t){var e=Date.now();t[this.config.timestampKey]=e,this.core.log(new U(t))},n([u()],t.prototype,"init",null),n([u()],t.prototype,"handleNetworkStatusChange",null),n([u()],t.prototype,"log",null),t}(),K=function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return e(r,t),r.generateTrackerInstance=function(){var t={};try{t=require("./anka-tracker.config.js")}catch(t){}var e=new r(t);return t.extractOnLaunchOption&&e.extractOnLaunchOption(),e},r.prototype.asyncInitWithCommonData=function(t){var e=this;return void 0===t&&(t={}),this.commonDataVendor.getCommonData({onLaunchOption:this.onLaunchOption}).then(function(n){e.init(Object.assign(n,e.config.commonData,t))}).catch(function(t){h.log("初始化失败"),console.log(t)})},r.prototype.extractOnLaunchOption=function(){function t(t){e.onLaunchOption=t}var e=this,n=App;App=function(e){var r=e.onLaunch;e.onLaunch=function(e){t.call(this,e),r&&r.call(this,e)},n(e)}},r.prototype.composeCommonData=function(t){var e=[];return t.map(function(t){"function"==typeof t?e.push(new Promise(function(e){t(e)})):e.push(Promise.resolve(t))}),Promise.all(e).then(function(t){return Promise.resolve(Object.assign.apply(Object,[{}].concat(t)))})},r.prototype.track=function(){for(var t=this,e=[],n=0;n=0;s--)(o=t[s])&&(a=(i<3?o(a):i>3?o(e,n,a):o(e,n))||a);return i>3&&a&&Object.defineProperty(e,n,a),a}function r(t){return new Promise(function(e,n){wx.request(p({},t,{success:function(t){t.statusCode>=200&&t.statusCode<300?e&&e(t.data):n&&n(t.data)},fail:function(t){n&&n(t)}}))})}function o(t){return new Promise(function(e,n){wx.setStorage(p({},t,{success:function(t){e(t)},fail:function(t){n(t)}}))})}function i(t){return new Promise(function(e,n){wx.getStorage({key:t,success:function(t){e(t.data)},fail:function(t){n(t)}})})}function a(){return new Promise(function(t,e){wx.getSystemInfo({success:function(e){t(e)},fail:function(t){e(t)}})})}function s(){return new Promise(function(t,e){wx.getNetworkType({success:function(e){t(e.networkType)},fail:function(t){e(t)}})})}function c(t){wx.onNetworkStatusChange(function(e){t(e.networkType)})}function u(){return function(t,e,n){return n.writable=!1,n}}const p=Object.assign||function(t){for(var e,n=1;n=48&&o<=57||o>=65&&o<=90||o>=97&&o<=122?n+=e.charAt(r):o<128?n+=y[o]:o<2048?n+=y[192|o>>6]+y[128|63&o]:o<55296||o>=57344?n+=y[224|o>>12]+y[128|o>>6&63]+y[128|63&o]:(r+=1,o=65536+((1023&o)<<10|1023&e.charCodeAt(r)),n+=y[240|o>>18]+y[128|o>>12&63]+y[128|o>>6&63]+y[128|63&o])}return n},isBuffer:function(t){return null!==t&&void 0!==t&&!!(t.constructor&&t.constructor.isBuffer&&t.constructor.isBuffer(t))},isRegExp:function(t){return"[object RegExp]"===Object.prototype.toString.call(t)},merge:function t(e,n,r){if(!n)return e;if("object"!=typeof n){if(Array.isArray(e))e.push(n);else{if("object"!=typeof e)return[e,n];(r.plainObjects||r.allowPrototypes||!d.call(Object.prototype,n))&&(e[n]=!0)}return e}if("object"!=typeof e)return[e].concat(n);var o=e;return Array.isArray(e)&&!Array.isArray(n)&&(o=m(e,r)),Array.isArray(e)&&Array.isArray(n)?(n.forEach(function(n,o){d.call(e,o)?e[o]&&"object"==typeof e[o]?e[o]=t(e[o],n,r):e.push(n):e[o]=n}),e):Object.keys(n).reduce(function(e,o){var i=n[o];return d.call(e,o)?e[o]=t(e[o],i,r):e[o]=i,e},o)}},b=String.prototype.replace,w={default:"RFC3986",formatters:{RFC1738:function(t){return b.call(t,/%20/g,"+")},RFC3986:function(t){return t}},RFC1738:"RFC1738",RFC3986:"RFC3986"},_={brackets:function(t){return t+"[]"},indices:function(t,e){return t+"["+e+"]"},repeat:function(t){return t}},O=Date.prototype.toISOString,k={delimiter:"&",encode:!0,encoder:v.encode,encodeValuesOnly:!1,serializeDate:function(t){return O.call(t)},skipNulls:!1,strictNullHandling:!1},j=function t(e,n,r,o,i,a,s,c,u,p,l,f){var h=e;if("function"==typeof s)h=s(n,h);else if(h instanceof Date)h=p(h);else if(null===h){if(o)return a&&!f?a(n,k.encoder):n;h=""}if("string"==typeof h||"number"==typeof h||"boolean"==typeof h||v.isBuffer(h))return a?[l(f?n:a(n,k.encoder))+"="+l(a(h,k.encoder))]:[l(n)+"="+l(String(h))];var d=[];if(void 0===h)return d;var y;if(Array.isArray(s))y=s;else{var g=Object.keys(h);y=c?g.sort(c):g}for(var m=0;m0?S+P:""},P=Object.prototype.hasOwnProperty,S={allowDots:!1,allowPrototypes:!1,arrayLimit:20,decoder:v.decode,delimiter:"&",depth:5,parameterLimit:1e3,plainObjects:!1,strictNullHandling:!1},x=function(t,e){for(var n={},r=e.ignoreQueryPrefix?t.replace(/^\?/,""):t,o=e.parameterLimit===1/0?void 0:e.parameterLimit,i=r.split(e.delimiter,o),a=0;a=0;--o){var i,a=t[o];if("[]"===a)i=(i=[]).concat(r);else{i=n.plainObjects?Object.create(null):{};var s="["===a.charAt(0)&&"]"===a.charAt(a.length-1)?a.slice(1,-1):a,c=parseInt(s,10);!isNaN(c)&&a!==s&&String(c)===s&&c>=0&&n.parseArrays&&c<=n.arrayLimit?(i=[])[c]=r:i[s]=r}r=i}return r},N=function(t,e,n){if(t){var r=n.allowDots?t.replace(/\.([^.[]+)/g,"[$1]"):t,o=/(\[[^[\]]*])/g,i=/(\[[^[\]]*])/.exec(r),a=i?r.slice(0,i.index):r,s=[];if(a){if(!n.plainObjects&&P.call(Object.prototype,a)&&!n.allowPrototypes)return;s.push(a)}for(var c=0;null!==(i=o.exec(r))&&c=l.FAILED&&t.status<=this.config.retry&&(this.failedQueue.push(t),this.updateStore(),this.run())},t.prototype.pop=function(){var t=this.failedQueue.length,e=this.config.groupMaxLength,n=t-e>=0?this.failedQueue.splice(0,e):this.failedQueue.splice(0,t).concat(this.queue.splice(0,e-t));return this.updateStore(),n},t.prototype.updateStore=function(t){var e=Date.now();(this.store&&e-this.lastStoreUpdate>=500||t&&this.store)&&(this.store.update(this.queue.concat(this.failedQueue)),this.lastStoreUpdate=e)},t.prototype.run=function(){this.executor.run()},t.prototype.suspend=function(t){this.updateStore(!0),this.executor.suspend(t)},t}(),M=function(){function t(){this.status=q.IDLE}return Object.defineProperty(t.prototype,"isIdle",{get:function(){return this.sender&&this.queueManager&&this.status===q.IDLE},enumerable:!0,configurable:!0}),t.prototype.init=function(t,e){this.sender=t,this.queueManager=e},t.prototype.run=function(){this.isIdle&&this.exec()},t.prototype.exec=function(){var t=this,e=this.queueManager.pop();if(!e.length)return void(this.status=q.IDLE);this.status=q.RUNNING,Promise.all(e.map(function(e){return t.sender.send(e)})).then(function(e){e.forEach(function(e){e.status!==l.SUCCESS&&t.queueManager.push(e)})}).then(function(){t.timer=setTimeout(function(){t.exec()},t.queueManager.config.interval)})},t.prototype.suspend=function(t){t?(this.status=q.PAUSE,clearTimeout(this.timer)):this.status===q.PAUSE?(this.status=q.IDLE,this.run()):this.status===q.IDLE&&this.run()},t}(),R=function(){function t(t){this.config=t,this.queueManager=new T(this.config)}return t.prototype.init=function(t){this.queueManager.init(p({},t))},t.prototype.log=function(t){this.queueManager.push(t)},t}(),F={debug:!0,httpMethod:"POST",retry:2,interval:1e3,groupMaxLength:5,timestampKey:"timestamp_ms",queueMaxLength:500,commonData:{},attachActionToUrl:!1,extractOnLaunchOption:!0},H=function(){function t(t){void 0===t&&(t={}),Object.assign(this,F,t)}return t}(),G=function(){function t(t){void 0===t&&(t={}),this.config=new H(t),this.core=new R(this.config),h.DEBUG=this.config.debug,this.core.queueManager.suspend(!0),this.networkDetector=new C,this.commonDataVendor=new L}return t.prototype.init=function(t){if(!this.sender){var e=this.handleNetworkStatusChange.bind(this);this.sender=new E(this.config,t),this.store=new f(this.config),this.core.init({sender:this.sender,store:this.store}),this.networkDetector.getNetworkStatus().then(e,e),this.networkDetector.watchNetworkStatusChange(e),h.log("初始化完成")}},t.prototype.handleNetworkStatusChange=function(t){var e="none"===t||t instanceof Error;this.core.queueManager.suspend(e)},t.prototype.log=function(t){var e=Date.now();t[this.config.timestampKey]=e,this.core.log(new U(t))},n([u()],t.prototype,"init",null),n([u()],t.prototype,"handleNetworkStatusChange",null),n([u()],t.prototype,"log",null),t}(),K=function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return e(r,t),r.generateTrackerInstance=function(){var t={};try{t=require("./anka-tracker.config.js")}catch(t){}var e=new r(t);return t.extractOnLaunchOption&&e.extractOnLaunchOption(),e},r.prototype.asyncInitWithCommonData=function(t){var e=this;return void 0===t&&(t={}),this.commonDataVendor.getCommonData({onLaunchOption:this.onLaunchOption}).then(function(n){e.init(Object.assign(n,e.config.commonData,t))}).catch(function(t){h.log("初始化失败"),console.log(t)})},r.prototype.extractOnLaunchOption=function(){function t(t){e.onLaunchOption=t}var e=this,n=App;App=function(e){var r=e.onLaunch;e.onLaunch=function(e){t.call(this,e),r&&r.call(this,e)},n(e)}},r.prototype.composeCommonData=function(t){var e=[];return t.map(function(t){"function"==typeof t?e.push(new Promise(function(e){t(e)})):e.push(Promise.resolve(t))}),Promise.all(e).then(function(t){return Promise.resolve(Object.assign.apply(Object,[{}].concat(t)))})},r.prototype.track=function(){for(var t=this,e=[],n=0;n