Skip to content

Commit c4ea231

Browse files
author
shengyonggen
committed
format控制台json,增加callback延时必发
1 parent 5a62eab commit c4ea231

File tree

5 files changed

+195
-177
lines changed

5 files changed

+195
-177
lines changed

sensorsdata.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sdk.js

Lines changed: 92 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ _.decodeURIComponent = function(val){
235235
result = decodeURIComponent(val);
236236
}catch(e){
237237
result = val;
238-
};
238+
}
239239
return result;
240240
};
241241

@@ -289,6 +289,15 @@ _.searchObjDate = function(o) {
289289
});
290290
}
291291
};
292+
293+
_.formatJsonString = function(obj){
294+
try{
295+
return JSON.stringify(obj, null, ' ');
296+
}catch(e){
297+
return JSON.stringify(obj);
298+
}
299+
};
300+
292301
// 把字符串格式数据限制字符串长度
293302
_.formatString = function(str) {
294303
if (str.length > sd.para.max_string_length) {
@@ -446,8 +455,7 @@ _.base64Encode = function(data) {
446455

447456
_.UUID = (function() {
448457
var T = function() {
449-
var d = 1 * new Date()
450-
, i = 0;
458+
var d = 1 * new Date(), i = 0;
451459
while (d == 1 * new Date()) {
452460
i++;
453461
}
@@ -569,7 +577,7 @@ _.urlParse = function(para) {
569577

570578
};
571579
return new URLParser(para);
572-
}
580+
};
573581

574582
/* ulr预置
575583
_.referringDomain = function(referrer) {
@@ -670,50 +678,37 @@ _.hasStandardBrowserEnviroment = function() {
670678

671679
};
672680

673-
_.bindReady = function(handler) {
674-
var called = false
675-
function ready() {
676-
if (called) {
677-
return false;
678-
}
679-
called = true;
680-
handler();
681-
}
682-
if ( document.addEventListener ) {
683-
document.addEventListener( "DOMContentLoaded", ready, false );
684-
} else if ( document.attachEvent ) {
685-
try {
686-
var isFrame = window.frameElement != null
687-
} catch(e) {}
688-
if ( document.documentElement.doScroll && !isFrame ) {
689-
function tryScroll(){
690-
if (called) return
691-
try {
692-
document.documentElement.doScroll("left")
693-
ready()
694-
} catch(e) {
695-
setTimeout(tryScroll, 10)
696-
}
697-
}
698-
tryScroll()
699-
}
700-
document.attachEvent("onreadystatechange", function(){
701-
if ( document.readyState === "complete" ) {
702-
ready()
703-
}
704-
})
705-
}
706-
if (window.addEventListener){
707-
window.addEventListener('load', ready, false)
708-
} else if (window.attachEvent) {
709-
window.attachEvent('onload', ready)
710-
} else {
711-
var fn = window.onload;
712-
window.onload = function() {
713-
fn && fn();
714-
ready();
681+
_.bindReady = function(fn,win) {
682+
win = win || window;
683+
var done = false,
684+
top = true,
685+
doc = win.document,
686+
root = doc.documentElement,
687+
modern = doc.addEventListener,
688+
add = modern ? 'addEventListener' : 'attachEvent',
689+
rem = modern ? 'removeEventListener' : 'detachEvent',
690+
pre = modern ? '' : 'on',
691+
init = function(e) {
692+
if (e.type == 'readystatechange' && doc.readyState != 'complete') return;
693+
(e.type == 'load' ? win : doc)[rem](pre + e.type, init, false);
694+
if (!done && (done = true)) fn.call(win, e.type || e);
695+
},
696+
poll = function() {
697+
try { root.doScroll('left'); } catch(e) { setTimeout(poll, 50); return; }
698+
init('poll');
699+
};
700+
701+
if (doc.readyState == 'complete') fn.call(win, 'lazy');
702+
else {
703+
if (!modern && root.doScroll) {
704+
try { top = !win.frameElement; } catch(e) { }
705+
if (top) poll();
715706
}
707+
doc[add](pre + 'DOMContentLoaded', init, false);
708+
doc[add](pre + 'readystatechange', init, false);
709+
win[add](pre + 'load', init, false);
716710
}
711+
717712
};
718713

719714

@@ -1334,7 +1329,7 @@ _.getReferrer = function(referrer){
13341329
};
13351330

13361331
_.getKeywordFromReferrer = function(){
1337-
var search_keyword = {baidu:'wd',google:'q',bing:'q',yahoo:'p',sogou:'query',so:'q'}
1332+
var search_keyword = {baidu:'wd',google:'q',bing:'q',yahoo:'p',sogou:'query',so:'q'};
13381333
if(document && typeof document.referrer === 'string'){
13391334
if(document.referrer.indexOf('http') === 0) {
13401335
var domain = _.url('domain',document.referrer);
@@ -1346,6 +1341,7 @@ _.getKeywordFromReferrer = function(){
13461341
}
13471342
}
13481343
}
1344+
return '未取到值';
13491345
}else{
13501346
return '未取到值';
13511347
}
@@ -1472,8 +1468,9 @@ sd.sendState.getSendCall = function(data, callback) {
14721468

14731469
// 加cache防止缓存
14741470
data._nocache = (String(Math.random()) + String(Math.random()) + String(Math.random())).replace(/\./g,'').slice(0,15);
1471+
var originData = data;
14751472
data = JSON.stringify(data);
1476-
logger.info(data);
1473+
logger.info(_.formatJsonString(originData));
14771474
// 打通app传数据给app
14781475
if(sd.para.use_app_track){
14791476
if((typeof SensorsData_APP_JS_Bridge === 'object') && SensorsData_APP_JS_Bridge.sensorsdata_track){
@@ -1515,38 +1512,50 @@ sd.sendState.prepareServerUrl = function(data,callback){
15151512
}
15161513
};
15171514

1518-
sd.sendState.callBack = function(callback){
1519-
1520-
(typeof callback === 'function') && callback();
1521-
1515+
sd.sendState.stateInfo = function(para){
1516+
this.callback = para.callback;
1517+
this.hasCalled = false;
1518+
this.img = document.createElement('img');
1519+
this.server_url = para.server_url;
1520+
this.sendState = para.sendState;
1521+
this.start();
15221522
};
15231523

1524-
sd.sendState.sendCall = function(server_url,callback){
1525-
1526-
1527-
++this._receive;
1528-
var state = '_state' + this._receive;
1524+
sd.sendState.stateInfo.prototype.start = function(){
15291525
var me = this;
1530-
this[state] = document.createElement('img');
1531-
1532-
this[state].onload = function(e) {
1533-
me[state].onload = null;
1534-
delete me[state];
1535-
++me._complete;
1536-
me.callBack(callback);
1526+
function callAndDelete(){
1527+
if(typeof me === 'object' && typeof me.callback === 'function' && !me.hasCalled){
1528+
me.hasCalled = true;
1529+
me.callback();
1530+
delete me;
1531+
}
1532+
}
1533+
setTimeout(callAndDelete, 1000);
1534+
this.img.onload = function(e) {
1535+
this.onload = null;
1536+
++me.sendState._complete;
1537+
callAndDelete();
15371538
};
1538-
this[state].onerror = function(e) {
1539-
me[state].onerror = null;
1540-
delete me[state];
1541-
me.callBack(callback);
1539+
this.img.onerror = function(e) {
1540+
this.onerror = null;
1541+
callAndDelete();
15421542
};
1543-
this[state].onabort = function(e) {
1544-
delete me[state];
1545-
me.callBack(callback);
1543+
this.img.onabort = function(e) {
1544+
this.onabort = null;
1545+
callAndDelete();
15461546
};
1547+
this.img.src = this.server_url;
1548+
};
15471549

1548-
this[state].src = server_url;
1549-
1550+
sd.sendState.sendCall = function(server_url,callback){
1551+
++this._receive;
1552+
var state = '_state' + this._receive;
1553+
var me = this;
1554+
this[state] = new this.stateInfo({
1555+
callback: callback,
1556+
server_url: server_url,
1557+
sendState: this
1558+
});
15501559
};
15511560

15521561
// 检查是否是新用户(第一次种cookie,且在8个小时内的)
@@ -1637,17 +1646,17 @@ var saNewUser = {
16371646
if(!store.getProps() || !store.getProps().$latest_referrer){
16381647
sd.register({
16391648
$latest_referrer: '取值异常',
1640-
$latest_referrer_host: '取值异常'
1641-
//$latest_traffic_source_type: '取值异常',
1642-
//$latest_search_keyword: '取值异常'
1649+
$latest_referrer_host: '取值异常',
1650+
$latest_traffic_source_type: '取值异常',
1651+
$latest_search_keyword: '取值异常'
16431652
});
16441653
}
16451654
} else {
16461655
sd.register({
1647-
//$latest_traffic_source_type:_.getSourceFromReferrer(),
1656+
$latest_traffic_source_type:_.getSourceFromReferrer(),
16481657
$latest_referrer: _.info.pageProp.referrer,
1649-
$latest_referrer_host: _.info.pageProp.referrer_host
1650-
//$latest_search_keyword: _.getKeywordFromReferrer()
1658+
$latest_referrer_host: _.info.pageProp.referrer_host,
1659+
$latest_search_keyword: _.getKeywordFromReferrer()
16511660
});
16521661
}
16531662
// utm
@@ -2153,10 +2162,10 @@ saEvent.send = function(p, callback) {
21532162
$first_visit_time: new Date(),
21542163
$first_referrer: _.getReferrer(),
21552164
$first_browser_language: navigator.language || '取值异常',
2156-
//$first_browser_charset: document.charset || '取值异常',
2157-
$first_referrer_host: _.info.pageProp.referrer_host
2158-
//$first_traffic_source_type: _.getSourceFromReferrer(),
2159-
//$first_search_keyword: _.getKeywordFromReferrer()
2165+
$first_browser_charset: document.charset || '取值异常',
2166+
$first_referrer_host: _.info.pageProp.referrer_host,
2167+
$first_traffic_source_type: _.getSourceFromReferrer(),
2168+
$first_search_keyword: _.getKeywordFromReferrer()
21602169
}, $utms)
21612170
);
21622171
}

0 commit comments

Comments
 (0)