@rk911/tracker-sdk 1.6.8 → 1.6.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/session.js +30 -6
- package/core/tracker.js +16 -4
- package/index.js +23 -1
- package/package.json +1 -1
package/core/session.js
CHANGED
|
@@ -10,6 +10,9 @@ export class SessionManager {
|
|
|
10
10
|
this.sequence = 0
|
|
11
11
|
this.userId = null
|
|
12
12
|
this.deviceId = null
|
|
13
|
+
this.isNativeSessionId = false
|
|
14
|
+
this.isNativeDeviceId = false
|
|
15
|
+
this.isNativeUserId = false
|
|
13
16
|
this._storageKey = config.session.storageKey || '_tracker_session'
|
|
14
17
|
this._renewListeners = []
|
|
15
18
|
|
|
@@ -32,6 +35,7 @@ export class SessionManager {
|
|
|
32
35
|
getSessionId() {
|
|
33
36
|
if (!this.sessionId || this._isExpired()) {
|
|
34
37
|
var wasRenew = !!this.sessionId
|
|
38
|
+
this.isNativeSessionId = false
|
|
35
39
|
this._createNew()
|
|
36
40
|
if (wasRenew) this._notifyRenew()
|
|
37
41
|
}
|
|
@@ -56,15 +60,34 @@ export class SessionManager {
|
|
|
56
60
|
this._save()
|
|
57
61
|
}
|
|
58
62
|
|
|
59
|
-
setUser(userId) {
|
|
63
|
+
setUser(userId, isNative = false) {
|
|
60
64
|
this.userId = userId
|
|
65
|
+
this.isNativeUserId = !!isNative
|
|
66
|
+
if (userId && !isNative) {
|
|
67
|
+
this.adapter.setStorage('userId', userId)
|
|
68
|
+
}
|
|
61
69
|
this._save()
|
|
62
70
|
}
|
|
63
71
|
|
|
64
|
-
setDeviceId(deviceId) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
72
|
+
setDeviceId(deviceId, isNative = false) {
|
|
73
|
+
if (deviceId) {
|
|
74
|
+
this.deviceId = deviceId
|
|
75
|
+
this.isNativeDeviceId = !!isNative
|
|
76
|
+
if (!isNative) {
|
|
77
|
+
this.adapter.setStorage(DEVICE_ID_KEY, deviceId)
|
|
78
|
+
this.adapter.setStorage('deviceId', deviceId)
|
|
79
|
+
}
|
|
80
|
+
this._save()
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
setSessionId(sessionId, isNative = false) {
|
|
85
|
+
if (sessionId) {
|
|
86
|
+
this.sessionId = sessionId
|
|
87
|
+
this.isNativeSessionId = !!isNative
|
|
88
|
+
this.lastActivityTime = this.adapter.getTimestamp()
|
|
89
|
+
this._save()
|
|
90
|
+
}
|
|
68
91
|
}
|
|
69
92
|
|
|
70
93
|
getUserId() {
|
|
@@ -95,13 +118,14 @@ export class SessionManager {
|
|
|
95
118
|
startTime: this.startTime,
|
|
96
119
|
lastActivityTime: this.lastActivityTime,
|
|
97
120
|
sequence: this.sequence,
|
|
121
|
+
isNativeSessionId: !!this.isNativeSessionId,
|
|
98
122
|
}
|
|
99
123
|
this.adapter.setSessionStorage(this._storageKey, data)
|
|
100
124
|
}
|
|
101
125
|
|
|
102
126
|
_restore() {
|
|
103
127
|
var data = this.adapter.getSessionStorage(this._storageKey)
|
|
104
|
-
if (data && data.sessionId) {
|
|
128
|
+
if (data && data.sessionId && !data.isNativeSessionId) {
|
|
105
129
|
this.sessionId = data.sessionId
|
|
106
130
|
this.startTime = data.startTime
|
|
107
131
|
this.lastActivityTime = data.lastActivityTime
|
package/core/tracker.js
CHANGED
|
@@ -200,12 +200,24 @@ export class Tracker {
|
|
|
200
200
|
|
|
201
201
|
// ---- 用户标识 ----
|
|
202
202
|
|
|
203
|
-
setUser(userId) {
|
|
204
|
-
this.session.setUser(userId)
|
|
203
|
+
setUser(userId, isNative = false) {
|
|
204
|
+
this.session.setUser(userId, isNative)
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
-
setDeviceId(deviceId) {
|
|
208
|
-
this.session.setDeviceId(deviceId)
|
|
207
|
+
setDeviceId(deviceId, isNative = false) {
|
|
208
|
+
this.session.setDeviceId(deviceId, isNative)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
setSessionId(sessionId, isNative = false) {
|
|
212
|
+
this.session.setSessionId(sessionId, isNative)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
getDeviceId() {
|
|
216
|
+
return this.session.getDeviceId()
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
getSessionId() {
|
|
220
|
+
return this.session.getSessionId()
|
|
209
221
|
}
|
|
210
222
|
|
|
211
223
|
setUserProperties(properties) {
|
package/index.js
CHANGED
|
@@ -89,6 +89,15 @@ export function createTracker(config) {
|
|
|
89
89
|
var adapter = createAdapter(platform, config)
|
|
90
90
|
trackerInstance = new Tracker(config, adapter)
|
|
91
91
|
|
|
92
|
+
// 触发 onBeforeInit 钩子(必须在插件注册与首屏事件触发前执行,以便同步/异步注入 Native 上下文)
|
|
93
|
+
if (typeof config.onBeforeInit === 'function') {
|
|
94
|
+
try {
|
|
95
|
+
config.onBeforeInit(trackerInstance)
|
|
96
|
+
} catch (e) {
|
|
97
|
+
console.warn('[TrackerSDK] onBeforeInit error:', e)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
92
101
|
// 注册插件
|
|
93
102
|
var pluginNames = config.plugins || []
|
|
94
103
|
pluginNames.forEach(function (pluginName) {
|
|
@@ -125,7 +134,14 @@ export function getTracker() {
|
|
|
125
134
|
function registerDeviceIfNeeded(tracker) {
|
|
126
135
|
var adapter = tracker.getAdapter()
|
|
127
136
|
var config = tracker.getConfig()
|
|
128
|
-
var
|
|
137
|
+
var session = tracker.getSession()
|
|
138
|
+
|
|
139
|
+
// 如果是由 App/Native 动态注入的临时 DeviceID,跳过 H5 本地注册逻辑
|
|
140
|
+
if (session.isNativeDeviceId && session.getDeviceId()) {
|
|
141
|
+
return
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
var existingId = session.getDeviceId() || adapter.getStorage('deviceId') || adapter.getStorage('_tracker_device_id')
|
|
129
145
|
if (existingId) {
|
|
130
146
|
tracker.setDeviceId(existingId)
|
|
131
147
|
return
|
|
@@ -188,6 +204,12 @@ function registerDeviceIfNeeded(tracker) {
|
|
|
188
204
|
* 不依赖内存缓存,确保清除存储后不会残留旧用户、切换账号后能及时更新
|
|
189
205
|
*/
|
|
190
206
|
function _ensureUserId(tracker) {
|
|
207
|
+
var session = tracker.getSession()
|
|
208
|
+
// 如果当前 userId 是由 Native 拼接注入的临时账号,不被 H5 本地 Cookie/Storage 覆写
|
|
209
|
+
if (session.isNativeUserId && session.getUserId()) {
|
|
210
|
+
return
|
|
211
|
+
}
|
|
212
|
+
|
|
191
213
|
var adapter = tracker.getAdapter()
|
|
192
214
|
// 从源头实时读取(登录页写入了 uni.setStorageSync('userId', ...))
|
|
193
215
|
var uid = adapter.getStorage('userId')
|