Skip to content

Commit 0fe9173

Browse files
author
shengyonggen
committed
全埋点的tracklink优化
1 parent 9e8ea69 commit 0fe9173

File tree

4 files changed

+163
-47
lines changed

4 files changed

+163
-47
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: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,42 +1613,56 @@ saEvent.send = function(p, callback) {
16131613
}
16141614
sd.allTrack = 'has_init';
16151615

1616-
16171616
var trackAll = {
1617+
getProps: function(tagName,target){
1618+
1619+
var props = {};
1620+
1621+
props._el_tagname = tagName;
1622+
props._el_name = target.getAttribute('name');
1623+
props._el_id = target.getAttribute('id');
1624+
props._el_classname = typeof target.className === 'string' ? target.className : null;
1625+
props._el_href = target.getAttribute('href');
1626+
1627+
// 获取内容
1628+
if (target.textContent) {
1629+
var textContent = _.trim(target.textContent);
1630+
if (textContent) {
1631+
textContent = textContent.replace(/[\r\n]/g, ' ').replace(/[ ]+/g, ' ').substring(0, 255);
1632+
}
1633+
props._el_value = textContent;
1634+
}
1635+
props = _.strip_empty_properties(props);
1636+
1637+
props.$url = location.href;
1638+
props.$url_path = location.pathname;
1639+
1640+
return props;
1641+
},
16181642
clickEvents: function(e){
16191643
var props = {};
16201644
var target = e.target;
1621-
var tagName = target.tagName.toLowerCase();
1645+
var tagName = target.tagName.toLowerCase();
1646+
16221647
if(' button a input '.indexOf(' '+ tagName + ' ') !== -1 ){
1648+
16231649
if(tagName === 'input'){
16241650
if(target.getAttribute('type') === 'button' || target.getAttribute('type') === 'submit'){
16251651
props._el_value = target.value;
16261652
}else{
16271653
return false;
16281654
}
16291655
}
1656+
1657+
_.extend(props, this.getProps(tagName,target));
16301658

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_value = textContent;
1659+
if(tagName === 'a'){
1660+
_.trackLink({event:e},'_web_event',props);
1661+
}else{
1662+
sd.track('_web_event',props);
16441663
}
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);
16511664
}
1665+
16521666
}
16531667
};
16541668

@@ -1729,8 +1743,52 @@ saEvent.send = function(p, callback) {
17291743
}
17301744
};
17311745

1732-
// 跟踪链接
1746+
_.trackLink = function(obj,event_name,event_prop){
1747+
obj = obj || {};
1748+
var link = null;
1749+
if(obj.ele){
1750+
link = obj.ele;
1751+
}
1752+
if(obj.event){
1753+
link = obj.event.target;
1754+
}
1755+
1756+
event_prop = event_prop || {};
1757+
if(!link || (typeof link !== 'object')){
1758+
return false;
1759+
}
1760+
// 如果是非当前页面会跳转的链接,直接track
1761+
if (!link.href || /^javascript/.test(link.href) || link.target) {
1762+
sd.track(event_name, event_prop);
1763+
return false;
1764+
}
1765+
function linkFunc(e){
1766+
e.preventDefault(); // 阻止默认跳转
1767+
var hasCalled = false;
1768+
function track_a_click(){
1769+
if (!hasCalled) {
1770+
hasCalled = true;
1771+
location.href = link.href; //把 A 链接的点击跳转,改成 location 的方式跳转
1772+
}
1773+
}
1774+
setTimeout(track_a_click, 1000); //如果没有回调成功,设置超时回调
1775+
sd.track(event_name, event_prop, track_a_click); //把跳转操作加在callback里
1776+
}
1777+
if(obj.event){
1778+
linkFunc(obj.event);
1779+
}
1780+
if(obj.ele){
1781+
_.addEvent(obj.ele,'click',function(e){
1782+
linkFunc(e);
1783+
});
1784+
}
1785+
};
1786+
17331787
sd.trackLink = function(link,event_name,event_prop){
1788+
_.trackLink({ele:link},event_name,event_prop);
1789+
};
1790+
// 跟踪链接
1791+
sd.trackLinks = function(link,event_name,event_prop){
17341792
var ele = link;
17351793
event_prop = event_prop || {};
17361794
if(!link || (typeof link !== 'object')){

src/sensorsdata.full.js

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,42 +2221,56 @@ saEvent.send = function(p, callback) {
22212221
}
22222222
sd.allTrack = 'has_init';
22232223

2224-
22252224
var trackAll = {
2225+
getProps: function(tagName,target){
2226+
2227+
var props = {};
2228+
2229+
props._el_tagname = tagName;
2230+
props._el_name = target.getAttribute('name');
2231+
props._el_id = target.getAttribute('id');
2232+
props._el_classname = typeof target.className === 'string' ? target.className : null;
2233+
props._el_href = target.getAttribute('href');
2234+
2235+
// 获取内容
2236+
if (target.textContent) {
2237+
var textContent = _.trim(target.textContent);
2238+
if (textContent) {
2239+
textContent = textContent.replace(/[\r\n]/g, ' ').replace(/[ ]+/g, ' ').substring(0, 255);
2240+
}
2241+
props._el_value = textContent;
2242+
}
2243+
props = _.strip_empty_properties(props);
2244+
2245+
props.$url = location.href;
2246+
props.$url_path = location.pathname;
2247+
2248+
return props;
2249+
},
22262250
clickEvents: function(e){
22272251
var props = {};
22282252
var target = e.target;
2229-
var tagName = target.tagName.toLowerCase();
2253+
var tagName = target.tagName.toLowerCase();
2254+
22302255
if(' button a input '.indexOf(' '+ tagName + ' ') !== -1 ){
2256+
22312257
if(tagName === 'input'){
22322258
if(target.getAttribute('type') === 'button' || target.getAttribute('type') === 'submit'){
22332259
props._el_value = target.value;
22342260
}else{
22352261
return false;
22362262
}
22372263
}
2264+
2265+
_.extend(props, this.getProps(tagName,target));
22382266

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_value = textContent;
2267+
if(tagName === 'a'){
2268+
_.trackLink({event:e},'_web_event',props);
2269+
}else{
2270+
sd.track('_web_event',props);
22522271
}
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);
22592272
}
2273+
22602274
}
22612275
};
22622276

@@ -2337,8 +2351,52 @@ saEvent.send = function(p, callback) {
23372351
}
23382352
};
23392353

2340-
// 跟踪链接
2354+
_.trackLink = function(obj,event_name,event_prop){
2355+
obj = obj || {};
2356+
var link = null;
2357+
if(obj.ele){
2358+
link = obj.ele;
2359+
}
2360+
if(obj.event){
2361+
link = obj.event.target;
2362+
}
2363+
2364+
event_prop = event_prop || {};
2365+
if(!link || (typeof link !== 'object')){
2366+
return false;
2367+
}
2368+
// 如果是非当前页面会跳转的链接,直接track
2369+
if (!link.href || /^javascript/.test(link.href) || link.target) {
2370+
sd.track(event_name, event_prop);
2371+
return false;
2372+
}
2373+
function linkFunc(e){
2374+
e.preventDefault(); // 阻止默认跳转
2375+
var hasCalled = false;
2376+
function track_a_click(){
2377+
if (!hasCalled) {
2378+
hasCalled = true;
2379+
location.href = link.href; //把 A 链接的点击跳转,改成 location 的方式跳转
2380+
}
2381+
}
2382+
setTimeout(track_a_click, 1000); //如果没有回调成功,设置超时回调
2383+
sd.track(event_name, event_prop, track_a_click); //把跳转操作加在callback里
2384+
}
2385+
if(obj.event){
2386+
linkFunc(obj.event);
2387+
}
2388+
if(obj.ele){
2389+
_.addEvent(obj.ele,'click',function(e){
2390+
linkFunc(e);
2391+
});
2392+
}
2393+
};
2394+
23412395
sd.trackLink = function(link,event_name,event_prop){
2396+
_.trackLink({ele:link},event_name,event_prop);
2397+
};
2398+
// 跟踪链接
2399+
sd.trackLinks = function(link,event_name,event_prop){
23422400
var ele = link;
23432401
event_prop = event_prop || {};
23442402
if(!link || (typeof link !== 'object')){

vtrack.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)