@jasolar/tracker-sdk 1.0.7 → 1.0.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/dist/index.cjs.js +195 -9
- package/dist/index.esm.js +195 -9
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -156,8 +156,10 @@ var Queue = /*#__PURE__*/function () {
|
|
|
156
156
|
var _this2 = this;
|
|
157
157
|
var isFlush = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
158
158
|
if (!this.list.length) {
|
|
159
|
+
console.log('[Tracker SDK] Queue flush called but queue is empty');
|
|
159
160
|
return;
|
|
160
161
|
}
|
|
162
|
+
console.log('[Tracker SDK] Flushing queue, items:', this.list.length, 'isFlush:', isFlush);
|
|
161
163
|
|
|
162
164
|
// 复制一份数据列表,防止在发送过程中 list 被修改
|
|
163
165
|
var dataToSend = _toConsumableArray(this.list);
|
|
@@ -165,9 +167,11 @@ var Queue = /*#__PURE__*/function () {
|
|
|
165
167
|
|
|
166
168
|
try {
|
|
167
169
|
// 逐条发送数据,而不是批量发送
|
|
168
|
-
dataToSend.forEach(function (item) {
|
|
170
|
+
dataToSend.forEach(function (item, index) {
|
|
171
|
+
console.log("[Tracker SDK] Sending event ".concat(index + 1, "/").concat(dataToSend.length, ":"), item.event);
|
|
169
172
|
_this2.flushFn(item, isFlush);
|
|
170
173
|
});
|
|
174
|
+
console.log('[Tracker SDK] All events sent successfully');
|
|
171
175
|
} catch (error) {
|
|
172
176
|
console.error("[Tracker SDK] Failed to flush data:", error);
|
|
173
177
|
} finally {
|
|
@@ -196,7 +200,17 @@ var Queue = /*#__PURE__*/function () {
|
|
|
196
200
|
function send(url, data) {
|
|
197
201
|
var token = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
198
202
|
var useBeacon = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
199
|
-
if (!url)
|
|
203
|
+
if (!url) {
|
|
204
|
+
console.warn('[Tracker SDK] Send called without URL');
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
console.log('[Tracker SDK] Sending data:', {
|
|
208
|
+
url: url,
|
|
209
|
+
event: data.event,
|
|
210
|
+
module: data.module,
|
|
211
|
+
useBeacon: useBeacon,
|
|
212
|
+
hasToken: !!token
|
|
213
|
+
});
|
|
200
214
|
var body = JSON.stringify(data);
|
|
201
215
|
|
|
202
216
|
// 构造请求头
|
|
@@ -209,6 +223,7 @@ function send(url, data) {
|
|
|
209
223
|
|
|
210
224
|
// 1. 如果强制使用 Beacon 或 fetch 不可用,直接使用 sendBeacon
|
|
211
225
|
if (useBeacon && typeof navigator !== 'undefined' && navigator.sendBeacon) {
|
|
226
|
+
console.log('[Tracker SDK] Using sendBeacon for transport');
|
|
212
227
|
var beaconUrl = url;
|
|
213
228
|
if (token) {
|
|
214
229
|
var separator = url.includes('?') ? '&' : '?';
|
|
@@ -217,7 +232,9 @@ function send(url, data) {
|
|
|
217
232
|
var blob = new Blob([body], {
|
|
218
233
|
type: 'application/json'
|
|
219
234
|
});
|
|
220
|
-
|
|
235
|
+
var result = navigator.sendBeacon(beaconUrl, blob);
|
|
236
|
+
console.log('[Tracker SDK] sendBeacon result:', result);
|
|
237
|
+
return result;
|
|
221
238
|
}
|
|
222
239
|
|
|
223
240
|
// 2. 正常场景优先使用 fetch + keepalive
|
|
@@ -347,6 +364,17 @@ function refreshSession() {
|
|
|
347
364
|
storage.set(SESSION_TIME_KEY, now());
|
|
348
365
|
}
|
|
349
366
|
|
|
367
|
+
/**
|
|
368
|
+
* 手动重置 session(比如用户登录切换)。
|
|
369
|
+
* @returns {string} 新的 session_id
|
|
370
|
+
*/
|
|
371
|
+
function resetSession() {
|
|
372
|
+
var newSid = uuid();
|
|
373
|
+
storage.set(SESSION_KEY, newSid);
|
|
374
|
+
storage.set(SESSION_TIME_KEY, now());
|
|
375
|
+
return newSid;
|
|
376
|
+
}
|
|
377
|
+
|
|
350
378
|
/**
|
|
351
379
|
* 节流函数:在指定的时间间隔内,无论触发多少次,函数只执行一次。
|
|
352
380
|
* 包含最后一次触发的延迟执行(trailing edge)。
|
|
@@ -382,7 +410,8 @@ function throttle(fn, delay) {
|
|
|
382
410
|
};
|
|
383
411
|
}
|
|
384
412
|
|
|
385
|
-
var _excluded = ["system", "module", "sub_module"]
|
|
413
|
+
var _excluded = ["system", "module", "sub_module"],
|
|
414
|
+
_excluded2 = ["enterTime"];
|
|
386
415
|
|
|
387
416
|
/**
|
|
388
417
|
* Tracker 核心类
|
|
@@ -394,6 +423,7 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
394
423
|
* @param {number} [options.delay=1000] - 批量上报延迟
|
|
395
424
|
* @param {number} [options.maxBatchSize=10] - 批量上报最大条数
|
|
396
425
|
* @param {string} [options.token] - 数据上报接口的 Token
|
|
426
|
+
* @param {number} [options.sessionTimeout=1800000] - Session 超时时间(毫秒),默认30分钟
|
|
397
427
|
*/
|
|
398
428
|
function Tracker() {
|
|
399
429
|
var _this = this;
|
|
@@ -404,7 +434,8 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
404
434
|
}
|
|
405
435
|
this.options = _objectSpread2({
|
|
406
436
|
delay: 1000,
|
|
407
|
-
maxBatchSize: 10
|
|
437
|
+
maxBatchSize: 10,
|
|
438
|
+
sessionTimeout: 30 * 60 * 1000
|
|
408
439
|
}, options);
|
|
409
440
|
this.deviceId = uuid();
|
|
410
441
|
this.sessionId = getSessionId();
|
|
@@ -412,6 +443,12 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
412
443
|
this.globalExt = {};
|
|
413
444
|
this.current = null;
|
|
414
445
|
this.lastActiveTime = now();
|
|
446
|
+
|
|
447
|
+
// 超时检测定时器
|
|
448
|
+
this.timeoutTimer = null;
|
|
449
|
+
|
|
450
|
+
// 标记是否因超时而离开(用于重新激活时判断)
|
|
451
|
+
this.isTimedOut = false;
|
|
415
452
|
this.queue = new Queue(function (data, isFlush) {
|
|
416
453
|
if (_this.options.endPoint) {
|
|
417
454
|
// 如果是 flush 触发的(通常是页面关闭),强制使用 sendBeacon
|
|
@@ -426,6 +463,9 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
426
463
|
// 自动绑定活跃检测和生命周期监听
|
|
427
464
|
this._bindActivity();
|
|
428
465
|
this._bindLifecycle();
|
|
466
|
+
|
|
467
|
+
// 启动超时检测
|
|
468
|
+
this._startTimeoutCheck();
|
|
429
469
|
}
|
|
430
470
|
|
|
431
471
|
/***********插件**********/
|
|
@@ -487,6 +527,9 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
487
527
|
enterTime: now()
|
|
488
528
|
});
|
|
489
529
|
|
|
530
|
+
// 清除超时标记
|
|
531
|
+
this.isTimedOut = false;
|
|
532
|
+
|
|
490
533
|
// 构建 context 对象
|
|
491
534
|
var context = {
|
|
492
535
|
device_id: this.deviceId,
|
|
@@ -514,16 +557,26 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
514
557
|
/**
|
|
515
558
|
* 记录离开模块
|
|
516
559
|
* @param {boolean} force - 是否强制使用当前时间作为结束时间(用于页面卸载等场景)
|
|
560
|
+
* @param {boolean} isTimeout - 是否因为超时而离开(内部使用)
|
|
517
561
|
*/
|
|
518
562
|
}, {
|
|
519
563
|
key: "leave",
|
|
520
564
|
value: function leave() {
|
|
521
565
|
var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
566
|
+
var isTimeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
522
567
|
if (!this.current) {
|
|
568
|
+
console.log('[Tracker SDK] Leave called but no current module');
|
|
523
569
|
return;
|
|
524
570
|
}
|
|
525
571
|
var endTime = force ? now() : this.lastActiveTime;
|
|
526
572
|
var duration = Math.max(0, endTime - this.current.enterTime);
|
|
573
|
+
console.log('[Tracker SDK] Leaving module:', {
|
|
574
|
+
module: this.current.module,
|
|
575
|
+
system: this.current.system,
|
|
576
|
+
duration: duration,
|
|
577
|
+
force: force,
|
|
578
|
+
isTimeout: isTimeout
|
|
579
|
+
});
|
|
527
580
|
|
|
528
581
|
// 构建 context 对象
|
|
529
582
|
var context = {
|
|
@@ -544,8 +597,18 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
544
597
|
context: context,
|
|
545
598
|
ext: extData
|
|
546
599
|
};
|
|
600
|
+
console.log('[Tracker SDK] Pushing module_leave event to queue:', event);
|
|
547
601
|
this.queue.push(event);
|
|
548
|
-
|
|
602
|
+
|
|
603
|
+
// 如果是因为超时而离开,保留 current 信息以便重新激活时使用
|
|
604
|
+
if (isTimeout) {
|
|
605
|
+
this.isTimedOut = true;
|
|
606
|
+
console.log('[Tracker SDK] Module info preserved for re-activation');
|
|
607
|
+
} else {
|
|
608
|
+
// 正常离开,清除 current
|
|
609
|
+
this.current = null;
|
|
610
|
+
this.isTimedOut = false;
|
|
611
|
+
}
|
|
549
612
|
}
|
|
550
613
|
|
|
551
614
|
/**
|
|
@@ -593,8 +656,44 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
593
656
|
value: function _bindActivity() {
|
|
594
657
|
var _this2 = this;
|
|
595
658
|
var update = function update() {
|
|
659
|
+
var wasInactive = _this2._checkInactive();
|
|
596
660
|
_this2.lastActiveTime = now();
|
|
597
661
|
refreshSession(); // 用户活跃时刷新 Session 有效期
|
|
662
|
+
|
|
663
|
+
// 如果之前因为超时而离开了,现在重新激活,需要重新 enter
|
|
664
|
+
if (wasInactive && _this2.isTimedOut && _this2.current) {
|
|
665
|
+
console.log('[Tracker SDK] User became active again after timeout');
|
|
666
|
+
|
|
667
|
+
// 重置超时标记
|
|
668
|
+
_this2.isTimedOut = false;
|
|
669
|
+
|
|
670
|
+
// 重新记录进入时间
|
|
671
|
+
_this2.current.enterTime = now();
|
|
672
|
+
|
|
673
|
+
// 构建 context 对象
|
|
674
|
+
var context = {
|
|
675
|
+
device_id: _this2.deviceId,
|
|
676
|
+
session_id: _this2.sessionId,
|
|
677
|
+
user_id: _this2.userId
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
// 构建 ext 对象
|
|
681
|
+
var extData = _objectSpread2(_objectSpread2({}, _this2.globalExt), _this2.current.ext);
|
|
682
|
+
|
|
683
|
+
// 上报 module_enter 事件
|
|
684
|
+
var event = {
|
|
685
|
+
system: _this2.current.system || '',
|
|
686
|
+
module: _this2.current.module || '',
|
|
687
|
+
sub_module: _this2.current.sub_module || '',
|
|
688
|
+
duration: 0,
|
|
689
|
+
event: 'module_enter',
|
|
690
|
+
timestamp: formatTime(now()),
|
|
691
|
+
context: context,
|
|
692
|
+
ext: extData
|
|
693
|
+
};
|
|
694
|
+
_this2.queue.push(event);
|
|
695
|
+
console.log('[Tracker SDK] Re-entered module after inactivity:', _this2.current.module);
|
|
696
|
+
}
|
|
598
697
|
};
|
|
599
698
|
var events = ['click', 'keydown', 'touchstart'];
|
|
600
699
|
events.forEach(function (event) {
|
|
@@ -612,20 +711,107 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
612
711
|
});
|
|
613
712
|
}
|
|
614
713
|
|
|
714
|
+
/**
|
|
715
|
+
* 检查用户是否处于非活跃状态(超过超时时间)
|
|
716
|
+
* @returns {boolean} 是否是非活跃状态
|
|
717
|
+
*/
|
|
718
|
+
}, {
|
|
719
|
+
key: "_checkInactive",
|
|
720
|
+
value: function _checkInactive() {
|
|
721
|
+
var inactiveTime = now() - this.lastActiveTime;
|
|
722
|
+
return inactiveTime >= this.options.sessionTimeout;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* 启动超时检测定时器
|
|
727
|
+
*/
|
|
728
|
+
}, {
|
|
729
|
+
key: "_startTimeoutCheck",
|
|
730
|
+
value: function _startTimeoutCheck() {
|
|
731
|
+
var _this3 = this;
|
|
732
|
+
// 动态计算检测间隔:超时时间的 1/6,但不超过 30 秒,不少于 5 秒
|
|
733
|
+
var checkInterval = Math.min(Math.max(this.options.sessionTimeout / 6, 5 * 1000), 30 * 1000);
|
|
734
|
+
console.log('[Tracker SDK] Timeout check interval:', (checkInterval / 1000).toFixed(1), 's');
|
|
735
|
+
this.timeoutTimer = setInterval(function () {
|
|
736
|
+
// 只要有 current 且未处于超时状态,就检测是否超时
|
|
737
|
+
if (_this3.current && !_this3.isTimedOut && _this3._checkInactive()) {
|
|
738
|
+
console.log('[Tracker SDK] User inactive for too long, triggering auto leave');
|
|
739
|
+
_this3.leave(true, true); // 强制上报离开事件,标记为超时
|
|
740
|
+
|
|
741
|
+
// 重置 Session,认为会话结束
|
|
742
|
+
_this3.sessionId = resetSession();
|
|
743
|
+
console.log('[Tracker SDK] Session reset due to inactivity, new sessionId:', _this3.sessionId);
|
|
744
|
+
}
|
|
745
|
+
}, checkInterval);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/**
|
|
749
|
+
* 停止超时检测
|
|
750
|
+
*/
|
|
751
|
+
}, {
|
|
752
|
+
key: "_stopTimeoutCheck",
|
|
753
|
+
value: function _stopTimeoutCheck() {
|
|
754
|
+
if (this.timeoutTimer) {
|
|
755
|
+
clearInterval(this.timeoutTimer);
|
|
756
|
+
this.timeoutTimer = null;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
|
|
615
760
|
/***********生命周期**********/
|
|
616
761
|
}, {
|
|
617
762
|
key: "_bindLifecycle",
|
|
618
763
|
value: function _bindLifecycle() {
|
|
619
|
-
var
|
|
764
|
+
var _this4 = this;
|
|
765
|
+
// 用于保存页面隐藏前的模块信息
|
|
766
|
+
var hiddenModule = null;
|
|
767
|
+
|
|
620
768
|
// 页面卸载或隐藏时,上报当前模块的停留时间并强制刷新队列
|
|
621
769
|
var flushData = function flushData() {
|
|
622
|
-
|
|
623
|
-
|
|
770
|
+
console.log('[Tracker SDK] Lifecycle: Page hidden/unloading, flushing data...');
|
|
771
|
+
console.log('[Tracker SDK] Current module:', _this4.current);
|
|
772
|
+
console.log('[Tracker SDK] Queue length:', _this4.queue.list.length);
|
|
773
|
+
|
|
774
|
+
// 停止超时检测
|
|
775
|
+
_this4._stopTimeoutCheck();
|
|
776
|
+
|
|
777
|
+
// 保存当前模块信息,以便页面恢复时重新 enter
|
|
778
|
+
if (_this4.current) {
|
|
779
|
+
hiddenModule = _objectSpread2({}, _this4.current);
|
|
780
|
+
console.log('[Tracker SDK] Saved module info for restoration:', hiddenModule.module);
|
|
781
|
+
}
|
|
782
|
+
_this4.leave(true);
|
|
783
|
+
_this4.queue.flush(true); // 传入 true,标记为页面关闭场景
|
|
784
|
+
|
|
785
|
+
console.log('[Tracker SDK] Data flushed successfully');
|
|
624
786
|
};
|
|
625
787
|
window.addEventListener('beforeunload', flushData);
|
|
626
788
|
document.addEventListener('visibilitychange', function () {
|
|
789
|
+
console.log('[Tracker SDK] Visibility changed:', document.visibilityState);
|
|
627
790
|
if (document.visibilityState === 'hidden') {
|
|
791
|
+
console.log('[Tracker SDK] Page is now hidden, triggering flush...');
|
|
628
792
|
flushData();
|
|
793
|
+
} else if (document.visibilityState === 'visible') {
|
|
794
|
+
console.log('[Tracker SDK] Page is now visible');
|
|
795
|
+
|
|
796
|
+
// 重新启动超时检测
|
|
797
|
+
_this4._startTimeoutCheck();
|
|
798
|
+
|
|
799
|
+
// 如果之前有保存的模块信息,重新 enter
|
|
800
|
+
if (hiddenModule) {
|
|
801
|
+
console.log('[Tracker SDK] Restoring module:', hiddenModule.module);
|
|
802
|
+
|
|
803
|
+
// 清除 enterTime,使用当前时间作为新的进入时间
|
|
804
|
+
var _hiddenModule = hiddenModule;
|
|
805
|
+
_hiddenModule.enterTime;
|
|
806
|
+
var moduleInfo = _objectWithoutProperties(_hiddenModule, _excluded2);
|
|
807
|
+
_this4.enter(moduleInfo);
|
|
808
|
+
|
|
809
|
+
// 重置 hiddenModule
|
|
810
|
+
hiddenModule = null;
|
|
811
|
+
console.log('[Tracker SDK] Module restored successfully');
|
|
812
|
+
} else {
|
|
813
|
+
console.log('[Tracker SDK] No module to restore');
|
|
814
|
+
}
|
|
629
815
|
}
|
|
630
816
|
});
|
|
631
817
|
}
|
package/dist/index.esm.js
CHANGED
|
@@ -154,8 +154,10 @@ var Queue = /*#__PURE__*/function () {
|
|
|
154
154
|
var _this2 = this;
|
|
155
155
|
var isFlush = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
156
156
|
if (!this.list.length) {
|
|
157
|
+
console.log('[Tracker SDK] Queue flush called but queue is empty');
|
|
157
158
|
return;
|
|
158
159
|
}
|
|
160
|
+
console.log('[Tracker SDK] Flushing queue, items:', this.list.length, 'isFlush:', isFlush);
|
|
159
161
|
|
|
160
162
|
// 复制一份数据列表,防止在发送过程中 list 被修改
|
|
161
163
|
var dataToSend = _toConsumableArray(this.list);
|
|
@@ -163,9 +165,11 @@ var Queue = /*#__PURE__*/function () {
|
|
|
163
165
|
|
|
164
166
|
try {
|
|
165
167
|
// 逐条发送数据,而不是批量发送
|
|
166
|
-
dataToSend.forEach(function (item) {
|
|
168
|
+
dataToSend.forEach(function (item, index) {
|
|
169
|
+
console.log("[Tracker SDK] Sending event ".concat(index + 1, "/").concat(dataToSend.length, ":"), item.event);
|
|
167
170
|
_this2.flushFn(item, isFlush);
|
|
168
171
|
});
|
|
172
|
+
console.log('[Tracker SDK] All events sent successfully');
|
|
169
173
|
} catch (error) {
|
|
170
174
|
console.error("[Tracker SDK] Failed to flush data:", error);
|
|
171
175
|
} finally {
|
|
@@ -194,7 +198,17 @@ var Queue = /*#__PURE__*/function () {
|
|
|
194
198
|
function send(url, data) {
|
|
195
199
|
var token = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
196
200
|
var useBeacon = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
197
|
-
if (!url)
|
|
201
|
+
if (!url) {
|
|
202
|
+
console.warn('[Tracker SDK] Send called without URL');
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
console.log('[Tracker SDK] Sending data:', {
|
|
206
|
+
url: url,
|
|
207
|
+
event: data.event,
|
|
208
|
+
module: data.module,
|
|
209
|
+
useBeacon: useBeacon,
|
|
210
|
+
hasToken: !!token
|
|
211
|
+
});
|
|
198
212
|
var body = JSON.stringify(data);
|
|
199
213
|
|
|
200
214
|
// 构造请求头
|
|
@@ -207,6 +221,7 @@ function send(url, data) {
|
|
|
207
221
|
|
|
208
222
|
// 1. 如果强制使用 Beacon 或 fetch 不可用,直接使用 sendBeacon
|
|
209
223
|
if (useBeacon && typeof navigator !== 'undefined' && navigator.sendBeacon) {
|
|
224
|
+
console.log('[Tracker SDK] Using sendBeacon for transport');
|
|
210
225
|
var beaconUrl = url;
|
|
211
226
|
if (token) {
|
|
212
227
|
var separator = url.includes('?') ? '&' : '?';
|
|
@@ -215,7 +230,9 @@ function send(url, data) {
|
|
|
215
230
|
var blob = new Blob([body], {
|
|
216
231
|
type: 'application/json'
|
|
217
232
|
});
|
|
218
|
-
|
|
233
|
+
var result = navigator.sendBeacon(beaconUrl, blob);
|
|
234
|
+
console.log('[Tracker SDK] sendBeacon result:', result);
|
|
235
|
+
return result;
|
|
219
236
|
}
|
|
220
237
|
|
|
221
238
|
// 2. 正常场景优先使用 fetch + keepalive
|
|
@@ -345,6 +362,17 @@ function refreshSession() {
|
|
|
345
362
|
storage.set(SESSION_TIME_KEY, now());
|
|
346
363
|
}
|
|
347
364
|
|
|
365
|
+
/**
|
|
366
|
+
* 手动重置 session(比如用户登录切换)。
|
|
367
|
+
* @returns {string} 新的 session_id
|
|
368
|
+
*/
|
|
369
|
+
function resetSession() {
|
|
370
|
+
var newSid = uuid();
|
|
371
|
+
storage.set(SESSION_KEY, newSid);
|
|
372
|
+
storage.set(SESSION_TIME_KEY, now());
|
|
373
|
+
return newSid;
|
|
374
|
+
}
|
|
375
|
+
|
|
348
376
|
/**
|
|
349
377
|
* 节流函数:在指定的时间间隔内,无论触发多少次,函数只执行一次。
|
|
350
378
|
* 包含最后一次触发的延迟执行(trailing edge)。
|
|
@@ -380,7 +408,8 @@ function throttle(fn, delay) {
|
|
|
380
408
|
};
|
|
381
409
|
}
|
|
382
410
|
|
|
383
|
-
var _excluded = ["system", "module", "sub_module"]
|
|
411
|
+
var _excluded = ["system", "module", "sub_module"],
|
|
412
|
+
_excluded2 = ["enterTime"];
|
|
384
413
|
|
|
385
414
|
/**
|
|
386
415
|
* Tracker 核心类
|
|
@@ -392,6 +421,7 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
392
421
|
* @param {number} [options.delay=1000] - 批量上报延迟
|
|
393
422
|
* @param {number} [options.maxBatchSize=10] - 批量上报最大条数
|
|
394
423
|
* @param {string} [options.token] - 数据上报接口的 Token
|
|
424
|
+
* @param {number} [options.sessionTimeout=1800000] - Session 超时时间(毫秒),默认30分钟
|
|
395
425
|
*/
|
|
396
426
|
function Tracker() {
|
|
397
427
|
var _this = this;
|
|
@@ -402,7 +432,8 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
402
432
|
}
|
|
403
433
|
this.options = _objectSpread2({
|
|
404
434
|
delay: 1000,
|
|
405
|
-
maxBatchSize: 10
|
|
435
|
+
maxBatchSize: 10,
|
|
436
|
+
sessionTimeout: 30 * 60 * 1000
|
|
406
437
|
}, options);
|
|
407
438
|
this.deviceId = uuid();
|
|
408
439
|
this.sessionId = getSessionId();
|
|
@@ -410,6 +441,12 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
410
441
|
this.globalExt = {};
|
|
411
442
|
this.current = null;
|
|
412
443
|
this.lastActiveTime = now();
|
|
444
|
+
|
|
445
|
+
// 超时检测定时器
|
|
446
|
+
this.timeoutTimer = null;
|
|
447
|
+
|
|
448
|
+
// 标记是否因超时而离开(用于重新激活时判断)
|
|
449
|
+
this.isTimedOut = false;
|
|
413
450
|
this.queue = new Queue(function (data, isFlush) {
|
|
414
451
|
if (_this.options.endPoint) {
|
|
415
452
|
// 如果是 flush 触发的(通常是页面关闭),强制使用 sendBeacon
|
|
@@ -424,6 +461,9 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
424
461
|
// 自动绑定活跃检测和生命周期监听
|
|
425
462
|
this._bindActivity();
|
|
426
463
|
this._bindLifecycle();
|
|
464
|
+
|
|
465
|
+
// 启动超时检测
|
|
466
|
+
this._startTimeoutCheck();
|
|
427
467
|
}
|
|
428
468
|
|
|
429
469
|
/***********插件**********/
|
|
@@ -485,6 +525,9 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
485
525
|
enterTime: now()
|
|
486
526
|
});
|
|
487
527
|
|
|
528
|
+
// 清除超时标记
|
|
529
|
+
this.isTimedOut = false;
|
|
530
|
+
|
|
488
531
|
// 构建 context 对象
|
|
489
532
|
var context = {
|
|
490
533
|
device_id: this.deviceId,
|
|
@@ -512,16 +555,26 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
512
555
|
/**
|
|
513
556
|
* 记录离开模块
|
|
514
557
|
* @param {boolean} force - 是否强制使用当前时间作为结束时间(用于页面卸载等场景)
|
|
558
|
+
* @param {boolean} isTimeout - 是否因为超时而离开(内部使用)
|
|
515
559
|
*/
|
|
516
560
|
}, {
|
|
517
561
|
key: "leave",
|
|
518
562
|
value: function leave() {
|
|
519
563
|
var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
564
|
+
var isTimeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
520
565
|
if (!this.current) {
|
|
566
|
+
console.log('[Tracker SDK] Leave called but no current module');
|
|
521
567
|
return;
|
|
522
568
|
}
|
|
523
569
|
var endTime = force ? now() : this.lastActiveTime;
|
|
524
570
|
var duration = Math.max(0, endTime - this.current.enterTime);
|
|
571
|
+
console.log('[Tracker SDK] Leaving module:', {
|
|
572
|
+
module: this.current.module,
|
|
573
|
+
system: this.current.system,
|
|
574
|
+
duration: duration,
|
|
575
|
+
force: force,
|
|
576
|
+
isTimeout: isTimeout
|
|
577
|
+
});
|
|
525
578
|
|
|
526
579
|
// 构建 context 对象
|
|
527
580
|
var context = {
|
|
@@ -542,8 +595,18 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
542
595
|
context: context,
|
|
543
596
|
ext: extData
|
|
544
597
|
};
|
|
598
|
+
console.log('[Tracker SDK] Pushing module_leave event to queue:', event);
|
|
545
599
|
this.queue.push(event);
|
|
546
|
-
|
|
600
|
+
|
|
601
|
+
// 如果是因为超时而离开,保留 current 信息以便重新激活时使用
|
|
602
|
+
if (isTimeout) {
|
|
603
|
+
this.isTimedOut = true;
|
|
604
|
+
console.log('[Tracker SDK] Module info preserved for re-activation');
|
|
605
|
+
} else {
|
|
606
|
+
// 正常离开,清除 current
|
|
607
|
+
this.current = null;
|
|
608
|
+
this.isTimedOut = false;
|
|
609
|
+
}
|
|
547
610
|
}
|
|
548
611
|
|
|
549
612
|
/**
|
|
@@ -591,8 +654,44 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
591
654
|
value: function _bindActivity() {
|
|
592
655
|
var _this2 = this;
|
|
593
656
|
var update = function update() {
|
|
657
|
+
var wasInactive = _this2._checkInactive();
|
|
594
658
|
_this2.lastActiveTime = now();
|
|
595
659
|
refreshSession(); // 用户活跃时刷新 Session 有效期
|
|
660
|
+
|
|
661
|
+
// 如果之前因为超时而离开了,现在重新激活,需要重新 enter
|
|
662
|
+
if (wasInactive && _this2.isTimedOut && _this2.current) {
|
|
663
|
+
console.log('[Tracker SDK] User became active again after timeout');
|
|
664
|
+
|
|
665
|
+
// 重置超时标记
|
|
666
|
+
_this2.isTimedOut = false;
|
|
667
|
+
|
|
668
|
+
// 重新记录进入时间
|
|
669
|
+
_this2.current.enterTime = now();
|
|
670
|
+
|
|
671
|
+
// 构建 context 对象
|
|
672
|
+
var context = {
|
|
673
|
+
device_id: _this2.deviceId,
|
|
674
|
+
session_id: _this2.sessionId,
|
|
675
|
+
user_id: _this2.userId
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
// 构建 ext 对象
|
|
679
|
+
var extData = _objectSpread2(_objectSpread2({}, _this2.globalExt), _this2.current.ext);
|
|
680
|
+
|
|
681
|
+
// 上报 module_enter 事件
|
|
682
|
+
var event = {
|
|
683
|
+
system: _this2.current.system || '',
|
|
684
|
+
module: _this2.current.module || '',
|
|
685
|
+
sub_module: _this2.current.sub_module || '',
|
|
686
|
+
duration: 0,
|
|
687
|
+
event: 'module_enter',
|
|
688
|
+
timestamp: formatTime(now()),
|
|
689
|
+
context: context,
|
|
690
|
+
ext: extData
|
|
691
|
+
};
|
|
692
|
+
_this2.queue.push(event);
|
|
693
|
+
console.log('[Tracker SDK] Re-entered module after inactivity:', _this2.current.module);
|
|
694
|
+
}
|
|
596
695
|
};
|
|
597
696
|
var events = ['click', 'keydown', 'touchstart'];
|
|
598
697
|
events.forEach(function (event) {
|
|
@@ -610,20 +709,107 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
610
709
|
});
|
|
611
710
|
}
|
|
612
711
|
|
|
712
|
+
/**
|
|
713
|
+
* 检查用户是否处于非活跃状态(超过超时时间)
|
|
714
|
+
* @returns {boolean} 是否是非活跃状态
|
|
715
|
+
*/
|
|
716
|
+
}, {
|
|
717
|
+
key: "_checkInactive",
|
|
718
|
+
value: function _checkInactive() {
|
|
719
|
+
var inactiveTime = now() - this.lastActiveTime;
|
|
720
|
+
return inactiveTime >= this.options.sessionTimeout;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/**
|
|
724
|
+
* 启动超时检测定时器
|
|
725
|
+
*/
|
|
726
|
+
}, {
|
|
727
|
+
key: "_startTimeoutCheck",
|
|
728
|
+
value: function _startTimeoutCheck() {
|
|
729
|
+
var _this3 = this;
|
|
730
|
+
// 动态计算检测间隔:超时时间的 1/6,但不超过 30 秒,不少于 5 秒
|
|
731
|
+
var checkInterval = Math.min(Math.max(this.options.sessionTimeout / 6, 5 * 1000), 30 * 1000);
|
|
732
|
+
console.log('[Tracker SDK] Timeout check interval:', (checkInterval / 1000).toFixed(1), 's');
|
|
733
|
+
this.timeoutTimer = setInterval(function () {
|
|
734
|
+
// 只要有 current 且未处于超时状态,就检测是否超时
|
|
735
|
+
if (_this3.current && !_this3.isTimedOut && _this3._checkInactive()) {
|
|
736
|
+
console.log('[Tracker SDK] User inactive for too long, triggering auto leave');
|
|
737
|
+
_this3.leave(true, true); // 强制上报离开事件,标记为超时
|
|
738
|
+
|
|
739
|
+
// 重置 Session,认为会话结束
|
|
740
|
+
_this3.sessionId = resetSession();
|
|
741
|
+
console.log('[Tracker SDK] Session reset due to inactivity, new sessionId:', _this3.sessionId);
|
|
742
|
+
}
|
|
743
|
+
}, checkInterval);
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* 停止超时检测
|
|
748
|
+
*/
|
|
749
|
+
}, {
|
|
750
|
+
key: "_stopTimeoutCheck",
|
|
751
|
+
value: function _stopTimeoutCheck() {
|
|
752
|
+
if (this.timeoutTimer) {
|
|
753
|
+
clearInterval(this.timeoutTimer);
|
|
754
|
+
this.timeoutTimer = null;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
|
|
613
758
|
/***********生命周期**********/
|
|
614
759
|
}, {
|
|
615
760
|
key: "_bindLifecycle",
|
|
616
761
|
value: function _bindLifecycle() {
|
|
617
|
-
var
|
|
762
|
+
var _this4 = this;
|
|
763
|
+
// 用于保存页面隐藏前的模块信息
|
|
764
|
+
var hiddenModule = null;
|
|
765
|
+
|
|
618
766
|
// 页面卸载或隐藏时,上报当前模块的停留时间并强制刷新队列
|
|
619
767
|
var flushData = function flushData() {
|
|
620
|
-
|
|
621
|
-
|
|
768
|
+
console.log('[Tracker SDK] Lifecycle: Page hidden/unloading, flushing data...');
|
|
769
|
+
console.log('[Tracker SDK] Current module:', _this4.current);
|
|
770
|
+
console.log('[Tracker SDK] Queue length:', _this4.queue.list.length);
|
|
771
|
+
|
|
772
|
+
// 停止超时检测
|
|
773
|
+
_this4._stopTimeoutCheck();
|
|
774
|
+
|
|
775
|
+
// 保存当前模块信息,以便页面恢复时重新 enter
|
|
776
|
+
if (_this4.current) {
|
|
777
|
+
hiddenModule = _objectSpread2({}, _this4.current);
|
|
778
|
+
console.log('[Tracker SDK] Saved module info for restoration:', hiddenModule.module);
|
|
779
|
+
}
|
|
780
|
+
_this4.leave(true);
|
|
781
|
+
_this4.queue.flush(true); // 传入 true,标记为页面关闭场景
|
|
782
|
+
|
|
783
|
+
console.log('[Tracker SDK] Data flushed successfully');
|
|
622
784
|
};
|
|
623
785
|
window.addEventListener('beforeunload', flushData);
|
|
624
786
|
document.addEventListener('visibilitychange', function () {
|
|
787
|
+
console.log('[Tracker SDK] Visibility changed:', document.visibilityState);
|
|
625
788
|
if (document.visibilityState === 'hidden') {
|
|
789
|
+
console.log('[Tracker SDK] Page is now hidden, triggering flush...');
|
|
626
790
|
flushData();
|
|
791
|
+
} else if (document.visibilityState === 'visible') {
|
|
792
|
+
console.log('[Tracker SDK] Page is now visible');
|
|
793
|
+
|
|
794
|
+
// 重新启动超时检测
|
|
795
|
+
_this4._startTimeoutCheck();
|
|
796
|
+
|
|
797
|
+
// 如果之前有保存的模块信息,重新 enter
|
|
798
|
+
if (hiddenModule) {
|
|
799
|
+
console.log('[Tracker SDK] Restoring module:', hiddenModule.module);
|
|
800
|
+
|
|
801
|
+
// 清除 enterTime,使用当前时间作为新的进入时间
|
|
802
|
+
var _hiddenModule = hiddenModule;
|
|
803
|
+
_hiddenModule.enterTime;
|
|
804
|
+
var moduleInfo = _objectWithoutProperties(_hiddenModule, _excluded2);
|
|
805
|
+
_this4.enter(moduleInfo);
|
|
806
|
+
|
|
807
|
+
// 重置 hiddenModule
|
|
808
|
+
hiddenModule = null;
|
|
809
|
+
console.log('[Tracker SDK] Module restored successfully');
|
|
810
|
+
} else {
|
|
811
|
+
console.log('[Tracker SDK] No module to restore');
|
|
812
|
+
}
|
|
627
813
|
}
|
|
628
814
|
});
|
|
629
815
|
}
|