@@ -176,8 +176,12 @@ _.inherit = function(subclass, superclass) {
176
176
return subclass ;
177
177
} ;
178
178
179
+ _ . trim = function ( str ) {
180
+ return str . replace ( / ^ [ \s \uFEFF \xA0 ] + | [ \s \uFEFF \xA0 ] + $ / g, '' ) ;
181
+ } ;
182
+
179
183
_ . isObject = function ( obj ) {
180
- return toString . call ( obj ) == '[object Object]' ;
184
+ return ( toString . call ( obj ) == '[object Object]' ) && ( obj != null ) ;
181
185
} ;
182
186
183
187
_ . isEmptyObject = function ( obj ) {
@@ -332,10 +336,11 @@ _.strip_sa_properties = function(p) {
332
336
return p ;
333
337
} ;
334
338
339
+ // 去掉undefined和null
335
340
_ . strip_empty_properties = function ( p ) {
336
341
var ret = { } ;
337
342
_ . each ( p , function ( v , k ) {
338
- if ( _ . isString ( v ) && v . length > 0 ) {
343
+ if ( v != null ) {
339
344
ret [ k ] = v ;
340
345
}
341
346
} ) ;
@@ -583,7 +588,54 @@ _.hasStandardBrowserEnviroment = function() {
583
588
584
589
} ;
585
590
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 ( ) {
587
639
var register_event = function ( element , type , handler ) {
588
640
if ( element . addEventListener ) {
589
641
element . addEventListener ( type , handler , false ) ;
@@ -593,13 +645,14 @@ _.addEvent = (function() {
593
645
element [ ontype ] = makeHandler ( element , handler , old_handler ) ;
594
646
}
595
647
} ;
596
-
597
648
function makeHandler ( element , new_handler , old_handlers ) {
598
649
var handler = function ( event ) {
599
650
event = event || fixEvent ( window . event ) ;
600
651
if ( ! event ) {
601
652
return undefined ;
602
653
}
654
+ event . target = event . srcElement ;
655
+
603
656
var ret = true ;
604
657
var old_result , new_result ;
605
658
if ( _ . isFunction ( old_handlers ) ) {
@@ -621,14 +674,15 @@ _.addEvent = (function() {
621
674
}
622
675
return event ;
623
676
}
677
+
624
678
fixEvent . preventDefault = function ( ) {
625
679
this . returnValue = false ;
626
680
} ;
627
681
fixEvent . stopPropagation = function ( ) {
628
682
this . cancelBubble = true ;
629
683
} ;
630
- return register_event ;
631
- } ) ( ) ;
684
+ register_event . apply ( null , arguments ) ;
685
+ } ;
632
686
633
687
_ . cookie = {
634
688
get : function ( name ) {
@@ -1405,7 +1459,8 @@ saEvent.send = function(p, callback) {
1405
1459
} ,
1406
1460
toState : function ( ds ) {
1407
1461
var state = null ;
1408
- if ( ds !== null && ( typeof ( state = JSON . parse ( ds ) ) === 'object' ) ) {
1462
+ if ( ds != null && _ . isJSONString ( ds ) ) {
1463
+ state = JSON . parse ( ds ) ;
1409
1464
if ( state . distinct_id ) {
1410
1465
this . _state = state ;
1411
1466
} else {
@@ -1537,20 +1592,42 @@ saEvent.send = function(p, callback) {
1537
1592
} ) ;
1538
1593
} ,
1539
1594
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
+ }
1547
1600
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 ) ;
1548
1629
}
1549
-
1550
-
1551
1630
} ) ;
1552
- */
1553
-
1554
1631
} ,
1555
1632
autoTrackWithoutProfile :function ( para ) {
1556
1633
this . autoTrack ( _ . extend ( para , { not_set_profile :true } ) ) ;
@@ -1945,9 +2022,14 @@ saEvent.send = function(p, callback) {
1945
2022
1946
2023
1947
2024
}
2025
+ //可视化埋点的后初始化
2026
+ sd . init = function ( ) {
2027
+ if ( _ . isObject ( sd . sdkMain ) ) {
2028
+ sd . sdkMain . _init ( ) ;
2029
+ }
2030
+ } ;
1948
2031
1949
-
1950
- sd . init = function ( ) {
2032
+ sd . _init = function ( ) {
1951
2033
// 防止爬虫等异常情况
1952
2034
/*
1953
2035
if(!_.hasStandardBrowserEnviroment()){
@@ -1961,10 +2043,12 @@ saEvent.send = function(p, callback) {
1961
2043
1962
2044
// 初始化distinct_id
1963
2045
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
+ }
1968
2052
1969
2053
} ;
1970
2054
0 commit comments