Skip to content

Commit 303bf04

Browse files
author
shengyonggen
committed
加入tracklink
1 parent 873a6d6 commit 303bf04

File tree

5 files changed

+200
-10
lines changed

5 files changed

+200
-10
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: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,58 @@ saEvent.send = function(p, callback) {
16021602
});
16031603
},
16041604
allTrack: function(){
1605+
// 避免没有ready
1606+
if(!document || !document.body){
1607+
setTimeout(this.allTrack,1000);
1608+
return false;
1609+
}
1610+
1611+
if(sd.allTrack === 'has_init'){
1612+
return false;
1613+
}
1614+
sd.allTrack = 'has_init';
1615+
1616+
1617+
var trackAll = {
1618+
clickEvents: function(e){
1619+
var props = {};
1620+
var target = e.target;
1621+
var tagName = target.tagName.toLowerCase();
1622+
if(' button a input '.indexOf(' '+ tagName + ' ') !== -1 ){
1623+
if(tagName === 'input'){
1624+
if(target.getAttribute('type') === 'button' || target.getAttribute('type') === 'submit'){
1625+
props.$el_value = target.value;
1626+
}else{
1627+
return false;
1628+
}
1629+
}
1630+
1631+
props.$el_tagName = tagName;
1632+
props.$el_name = target.getAttribute('name');
1633+
props.$el_id = target.getAttribute('id');
1634+
props.$el_className = typeof target.className === 'string' ? target.className : null;
1635+
props.$el_href = target.getAttribute('href');
1636+
1637+
// 获取内容
1638+
if (target.textContent) {
1639+
var textContent = _.trim(target.textContent);
1640+
if (textContent) {
1641+
textContent = textContent.replace(/[\r\n]/g, ' ').replace(/[ ]+/g, ' ').substring(0, 255);
1642+
}
1643+
props.$el_text = textContent;
1644+
}
1645+
props = _.strip_empty_properties(props);
1646+
1647+
props.$url = location.href;
1648+
props.$url_path = location.pathname;
1649+
1650+
sd.track('$web_event',props);
1651+
}
1652+
}
1653+
};
1654+
1655+
1656+
_.addEvent(document,'click',function(e){trackAll.clickEvents(e);});
16051657

16061658
},
16071659
autoTrackWithoutProfile:function(para){
@@ -1677,6 +1729,32 @@ saEvent.send = function(p, callback) {
16771729
}
16781730
};
16791731

1732+
// 跟踪链接
1733+
sd.trackLink = function(link,event_name,event_prop){
1734+
var ele = link;
1735+
event_prop = event_prop || {};
1736+
if(!link || (typeof link !== 'object')){
1737+
return false;
1738+
}
1739+
if (!link.href || /^javascript/.test(link.href) || link.target) {
1740+
return false;
1741+
}
1742+
_.addEvent(link,'click',function(e){
1743+
e.preventDefault(); // 阻止默认跳转
1744+
var hasCalled = false;
1745+
setTimeout(track_a_click, 1000); //如果没有回调成功,设置超时回调
1746+
function track_a_click(){
1747+
if (!hasCalled) {
1748+
hasCalled = true;
1749+
location.href = link.href; //把 A 链接的点击跳转,改成 location 的方式跳转
1750+
}
1751+
}
1752+
sd.track(event_name, event_prop, track_a_click); //把跳转操作加在callback里
1753+
});
1754+
1755+
};
1756+
1757+
16801758
/*
16811759
* @param {object} properties
16821760
* */
@@ -1920,7 +1998,6 @@ saEvent.send = function(p, callback) {
19201998
}
19211999
};
19222000

1923-
19242001
function app_js_bridge(){
19252002
var app_info = null;
19262003
var todo = null;

src/sensorsdata.full.js

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @fileoverview sensors analytic javascript sdk
33
44
*/
5+
try{
56

67
(function(sd) {
78

@@ -617,7 +618,7 @@ if(typeof JSON!=='object'){JSON={}}(function(){'use strict';var rx_one=/^[\],:{}
617618
, slice = ArrayProto.slice
618619
, toString = ObjProto.toString
619620
, hasOwnProperty = ObjProto.hasOwnProperty
620-
, LIB_VERSION = '1.6.16';
621+
, LIB_VERSION = '1.6.17';
621622

622623
sd.lib_version = LIB_VERSION;
623624

@@ -2209,6 +2210,58 @@ saEvent.send = function(p, callback) {
22092210
});
22102211
},
22112212
allTrack: function(){
2213+
// 避免没有ready
2214+
if(!document || !document.body){
2215+
setTimeout(this.allTrack,1000);
2216+
return false;
2217+
}
2218+
2219+
if(sd.allTrack === 'has_init'){
2220+
return false;
2221+
}
2222+
sd.allTrack = 'has_init';
2223+
2224+
2225+
var trackAll = {
2226+
clickEvents: function(e){
2227+
var props = {};
2228+
var target = e.target;
2229+
var tagName = target.tagName.toLowerCase();
2230+
if(' button a input '.indexOf(' '+ tagName + ' ') !== -1 ){
2231+
if(tagName === 'input'){
2232+
if(target.getAttribute('type') === 'button' || target.getAttribute('type') === 'submit'){
2233+
props.$el_value = target.value;
2234+
}else{
2235+
return false;
2236+
}
2237+
}
2238+
2239+
props.$el_tagName = tagName;
2240+
props.$el_name = target.getAttribute('name');
2241+
props.$el_id = target.getAttribute('id');
2242+
props.$el_className = typeof target.className === 'string' ? target.className : null;
2243+
props.$el_href = target.getAttribute('href');
2244+
2245+
// 获取内容
2246+
if (target.textContent) {
2247+
var textContent = _.trim(target.textContent);
2248+
if (textContent) {
2249+
textContent = textContent.replace(/[\r\n]/g, ' ').replace(/[ ]+/g, ' ').substring(0, 255);
2250+
}
2251+
props.$el_text = textContent;
2252+
}
2253+
props = _.strip_empty_properties(props);
2254+
2255+
props.$url = location.href;
2256+
props.$url_path = location.pathname;
2257+
2258+
sd.track('$web_event',props);
2259+
}
2260+
}
2261+
};
2262+
2263+
2264+
_.addEvent(document,'click',function(e){trackAll.clickEvents(e);});
22122265

22132266
},
22142267
autoTrackWithoutProfile:function(para){
@@ -2284,6 +2337,32 @@ saEvent.send = function(p, callback) {
22842337
}
22852338
};
22862339

2340+
// 跟踪链接
2341+
sd.trackLink = function(link,event_name,event_prop){
2342+
var ele = link;
2343+
event_prop = event_prop || {};
2344+
if(!link || (typeof link !== 'object')){
2345+
return false;
2346+
}
2347+
if (!link.href || /^javascript/.test(link.href) || link.target) {
2348+
return false;
2349+
}
2350+
_.addEvent(link,'click',function(e){
2351+
e.preventDefault(); // 阻止默认跳转
2352+
var hasCalled = false;
2353+
setTimeout(track_a_click, 1000); //如果没有回调成功,设置超时回调
2354+
function track_a_click(){
2355+
if (!hasCalled) {
2356+
hasCalled = true;
2357+
location.href = link.href; //把 A 链接的点击跳转,改成 location 的方式跳转
2358+
}
2359+
}
2360+
sd.track(event_name, event_prop, track_a_click); //把跳转操作加在callback里
2361+
});
2362+
2363+
};
2364+
2365+
22872366
/*
22882367
* @param {object} properties
22892368
* */
@@ -2527,7 +2606,6 @@ saEvent.send = function(p, callback) {
25272606
}
25282607
};
25292608

2530-
25312609
function app_js_bridge(){
25322610
var app_info = null;
25332611
var todo = null;
@@ -2645,3 +2723,20 @@ saEvent.send = function(p, callback) {
26452723

26462724

26472725
})(window['sensorsDataAnalytic201505']);
2726+
2727+
2728+
2729+
}catch(err){
2730+
(function(){
2731+
2732+
var sd = window['sensorsDataAnalytic201505'];
2733+
if(typeof sd === 'string'){
2734+
sd = window[sd];
2735+
if((sd != null) && (typeof sd === 'function' || typeof sd === 'object')){
2736+
sd.track && sd.track('_js_sdk_error',{_js_sdk_error_msg:err,$url:location.href});
2737+
}
2738+
}
2739+
2740+
2741+
})();
2742+
}

src/sensorsdata.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @fileoverview sensors analytic javascript sdk
33
44
*/
5+
try{
56

67
(function(sd) {
78

@@ -89,3 +90,20 @@
8990

9091

9192
})(window['sensorsDataAnalytic201505']);
93+
94+
95+
96+
}catch(err){
97+
(function(){
98+
99+
var sd = window['sensorsDataAnalytic201505'];
100+
if(typeof sd === 'string'){
101+
sd = window[sd];
102+
if((sd != null) && (typeof sd === 'function' || typeof sd === 'object')){
103+
sd.track && sd.track('_js_sdk_error',{_js_sdk_error_msg:err,$url:location.href});
104+
}
105+
}
106+
107+
108+
})();
109+
}

vtrack.min.js

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

0 commit comments

Comments
 (0)