Skip to content

Commit 1fbc5d6

Browse files
author
shengyonggen
committed
修改ie8下undeifned是object类型,增加可视化埋点后初始化,增加对chrome45对cookie删除value的问题,已经自带全埋点未做完
1 parent bb29bd1 commit 1fbc5d6

File tree

6 files changed

+237
-69
lines changed

6 files changed

+237
-69
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: 108 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,12 @@ _.inherit = function(subclass, superclass) {
176176
return subclass;
177177
};
178178

179+
_.trim = function(str){
180+
return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
181+
};
182+
179183
_.isObject = function(obj) {
180-
return toString.call(obj) == '[object Object]';
184+
return (toString.call(obj) == '[object Object]') && (obj != null);
181185
};
182186

183187
_.isEmptyObject = function(obj) {
@@ -332,10 +336,11 @@ _.strip_sa_properties = function(p) {
332336
return p;
333337
};
334338

339+
// 去掉undefined和null
335340
_.strip_empty_properties = function(p) {
336341
var ret = {};
337342
_.each(p, function(v, k) {
338-
if (_.isString(v) && v.length > 0) {
343+
if (v != null) {
339344
ret[k] = v;
340345
}
341346
});
@@ -583,7 +588,54 @@ _.hasStandardBrowserEnviroment = function() {
583588

584589
};
585590

586-
_.addEvent = (function() {
591+
_.bindReady = function(handler) {
592+
var called = false
593+
function ready() {
594+
if (called) {
595+
return false;
596+
}
597+
called = true;
598+
handler();
599+
}
600+
if ( document.addEventListener ) {
601+
document.addEventListener( "DOMContentLoaded", ready, false );
602+
} else if ( document.attachEvent ) {
603+
try {
604+
var isFrame = window.frameElement != null
605+
} catch(e) {}
606+
if ( document.documentElement.doScroll && !isFrame ) {
607+
function tryScroll(){
608+
if (called) return
609+
try {
610+
document.documentElement.doScroll("left")
611+
ready()
612+
} catch(e) {
613+
setTimeout(tryScroll, 10)
614+
}
615+
}
616+
tryScroll()
617+
}
618+
document.attachEvent("onreadystatechange", function(){
619+
if ( document.readyState === "complete" ) {
620+
ready()
621+
}
622+
})
623+
}
624+
if (window.addEventListener){
625+
window.addEventListener('load', ready, false)
626+
} else if (window.attachEvent) {
627+
window.attachEvent('onload', ready)
628+
} else {
629+
var fn = window.onload;
630+
window.onload = function() {
631+
fn && fn();
632+
ready();
633+
}
634+
}
635+
};
636+
637+
638+
_.addEvent = function() {
587639
var register_event = function(element, type, handler) {
588640
if (element.addEventListener) {
589641
element.addEventListener(type, handler, false);
@@ -593,13 +645,14 @@ _.addEvent = (function() {
593645
element[ontype] = makeHandler(element, handler, old_handler);
594646
}
595647
};
596-
597648
function makeHandler(element, new_handler, old_handlers) {
598649
var handler = function(event) {
599650
event = event || fixEvent(window.event);
600651
if (!event) {
601652
return undefined;
602653
}
654+
event.target = event.srcElement;
655+
603656
var ret = true;
604657
var old_result, new_result;
605658
if (_.isFunction(old_handlers)) {
@@ -621,14 +674,15 @@ _.addEvent = (function() {
621674
}
622675
return event;
623676
}
677+
624678
fixEvent.preventDefault = function() {
625679
this.returnValue = false;
626680
};
627681
fixEvent.stopPropagation = function() {
628682
this.cancelBubble = true;
629683
};
630-
return register_event;
631-
})();
684+
register_event.apply(null,arguments);
685+
};
632686

633687
_.cookie = {
634688
get: function(name) {
@@ -1405,7 +1459,8 @@ saEvent.send = function(p, callback) {
14051459
},
14061460
toState: function(ds) {
14071461
var state = null;
1408-
if (ds !== null && (typeof (state = JSON.parse(ds)) === 'object')) {
1462+
if (ds != null && _.isJSONString(ds)) {
1463+
state = JSON.parse(ds);
14091464
if (state.distinct_id) {
14101465
this._state = state;
14111466
} else {
@@ -1537,20 +1592,42 @@ saEvent.send = function(p, callback) {
15371592
});
15381593
},
15391594
allTrack: function(){
1540-
/*
1541-
_.addEvent('document','click',function(e){
1542-
var ev = e || window.event;
1543-
var target = ev.target || ev.srcElement;
1544-
var tagName = target.tagName.toLowerCase();
1545-
if( tagName === 'button' || tagName === 'a' || tagName === 'input' || tagName === 'textarea'){
1546-
1595+
// 避免没有ready
1596+
if(!document || !document.body){
1597+
setTimeout(this.allTrack,1000);
1598+
return false;
1599+
}
15471600

1601+
if(sd.allTrack === 'has_init'){
1602+
return false;
1603+
}
1604+
sd.allTrack = 'has_init';
1605+
1606+
_.addEvent(document,'click',function(e){
1607+
1608+
var props = {};
1609+
var target = e.target;
1610+
var tagName = target.tagName.toLowerCase();
1611+
if(' button a select input textarea '.indexOf(' '+ tagName + ' ') !== -1){
1612+
props.$el_tagName = tagName;
1613+
props.$el_name = target.getAttribute('name');
1614+
props.$el_id = target.getAttribute('id');
1615+
props.$el_className = target.className;
1616+
props.$el_href = target.getAttribute('href');
1617+
1618+
// 获取内容
1619+
if (target.textContent) {
1620+
var textContent = _.trim(target.textContent);
1621+
if (textContent) {
1622+
textContent = textContent.replace(/[\r\n]/g, ' ').replace(/[ ]+/g, ' ').substring(0, 255);
1623+
}
1624+
props.$el_text = textContent;
1625+
}
1626+
props = _.strip_empty_properties(props);
1627+
console.log(props)
1628+
sd.track('$web_event',props);
15481629
}
1549-
1550-
15511630
});
1552-
*/
1553-
15541631
},
15551632
autoTrackWithoutProfile:function(para){
15561633
this.autoTrack(_.extend(para,{not_set_profile:true}));
@@ -1945,9 +2022,14 @@ saEvent.send = function(p, callback) {
19452022

19462023

19472024
}
2025+
//可视化埋点的后初始化
2026+
sd.init = function(){
2027+
if(_.isObject(sd.sdkMain)){
2028+
sd.sdkMain._init();
2029+
}
2030+
};
19482031

1949-
1950-
sd.init = function() {
2032+
sd._init = function() {
19512033
// 防止爬虫等异常情况
19522034
/*
19532035
if(!_.hasStandardBrowserEnviroment()){
@@ -1961,10 +2043,12 @@ saEvent.send = function(p, callback) {
19612043

19622044
// 初始化distinct_id
19632045
store.init();
1964-
1965-
_.each(sd._q, function(content) {
1966-
sd[content[0]].apply(sd, slice.call(content[1]));
1967-
});
2046+
// 发送数据
2047+
if(sd._q && _.isArray(sd._q) && sd._q.length > 0 ){
2048+
_.each(sd._q, function(content) {
2049+
sd[content[0]].apply(sd, slice.call(content[1]));
2050+
});
2051+
}
19682052

19692053
};
19702054

0 commit comments

Comments
 (0)