Skip to content

Commit 50b4b77

Browse files
author
shengyonggen
committed
更新1.5.4
1 parent 0330bb1 commit 50b4b77

File tree

6 files changed

+119
-12
lines changed

6 files changed

+119
-12
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ Sensors Analytics JavaScript SDK
1616

1717
# 代码埋点最佳实践
1818
1. 控制台: 在代码埋点时,在控制台会打出 console 。每次 track 都会打出一个对象,里面 event 是你设置的事件名,注意观察数据是否和自己想要的一致!如果没出现,就是埋点失败!同时注意观察控制台的报错,会提示各种错误信息,比如事件名不合法,或者属性值无效等!
19-
2. 后端验证: 设置 debug_mode:true。这里除了会打 console 外,还会发一次 ajax 请求,在后端会再次验证数据是否合法
20-
3. 左下角的埋点管理: 这里会汇总数据的错误
19+
2. 左下角的埋点管理: 这里会汇总数据的错误
20+
3. 后端验证: 设置 debug_mode:true。这里除了会打 console 外,还会发一次 ajax 请求,在后端会再次验证数据是否合法
2121

2222
# 使用说明
2323
1. /product下的是 代码埋点的最新源文件,欢迎提交修改。可视化埋点较为复杂,没有提供源文件,且不推荐使用。
2424
2. /dist下的是 可视化埋点和代码埋点的可用发行版文件。请将 /dist/版本号 下的文件都下载到你们自己网站目录下面!!!
2525
4. ***升级使用新版 SDK 前,请在微信群里先问下你们的神策分析系统版本是否支持!!!***
2626

27+
####1.5.4
28+
增加自定义来源渠道参数source_channel,自定义渠道的参数也会被加到$pageview中,增加$is_first_time,增加异常检测(distinct_id为null的问题),$is_first_day如果为假时候,也会传属性false。把$pageview的$browser_language属性改成用户属性。
2729
#####1.5.3
2830
可视化埋点定义模式下修复找不到元素时控制台报错的问题,修复定义模式下iframe非本域时加载脚本时控制台报错的问题。
2931
#####1.5.2

dist/1.5.4/sensorsdata.min.js

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

dist/1.5.4/vendor.min.css

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

dist/1.5.4/vendor.min.js

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

dist/1.5.4/vtrack.min.js

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

product/sensorsdata.js

Lines changed: 106 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ if(typeof JSON!=='object'){JSON={}}(function(){'use strict';var rx_one=/^[\],:{}
2929
session_time: 0,
3030

3131
use_client_time: false,
32+
//来源参数名字
33+
source_channel:[],
34+
3235
// 七鱼过滤id
3336
vtrack_ignore:{}
3437

@@ -601,12 +604,16 @@ if(typeof JSON!=='object'){JSON={}}(function(){'use strict';var rx_one=/^[\],:{}
601604
, slice = ArrayProto.slice
602605
, toString = ObjProto.toString
603606
, hasOwnProperty = ObjProto.hasOwnProperty
604-
, LIB_VERSION = '1.5.3';
607+
, LIB_VERSION = '1.5.4';
605608

606609
// 提供错误日志
607610
var error_msg = [];
608611
var is_first_visitor = false;
609612

613+
// 标准广告系列来源
614+
var source_channel_standard = 'utm_source utm_medium utm_campaign utm_content utm_term';
615+
616+
610617
var logger = typeof logger === 'object' ? logger : {};
611618
logger.info = function() {
612619
if(!sd.para.show_log){
@@ -833,6 +840,19 @@ if(typeof JSON!=='object'){JSON={}}(function(){'use strict';var rx_one=/^[\],:{}
833840
});
834841
}
835842
};
843+
// 数组去重复
844+
_.unique = function (ar){
845+
var temp,n=[],o= {};
846+
for(var i = 0; i < ar.length; i++){
847+
temp = ar[i];
848+
if(!(temp in o)){
849+
o[temp] = true;
850+
n.push(temp);
851+
}
852+
}
853+
return n;
854+
};
855+
836856

837857
// 只能是sensors满足的数据格式
838858
_.strip_sa_properties = function(p) {
@@ -1256,9 +1276,13 @@ if(typeof JSON!=='object'){JSON={}}(function(){'use strict';var rx_one=/^[\],:{}
12561276

12571277
_.info = {
12581278
campaignParams: function() {
1259-
var campaign_keywords = 'utm_source utm_medium utm_campaign utm_content utm_term'.split(' ')
1279+
var campaign_keywords = source_channel_standard.split(' ')
12601280
, kw = ''
12611281
, params = {};
1282+
if(_.isArray(sd.para.source_channel) && sd.para.source_channel.length >0 ){
1283+
campaign_keywords = campaign_keywords.concat(sd.para.source_channel);
1284+
campaign_keywords = _.unique(campaign_keywords);
1285+
}
12621286
_.each(campaign_keywords, function(kwkey) {
12631287
kw = _.getQueryParam(location.href, kwkey);
12641288
if (kw.length) {
@@ -1358,8 +1382,23 @@ if(typeof JSON!=='object'){JSON={}}(function(){'use strict';var rx_one=/^[\],:{}
13581382
// 检查是否是新用户(第一次种cookie,且在8个小时内的)
13591383
var saNewUser = {
13601384
checkIsAddSign: function(data){
1361-
if(data.type === 'track' && _.cookie.get('sensorsdata_is_new_user') !== null){
1362-
data.properties.$is_first_day = true;
1385+
if(data.type === 'track'){
1386+
if(_.cookie.get('sensorsdata_is_new_user') !== null){
1387+
data.properties.$is_first_day = true;
1388+
}else{
1389+
data.properties.$is_first_day = false;
1390+
}
1391+
}
1392+
},
1393+
is_first_visit_time: false,
1394+
checkIsFirstTime: function(data){
1395+
if(data.type === 'track'){
1396+
if(this.is_first_visit_time){
1397+
data.properties.$is_first_time = true;
1398+
this.is_first_visit_time = false;
1399+
}else{
1400+
data.properties.$is_first_time = false;
1401+
}
13631402
}
13641403
},
13651404
storeInitCheck: function(){
@@ -1372,9 +1411,20 @@ if(typeof JSON!=='object'){JSON={}}(function(){'use strict';var rx_one=/^[\],:{}
13721411
s: 59 - date.getSeconds()
13731412
};
13741413
_.cookie.set('sensorsdata_is_new_user','true',obj.h*3600+obj.m*60+obj.s+'s');
1414+
// 如果是is_first_visit_time,且第一次,那就发数据
1415+
this.is_first_visit_time = true;
13751416
}else{
1417+
// 如果没有这个cookie,肯定不是首日
13761418
if(_.cookie.get('sensorsdata_is_new_user') === null){
1377-
this.checkIsAddSign = function(){};
1419+
this.checkIsAddSign = function(data){
1420+
if(data.type === 'track'){
1421+
data.properties.$is_first_day = false;
1422+
}
1423+
};
1424+
}
1425+
// 如果不是第一次打开的用户,肯定不是首次访问
1426+
this.checkIsFirstTime = function(data){
1427+
data.properties.$is_first_time = false;
13781428
}
13791429
}
13801430
}
@@ -1486,11 +1536,52 @@ if(typeof JSON!=='object'){JSON={}}(function(){'use strict';var rx_one=/^[\],:{}
14861536
distinct_id: store.getDistinctId(),
14871537
properties: {}
14881538
};
1489-
if(error_msg.length > 0){
1490-
data.jssdk_error = error_msg.join('--');
1539+
1540+
// 测试部分数据没有distinct_id的问题
1541+
if(typeof store.getDistinctId() !== 'string' || typeof store.getDistinctId() === ''){
1542+
var wrong_case = '';
1543+
switch (store.getDistinctId()) {
1544+
case null :
1545+
wrong_case = 'null';
1546+
break;
1547+
case (void 0) :
1548+
wrong_case = 'undefined';
1549+
break;
1550+
case '':
1551+
wrong_case = '空';
1552+
break;
1553+
default:
1554+
wrong_case = String(store.getDistinctId());
1555+
}
1556+
error_msg.push('distinct_id_wrong' + wrong_case + '-' + (new Date()).getTime());
14911557
}
14921558

14931559
_.extend(data, p);
1560+
1561+
// 测试部分数据没有distinct_id的问题
1562+
if(typeof data.distinct_id !== 'string' || typeof data.distinct_id === ''){
1563+
var wrong_case = '';
1564+
switch (data.distinct_id) {
1565+
case null :
1566+
wrong_case = 'null';
1567+
break;
1568+
case (void 0) :
1569+
wrong_case = 'undefined';
1570+
break;
1571+
case '':
1572+
wrong_case = '空';
1573+
break;
1574+
default:
1575+
wrong_case = String(data.distinct_id);
1576+
}
1577+
error_msg.push('distinct_id_wrong' + wrong_case + '-' + (new Date()).getTime());
1578+
}
1579+
1580+
if(error_msg.length > 0){
1581+
data.jssdk_error = error_msg.join('--');
1582+
}
1583+
1584+
14941585
if (_.isObject(p.properties) && !_.isEmptyObject(p.properties)) {
14951586
_.extend(data.properties, p.properties);
14961587
}
@@ -1511,6 +1602,7 @@ if(typeof JSON!=='object'){JSON={}}(function(){'use strict';var rx_one=/^[\],:{}
15111602
_.searchObjDate(data);
15121603
//判断是否要给数据增加新用户属性
15131604
saNewUser.checkIsAddSign(data);
1605+
saNewUser.checkIsFirstTime(data);
15141606

15151607
if (sd.para.debug_mode === true){
15161608
logger.info(data);
@@ -1680,13 +1772,18 @@ if(typeof JSON!=='object'){JSON={}}(function(){'use strict';var rx_one=/^[\],:{}
16801772
var utms = _.info.campaignParams();
16811773
var $utms = {};
16821774
for(var i in utms){
1683-
$utms['$'+i] = utms[i];
1775+
if((' ' + source_channel_standard + ' ').indexOf(' ' + i + ' ') !== -1){
1776+
$utms['$'+i] = utms[i];
1777+
}else{
1778+
$utms[i] = utms[i];
1779+
}
16841780
}
16851781
// setOnceProfile
16861782
if(is_first_visitor){
16871783
sd.setOnceProfile(_.extend({
16881784
$first_visit_time: new Date(),
16891785
$first_referrer: document.referrer,
1786+
$first_browser_language: navigator.language,
16901787
$first_referrer_host: _.info.referringDomain(document.referrer)
16911788
},$utms)
16921789
);
@@ -1697,8 +1794,7 @@ if(typeof JSON!=='object'){JSON={}}(function(){'use strict';var rx_one=/^[\],:{}
16971794
$referrer_host: _.info.referringDomain(document.referrer),
16981795
$url: location.href,
16991796
$url_path: location.pathname,
1700-
$title: document.title,
1701-
$browser_language: navigator.language
1797+
$title: document.title
17021798
},$utms)
17031799
);
17041800
}

0 commit comments

Comments
 (0)