Skip to content

Commit dd2b0b7

Browse files
author
shengyonggen
committed
修改队列发送数据,自定义预置事件的属性,page的load和show调整
1 parent 96df947 commit dd2b0b7

File tree

3 files changed

+135
-62
lines changed

3 files changed

+135
-62
lines changed

sensorsdata.js

Lines changed: 133 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var ArrayProto = Array.prototype,
1919
slice = ArrayProto.slice,
2020
toString = ObjProto.toString,
2121
hasOwnProperty = ObjProto.hasOwnProperty,
22-
LIB_VERSION = '0.9',
22+
LIB_VERSION = '1.0',
2323
LIB_NAME = 'MiniProgram';
2424

2525
var source_channel_standard = 'utm_source utm_medium utm_campaign utm_content utm_term';
@@ -456,28 +456,28 @@ _.getUtm = function (url) {
456456
var params = {};
457457
url = _.decodeURIComponent(url);
458458
url = url.split('?');
459-
if(url.length === 2){
459+
if (url.length === 2) {
460460
url = url[1];
461-
}else{
461+
} else {
462462
return {};
463463
}
464464

465-
url = '?' + url;
466-
if (_.isArray(sa.para.source_channel) && sa.para.source_channel.length > 0) {
467-
campaign_keywords = campaign_keywords.concat(sa.para.source_channel);
468-
campaign_keywords = _.unique(campaign_keywords);
469-
}
470-
_.each(campaign_keywords, function (kwkey) {
471-
kw = _.getQueryParam(url, kwkey);
472-
if (kw.length) {
473-
if (_.include(campagin_w,kwkey)){
474-
params['$' + kwkey] = kw;
475-
}else{
476-
params[kwkey] = kw;
477-
}
465+
url = '?' + url;
466+
if (_.isArray(sa.para.source_channel) && sa.para.source_channel.length > 0) {
467+
campaign_keywords = campaign_keywords.concat(sa.para.source_channel);
468+
campaign_keywords = _.unique(campaign_keywords);
469+
}
470+
_.each(campaign_keywords, function (kwkey) {
471+
kw = _.getQueryParam(url, kwkey);
472+
if (kw.length) {
473+
if (_.include(campagin_w, kwkey)) {
474+
params['$' + kwkey] = kw;
475+
} else {
476+
params[kwkey] = kw;
478477
}
479-
});
480-
return params;
478+
}
479+
});
480+
return params;
481481
};
482482

483483
_.info = {
@@ -748,6 +748,63 @@ sa.init = function () {
748748

749749
};
750750

751+
// 发送队列
752+
_.autoExeQueue = function () {
753+
var queue = {
754+
// 简单队列
755+
items: [],
756+
enqueue: function (val) {
757+
this.items.push(val);
758+
this.start();
759+
},
760+
dequeue: function () {
761+
return this.items.shift();
762+
},
763+
getCurrentItem: function () {
764+
return this.items[0];
765+
},
766+
// 自动循环执行队列
767+
isRun: false,
768+
start: function () {
769+
if (this.items.length > 0 && !this.isRun) {
770+
this.isRun = true;
771+
this.getCurrentItem().start();
772+
}
773+
},
774+
close: function () {
775+
this.dequeue();
776+
this.isRun = false;
777+
this.start();
778+
}
779+
};
780+
return queue;
781+
};
782+
783+
sa.requestQueue = function (para) {
784+
this.url = para.url;
785+
};
786+
sa.requestQueue.prototype.isEnd = function () {
787+
if (!this.received) {
788+
this.received = true;
789+
this.close();
790+
}
791+
};
792+
sa.requestQueue.prototype.start = function () {
793+
var me = this;
794+
setTimeout(function () {
795+
me.isEnd();
796+
}, 300);
797+
wx.request({
798+
url: this.url,
799+
method: 'GET',
800+
complete: function(){
801+
me.isEnd();
802+
}
803+
});
804+
};
805+
806+
sa.dataQueue = _.autoExeQueue();
807+
751808
sa.send = function (t) {
752809
var url = '';
753810
t._nocache = (String(Math.random()) + String(Math.random()) + String(Math.random())).slice(2, 15);
@@ -761,16 +818,33 @@ sa.send = function (t) {
761818
} else {
762819
url = sa.para.server_url + '?data=' + encodeURIComponent(_.base64Encode(t));
763820
}
764-
765-
var sendRequest = function () {
766-
wx.request({
767-
"url": url,
768-
"method": "GET"
769-
});
821+
var instance = new sa.requestQueue({
822+
url:url
823+
});
824+
instance.close = function () {
825+
sa.dataQueue.close();
770826
};
771-
sendRequest();
827+
sa.dataQueue.enqueue(instance);
828+
829+
};
830+
831+
sa.autoTrackCustom = function(api, prop, event) {
832+
var temp = sa.para.autoTrack[api];
833+
var tempFunc = '';
834+
if (sa.para.autoTrack && temp) {
835+
if (typeof temp === 'function') {
836+
tempFunc = temp();
837+
if(_.isObject(tempFunc)){
838+
_.extend(prop, tempFunc);
839+
}
840+
}else if(_.isObject(temp)){
841+
_.extend(prop, temp);
842+
}
843+
sa.track(event, prop);
844+
}
772845
};
773846

847+
774848
function e(t, n, o) {
775849
if (t[n]) {
776850
var e = t[n];
@@ -798,7 +872,7 @@ function appLaunch(para) {
798872
}
799873
// console.log('app_launch', JSON.stringify(arguments));
800874
if (sa.para.autoTrack && sa.para.autoTrack.appLaunch === true) {
801-
sa.track('$MPLaunch', prop);
875+
sa.autoTrackCustom('appLaunch', prop, '$MPLaunch');
802876
}
803877

804878
};
@@ -815,13 +889,13 @@ function appShow(para) {
815889
_.extend(prop, _.getUtm(para.query.q));
816890
}
817891
if (sa.para.autoTrack && sa.para.autoTrack.appShow === true) {
818-
sa.track('$MPShow', prop);
892+
sa.autoTrackCustom('appShow',prop,'$MPShow');
819893
}
820894
};
821895

822896
function appHide() {
823897
if (sa.para.autoTrack && sa.para.autoTrack.appHide === true) {
824-
sa.track('$MPHide');
898+
sa.autoTrackCustom('appHide', {}, '$MPHide');
825899
}
826900
// console.log('app_hide', JSON.stringify(arguments));
827901
// sa.track('app_hide', { detail: JSON.stringify(arguments) });
@@ -853,34 +927,6 @@ function pageOnunload(n, e) {
853927
// sa.track('page_unload', { detail: JSON.stringify(arguments) });
854928
}
855929

856-
function pageOnload(para) {
857-
//console.log('s-page_onload', JSON.stringify(arguments));
858-
var router = typeof this["__route__"] === 'string' ? this["__route__"] : '系统没有取到值';
859-
860-
var prop = {};
861-
prop.$referrer = sa_referrer;
862-
prop.$url_path = router;
863-
// 暂时只解析传统网页渠道的query
864-
if (para && _.isObject(para) && para.q) {
865-
_.extend(prop, _.getUtm(para.q));
866-
}
867-
868-
if (sa.para.onshow) {
869-
sa.para.onshow(sa, router, this);
870-
} else {
871-
if (sa.para.autoTrack && sa.para.autoTrack.pageLoad === true) {
872-
sa.track('$MPViewScreen', prop);
873-
}
874-
}
875-
876-
sa_referrer = router;
877-
878-
};
879-
880-
function pageOnshow() {
881-
//console.log('s-page_show', JSON.stringify(arguments));
882-
};
883-
884930
function pageOnHide() {
885931
// console.log('s-page_hide', JSON.stringify(arguments));
886932
// sa.track('page_hide', { detail: JSON.stringify(arguments) });
@@ -906,12 +952,39 @@ function pageOnShareAppMessage(n, e) {
906952
}
907953

908954
var v = Page;
909-
910955
Page = function (t) {
956+
e(t, "onLoad", function (para) {
957+
// 暂时只解析传统网页渠道的query
958+
if (para && _.isObject(para) && para.q) {
959+
this.sensors_mp_load_utm = _.getUtm(para.q);
960+
}
961+
});
962+
963+
e(t, "onShow", function () {
964+
var router = '系统没有取到值';
965+
if (typeof this === 'object') {
966+
if (typeof this.route === 'string') {
967+
router = this.route;
968+
} else if (typeof this.__route__ === 'string') {
969+
router = this.__route__;
970+
}
971+
}
972+
973+
var prop = {};
974+
prop.$referrer = sa_referrer;
975+
prop.$url_path = router;
976+
if (this.sensors_mp_load_utm){
977+
_.extend(prop, this.sensors_mp_load_utm);
978+
this.sensors_mp_load_utm = null;
979+
}
980+
if (sa.para.onshow) {
981+
sa.para.onshow(sa, router, this);
982+
} else {
983+
sa.autoTrackCustom('pageShow', prop, '$MPViewScreen');
984+
}
985+
sa_referrer = router;
986+
});
911987

912-
e(t, "onLoad", pageOnload);
913-
// e(t, "onUnload", pageOnunload);
914-
// e(t, "onShow", pageOnshow);
915988
// e(t, "onHide", pageOnHide);
916989

917990
// e(t, "onReady", pageOnReady);

0 commit comments

Comments
 (0)