Skip to content

Commit 15135a0

Browse files
author
shengyonggen
committed
Release 1.22.8
1 parent 0358658 commit 15135a0

19 files changed

+259
-42
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.22.8 (2022-05-31)
2+
1. 新增
3+
- 新增 `SessionEvent` 插件
4+
15
## 1.22.7 (2022-05-20)
26
1. 修复
37
- 修复 `iOS` 内嵌 `H5` 打通情况下补发 `$WebPageLeave` 时间不准确的问题

core/sensorsdata.amd.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.

core/sensorsdata.es6.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.

core/sensorsdata.min.js

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

core/sensorsdata.mtp.min.js

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

heatmap.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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sa-sdk-javascript",
3-
"version": "1.22.7",
3+
"version": "1.22.8",
44
"description": "official sensorsdata javascript sdk",
55
"main": "sensorsdata.min.js",
66
"scripts": {

plugins/session-event.js

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
sensorsDataAnalytic201505.modules['SessionEvent'] = (function () {
2+
'use strict';
3+
4+
/**
5+
* 添加自定义属性
6+
*
7+
* @param { JSON } data 数据日志
8+
* @param { Object } instance 当前 use 插件的实例
9+
* @return { JSON } 添加完毕后的数据日志
10+
*/
11+
function addProperties(data, instance) {
12+
if (data.type !== 'track') return data;
13+
var sd = instance.sd;
14+
var _ = sd._;
15+
var check = sd.saEvent.check;
16+
17+
// 克隆数据,阻止 hookRegister 中无法对原有 data 的更改
18+
var copyData = _.extend2Lev({ properties: {} }, data);
19+
var currentProps = instance.currentProps;
20+
var properties = copyData.properties;
21+
var event = copyData.event;
22+
var props = {};
23+
24+
_.each(currentProps, function (prop) {
25+
if (_.isObject(prop)) {
26+
if (prop.events.indexOf(event) > -1) {
27+
// 校验属性及属性值是否合法
28+
if (check({ properties: prop.properties })) {
29+
props = _.extend(props, prop.properties);
30+
}
31+
}
32+
} else if (_.isFunction(prop)) {
33+
var callbackProp = prop({
34+
event: event,
35+
properties: properties,
36+
data: copyData
37+
});
38+
// 校验属性及属性值是否合法
39+
if (_.isObject(callbackProp) && !_.isEmptyObject(callbackProp) && check({ properties: callbackProp })) {
40+
props = _.extend(props, callbackProp);
41+
}
42+
}
43+
});
44+
data.properties = _.extend(properties, props);
45+
return data;
46+
}
47+
48+
function DataStageImpl(registerInstance) {
49+
var _this = this;
50+
this.sd = registerInstance.sd;
51+
this.currentProps = registerInstance.customRegister;
52+
this.interceptor = {
53+
addCustomProps: {
54+
priority: 0,
55+
entry: function (data) {
56+
addProperties(data, _this);
57+
return data;
58+
}
59+
}
60+
};
61+
}
62+
/**
63+
* DataStageImpl 必须有 init 方法,可以为空方法
64+
*/
65+
DataStageImpl.prototype.init = function () {};
66+
67+
function registerPropertiesFeature(registerInstance) {
68+
// assign dataStage for registerFeature to find the dataStage implementation
69+
this.dataStage = new DataStageImpl(registerInstance);
70+
}
71+
72+
function RegisterProperties() {
73+
this.sd = null;
74+
this.log = (window.console && window.console.log) || function () {};
75+
this.customRegister = [];
76+
}
77+
RegisterProperties.prototype.init = function (sd) {
78+
if (sd) {
79+
this.sd = sd;
80+
this._ = sd._;
81+
this.log = sd.log;
82+
sd.registerFeature(new registerPropertiesFeature(this));
83+
} else {
84+
this.log('神策JS SDK未成功引入');
85+
}
86+
};
87+
88+
RegisterProperties.prototype.register = function (customProps) {
89+
if (!this.sd) {
90+
this.log('神策JS SDK未成功引入');
91+
return;
92+
}
93+
if (this._.isObject(customProps) && this._.isArray(customProps.events) && customProps.events.length > 0 && this._.isObject(customProps.properties) && !this._.isEmptyObject(customProps.properties)) {
94+
this.customRegister.push(customProps);
95+
} else {
96+
this.log('RegisterProperties: register 参数错误');
97+
}
98+
};
99+
100+
RegisterProperties.prototype.hookRegister = function (customFun) {
101+
if (!this.sd) {
102+
this.log('神策JS SDK未成功引入');
103+
return;
104+
}
105+
if (this._.isFunction(customFun)) {
106+
this.customRegister.push(customFun);
107+
} else {
108+
this.log('RegisterProperties: hookRegister 参数错误');
109+
}
110+
};
111+
112+
function Store(sd) {
113+
this.sd = sd;
114+
this._ = sd._;
115+
this.cookie_value = null;
116+
}
117+
Store.prototype.saveObjectVal = function (name, value) {
118+
if (!this._.isString(value)) {
119+
value = JSON.stringify(value);
120+
}
121+
if (this.sd.para.encrypt_cookie == true) {
122+
value = this._.encrypt(value);
123+
}
124+
if (this._.cookie.isSupport()) {
125+
this._.cookie.set(name, value);
126+
}
127+
this.cookie_value = value;
128+
};
129+
130+
Store.prototype.readObjectVal = function (name) {
131+
var value = this._.cookie.isSupport() ? this._.cookie.get(name) : this.cookie_value;
132+
if (!value) return null;
133+
value = this._.decryptIfNeeded(value);
134+
return this._.safeJSONParse(value);
135+
};
136+
137+
var COOKIE_NAME = 'sensorsdata2015jssdksession';
138+
139+
function SessionEvent() {
140+
this.registerProperties = null;
141+
this.store = null;
142+
this.sd = null;
143+
this._ = null;
144+
this.log = (window.console && window.console.log) || function () {};
145+
this.cookie_name = '';
146+
this.prop = {};
147+
}
148+
149+
SessionEvent.prototype.init = function (sd) {
150+
if (!sd || typeof sd !== 'object') {
151+
this.log('Session Event 插件初始化失败!');
152+
return;
153+
}
154+
var _this = this;
155+
this.sd = sd;
156+
this._ = sd._;
157+
this.log = sd.log;
158+
this.cookie_name = COOKIE_NAME + (sd.para.sdk_id || '');
159+
this.registerProperties = new RegisterProperties();
160+
this.registerProperties.init(sd);
161+
this.store = new Store(sd);
162+
this.registerProperties.hookRegister(function () {
163+
return _this.addSessionID();
164+
});
165+
};
166+
167+
SessionEvent.prototype.addSessionID = function () {
168+
var time = +new Date();
169+
this.prop = this.store.readObjectVal(this.cookie_name) || {};
170+
171+
var first_time = this.prop.first_session_time;
172+
var latest_time = this.prop.latest_session_time;
173+
// 切换逻辑:首次 || 向前调整时间 || 首次时间与当前时间差大于 12 小时 || 当前时间与上次触发时间大于30分钟
174+
if (!first_time || !latest_time || first_time > time || latest_time > time || time - first_time > 12 * 60 * 60 * 1000 || time - latest_time > 30 * 60 * 1000) {
175+
var uuid = this._.UUID();
176+
this.prop = {
177+
session_id: uuid.replace(/-/g, ''),
178+
first_session_time: time,
179+
latest_session_time: time
180+
};
181+
} else {
182+
this.prop.latest_session_time = time;
183+
}
184+
185+
this.store.saveObjectVal(this.cookie_name, this.prop);
186+
return {
187+
$event_session_id: this.prop.session_id
188+
};
189+
};
190+
191+
var instance = new SessionEvent();
192+
instance.__constructor__ = SessionEvent;
193+
// 对外公开 PageLoad 插件
194+
if (window.SensorsDataWebJSSDKPlugin && Object.prototype.toString.call(window.SensorsDataWebJSSDKPlugin) === '[object Object]') {
195+
window.SensorsDataWebJSSDKPlugin.SessionEvent = window.SensorsDataWebJSSDKPlugin.SessionEvent || instance;
196+
} else {
197+
window.SensorsDataWebJSSDKPlugin = {
198+
SessionEvent: instance
199+
};
200+
}
201+
202+
return instance;
203+
204+
}());

plugins/session-event.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.

product/heatmap.full.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9457,7 +9457,7 @@
94579457

94589458
window.sa_jssdk_heatmap_render = function(se, data, type, url) {
94599459
sd = se;
9460-
sd.heatmap_version = '1.22.7';
9460+
sd.heatmap_version = '1.22.8';
94619461
_ = sd._;
94629462
_.querySelectorAll = function(val) {
94639463
if (typeof val !== 'string') {

0 commit comments

Comments
 (0)