@jasolar/tracker-sdk 1.0.8 → 1.0.10
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 +163 -26
- package/dist/index.esm.js +163 -26
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -364,6 +364,17 @@ function refreshSession() {
|
|
|
364
364
|
storage.set(SESSION_TIME_KEY, now());
|
|
365
365
|
}
|
|
366
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
|
+
|
|
367
378
|
/**
|
|
368
379
|
* 节流函数:在指定的时间间隔内,无论触发多少次,函数只执行一次。
|
|
369
380
|
* 包含最后一次触发的延迟执行(trailing edge)。
|
|
@@ -412,6 +423,7 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
412
423
|
* @param {number} [options.delay=1000] - 批量上报延迟
|
|
413
424
|
* @param {number} [options.maxBatchSize=10] - 批量上报最大条数
|
|
414
425
|
* @param {string} [options.token] - 数据上报接口的 Token
|
|
426
|
+
* @param {number} [options.sessionTimeout=1800000] - Session 超时时间(毫秒),默认30分钟
|
|
415
427
|
*/
|
|
416
428
|
function Tracker() {
|
|
417
429
|
var _this = this;
|
|
@@ -422,7 +434,8 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
422
434
|
}
|
|
423
435
|
this.options = _objectSpread2({
|
|
424
436
|
delay: 1000,
|
|
425
|
-
maxBatchSize: 10
|
|
437
|
+
maxBatchSize: 10,
|
|
438
|
+
sessionTimeout: 30 * 60 * 1000
|
|
426
439
|
}, options);
|
|
427
440
|
this.deviceId = uuid();
|
|
428
441
|
this.sessionId = getSessionId();
|
|
@@ -430,6 +443,12 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
430
443
|
this.globalExt = {};
|
|
431
444
|
this.current = null;
|
|
432
445
|
this.lastActiveTime = now();
|
|
446
|
+
|
|
447
|
+
// 超时检测定时器
|
|
448
|
+
this.timeoutTimer = null;
|
|
449
|
+
|
|
450
|
+
// 标记是否因超时而离开(用于重新激活时判断)
|
|
451
|
+
this.isTimedOut = false;
|
|
433
452
|
this.queue = new Queue(function (data, isFlush) {
|
|
434
453
|
if (_this.options.endPoint) {
|
|
435
454
|
// 如果是 flush 触发的(通常是页面关闭),强制使用 sendBeacon
|
|
@@ -444,6 +463,9 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
444
463
|
// 自动绑定活跃检测和生命周期监听
|
|
445
464
|
this._bindActivity();
|
|
446
465
|
this._bindLifecycle();
|
|
466
|
+
|
|
467
|
+
// 启动超时检测
|
|
468
|
+
this._startTimeoutCheck();
|
|
447
469
|
}
|
|
448
470
|
|
|
449
471
|
/***********插件**********/
|
|
@@ -505,6 +527,9 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
505
527
|
enterTime: now()
|
|
506
528
|
});
|
|
507
529
|
|
|
530
|
+
// 清除超时标记
|
|
531
|
+
this.isTimedOut = false;
|
|
532
|
+
|
|
508
533
|
// 构建 context 对象
|
|
509
534
|
var context = {
|
|
510
535
|
device_id: this.deviceId,
|
|
@@ -532,11 +557,15 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
532
557
|
/**
|
|
533
558
|
* 记录离开模块
|
|
534
559
|
* @param {boolean} force - 是否强制使用当前时间作为结束时间(用于页面卸载等场景)
|
|
560
|
+
* @param {boolean} isTimeout - 是否因为超时而离开(内部使用)
|
|
561
|
+
* @param {string} reason - 离开原因(可选):'timeout' | 'visibility' | 'normal'
|
|
535
562
|
*/
|
|
536
563
|
}, {
|
|
537
564
|
key: "leave",
|
|
538
565
|
value: function leave() {
|
|
539
566
|
var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
567
|
+
var isTimeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
568
|
+
var reason = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'normal';
|
|
540
569
|
if (!this.current) {
|
|
541
570
|
console.log('[Tracker SDK] Leave called but no current module');
|
|
542
571
|
return;
|
|
@@ -547,7 +576,9 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
547
576
|
module: this.current.module,
|
|
548
577
|
system: this.current.system,
|
|
549
578
|
duration: duration,
|
|
550
|
-
force: force
|
|
579
|
+
force: force,
|
|
580
|
+
isTimeout: isTimeout,
|
|
581
|
+
reason: reason
|
|
551
582
|
});
|
|
552
583
|
|
|
553
584
|
// 构建 context 对象
|
|
@@ -567,11 +598,22 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
567
598
|
event: 'module_leave',
|
|
568
599
|
timestamp: formatTime(now()),
|
|
569
600
|
context: context,
|
|
570
|
-
ext: extData
|
|
601
|
+
ext: _objectSpread2(_objectSpread2({}, extData), {}, {
|
|
602
|
+
leave_reason: reason // 添加离开原因
|
|
603
|
+
})
|
|
571
604
|
};
|
|
572
605
|
console.log('[Tracker SDK] Pushing module_leave event to queue:', event);
|
|
573
606
|
this.queue.push(event);
|
|
574
|
-
|
|
607
|
+
|
|
608
|
+
// 如果是因为超时而离开,保留 current 信息以便重新激活时使用
|
|
609
|
+
if (isTimeout) {
|
|
610
|
+
this.isTimedOut = true;
|
|
611
|
+
console.log('[Tracker SDK] Module info preserved for re-activation');
|
|
612
|
+
} else {
|
|
613
|
+
// 正常离开,清除 current
|
|
614
|
+
this.current = null;
|
|
615
|
+
this.isTimedOut = false;
|
|
616
|
+
}
|
|
575
617
|
}
|
|
576
618
|
|
|
577
619
|
/**
|
|
@@ -619,8 +661,44 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
619
661
|
value: function _bindActivity() {
|
|
620
662
|
var _this2 = this;
|
|
621
663
|
var update = function update() {
|
|
664
|
+
var wasInactive = _this2._checkInactive();
|
|
622
665
|
_this2.lastActiveTime = now();
|
|
623
666
|
refreshSession(); // 用户活跃时刷新 Session 有效期
|
|
667
|
+
|
|
668
|
+
// 如果之前因为超时而离开了,现在重新激活,需要重新 enter
|
|
669
|
+
if (wasInactive && _this2.isTimedOut && _this2.current) {
|
|
670
|
+
console.log('[Tracker SDK] User became active again after timeout');
|
|
671
|
+
|
|
672
|
+
// 重置超时标记
|
|
673
|
+
_this2.isTimedOut = false;
|
|
674
|
+
|
|
675
|
+
// 重新记录进入时间
|
|
676
|
+
_this2.current.enterTime = now();
|
|
677
|
+
|
|
678
|
+
// 构建 context 对象
|
|
679
|
+
var context = {
|
|
680
|
+
device_id: _this2.deviceId,
|
|
681
|
+
session_id: _this2.sessionId,
|
|
682
|
+
user_id: _this2.userId
|
|
683
|
+
};
|
|
684
|
+
|
|
685
|
+
// 构建 ext 对象
|
|
686
|
+
var extData = _objectSpread2(_objectSpread2({}, _this2.globalExt), _this2.current.ext);
|
|
687
|
+
|
|
688
|
+
// 上报 module_enter 事件
|
|
689
|
+
var event = {
|
|
690
|
+
system: _this2.current.system || '',
|
|
691
|
+
module: _this2.current.module || '',
|
|
692
|
+
sub_module: _this2.current.sub_module || '',
|
|
693
|
+
duration: 0,
|
|
694
|
+
event: 'module_enter',
|
|
695
|
+
timestamp: formatTime(now()),
|
|
696
|
+
context: context,
|
|
697
|
+
ext: extData
|
|
698
|
+
};
|
|
699
|
+
_this2.queue.push(event);
|
|
700
|
+
console.log('[Tracker SDK] Re-entered module after inactivity:', _this2.current.module);
|
|
701
|
+
}
|
|
624
702
|
};
|
|
625
703
|
var events = ['click', 'keydown', 'touchstart'];
|
|
626
704
|
events.forEach(function (event) {
|
|
@@ -638,27 +716,76 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
638
716
|
});
|
|
639
717
|
}
|
|
640
718
|
|
|
719
|
+
/**
|
|
720
|
+
* 检查用户是否处于非活跃状态(超过超时时间)
|
|
721
|
+
* @returns {boolean} 是否是非活跃状态
|
|
722
|
+
*/
|
|
723
|
+
}, {
|
|
724
|
+
key: "_checkInactive",
|
|
725
|
+
value: function _checkInactive() {
|
|
726
|
+
var inactiveTime = now() - this.lastActiveTime;
|
|
727
|
+
return inactiveTime >= this.options.sessionTimeout;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/**
|
|
731
|
+
* 启动超时检测定时器
|
|
732
|
+
*/
|
|
733
|
+
}, {
|
|
734
|
+
key: "_startTimeoutCheck",
|
|
735
|
+
value: function _startTimeoutCheck() {
|
|
736
|
+
var _this3 = this;
|
|
737
|
+
// 动态计算检测间隔:超时时间的 1/6,但不超过 30 秒,不少于 5 秒
|
|
738
|
+
var checkInterval = Math.min(Math.max(this.options.sessionTimeout / 6, 5 * 1000), 30 * 1000);
|
|
739
|
+
console.log('[Tracker SDK] Timeout check interval:', (checkInterval / 1000).toFixed(1), 's');
|
|
740
|
+
this.timeoutTimer = setInterval(function () {
|
|
741
|
+
// 只要有 current 且未处于超时状态,就检测是否超时
|
|
742
|
+
if (_this3.current && !_this3.isTimedOut && _this3._checkInactive()) {
|
|
743
|
+
console.log('[Tracker SDK] User inactive for too long, triggering auto leave');
|
|
744
|
+
_this3.leave(true, true); // 强制上报离开事件,标记为超时
|
|
745
|
+
|
|
746
|
+
// 重置 Session,认为会话结束
|
|
747
|
+
_this3.sessionId = resetSession();
|
|
748
|
+
console.log('[Tracker SDK] Session reset due to inactivity, new sessionId:', _this3.sessionId);
|
|
749
|
+
}
|
|
750
|
+
}, checkInterval);
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* 停止超时检测
|
|
755
|
+
*/
|
|
756
|
+
}, {
|
|
757
|
+
key: "_stopTimeoutCheck",
|
|
758
|
+
value: function _stopTimeoutCheck() {
|
|
759
|
+
if (this.timeoutTimer) {
|
|
760
|
+
clearInterval(this.timeoutTimer);
|
|
761
|
+
this.timeoutTimer = null;
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
|
|
641
765
|
/***********生命周期**********/
|
|
642
766
|
}, {
|
|
643
767
|
key: "_bindLifecycle",
|
|
644
768
|
value: function _bindLifecycle() {
|
|
645
|
-
var
|
|
769
|
+
var _this4 = this;
|
|
646
770
|
// 用于保存页面隐藏前的模块信息
|
|
647
771
|
var hiddenModule = null;
|
|
648
772
|
|
|
649
773
|
// 页面卸载或隐藏时,上报当前模块的停留时间并强制刷新队列
|
|
650
774
|
var flushData = function flushData() {
|
|
651
775
|
console.log('[Tracker SDK] Lifecycle: Page hidden/unloading, flushing data...');
|
|
652
|
-
console.log('[Tracker SDK] Current module:',
|
|
653
|
-
console.log('[Tracker SDK] Queue length:',
|
|
776
|
+
console.log('[Tracker SDK] Current module:', _this4.current);
|
|
777
|
+
console.log('[Tracker SDK] Queue length:', _this4.queue.list.length);
|
|
778
|
+
|
|
779
|
+
// 停止超时检测
|
|
780
|
+
_this4._stopTimeoutCheck();
|
|
654
781
|
|
|
655
782
|
// 保存当前模块信息,以便页面恢复时重新 enter
|
|
656
|
-
if (
|
|
657
|
-
hiddenModule = _objectSpread2({},
|
|
783
|
+
if (_this4.current) {
|
|
784
|
+
hiddenModule = _objectSpread2({}, _this4.current);
|
|
658
785
|
console.log('[Tracker SDK] Saved module info for restoration:', hiddenModule.module);
|
|
659
786
|
}
|
|
660
|
-
|
|
661
|
-
|
|
787
|
+
_this4.leave(true, false, 'visibility'); // 标记为可见性变化导致的离开
|
|
788
|
+
_this4.queue.flush(true); // 传入 true,标记为页面关闭场景
|
|
662
789
|
|
|
663
790
|
console.log('[Tracker SDK] Data flushed successfully');
|
|
664
791
|
};
|
|
@@ -671,6 +798,9 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
671
798
|
} else if (document.visibilityState === 'visible') {
|
|
672
799
|
console.log('[Tracker SDK] Page is now visible');
|
|
673
800
|
|
|
801
|
+
// 重新启动超时检测
|
|
802
|
+
_this4._startTimeoutCheck();
|
|
803
|
+
|
|
674
804
|
// 如果之前有保存的模块信息,重新 enter
|
|
675
805
|
if (hiddenModule) {
|
|
676
806
|
console.log('[Tracker SDK] Restoring module:', hiddenModule.module);
|
|
@@ -679,7 +809,7 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
679
809
|
var _hiddenModule = hiddenModule;
|
|
680
810
|
_hiddenModule.enterTime;
|
|
681
811
|
var moduleInfo = _objectWithoutProperties(_hiddenModule, _excluded2);
|
|
682
|
-
|
|
812
|
+
_this4.enter(moduleInfo);
|
|
683
813
|
|
|
684
814
|
// 重置 hiddenModule
|
|
685
815
|
hiddenModule = null;
|
|
@@ -794,15 +924,25 @@ function routerPlugin(tracker) {
|
|
|
794
924
|
var lastPath = window.location.pathname + window.location.search + window.location.hash;
|
|
795
925
|
|
|
796
926
|
/**
|
|
797
|
-
*
|
|
927
|
+
* 根据路径获取模块信息
|
|
798
928
|
*/
|
|
799
|
-
var
|
|
929
|
+
var getModuleInfo = function getModuleInfo(path) {
|
|
800
930
|
// 如果提供了自定义映射函数,使用它
|
|
801
931
|
if (typeof moduleMapper === 'function') {
|
|
802
|
-
|
|
932
|
+
var result = moduleMapper(path);
|
|
933
|
+
if (typeof result === 'string') {
|
|
934
|
+
return {
|
|
935
|
+
module: result
|
|
936
|
+
};
|
|
937
|
+
}
|
|
938
|
+
return result || {
|
|
939
|
+
module: path
|
|
940
|
+
};
|
|
803
941
|
}
|
|
804
942
|
// 否则使用完整路径
|
|
805
|
-
return
|
|
943
|
+
return {
|
|
944
|
+
module: path
|
|
945
|
+
};
|
|
806
946
|
};
|
|
807
947
|
|
|
808
948
|
/**
|
|
@@ -891,10 +1031,9 @@ function routerPlugin(tracker) {
|
|
|
891
1031
|
// 如果路径没变,但 tracker.current 为空(可能被生命周期清空了),需要重新 enter
|
|
892
1032
|
if (!tracker.current) {
|
|
893
1033
|
console.log('[Tracker SDK] Tracker current is null, re-entering current route');
|
|
894
|
-
tracker.enter({
|
|
895
|
-
module: getModuleName(currentPath),
|
|
1034
|
+
tracker.enter(_objectSpread2(_objectSpread2({}, getModuleInfo(currentPath)), {}, {
|
|
896
1035
|
system: system
|
|
897
|
-
});
|
|
1036
|
+
}));
|
|
898
1037
|
}
|
|
899
1038
|
return;
|
|
900
1039
|
}
|
|
@@ -904,11 +1043,10 @@ function routerPlugin(tracker) {
|
|
|
904
1043
|
if (tracker.current) {
|
|
905
1044
|
tracker.leave();
|
|
906
1045
|
}
|
|
907
|
-
tracker.enter({
|
|
908
|
-
|
|
909
|
-
// 使用映射后的模块名
|
|
1046
|
+
tracker.enter(_objectSpread2(_objectSpread2({}, getModuleInfo(currentPath)), {}, {
|
|
1047
|
+
// 使用映射后的模块信息
|
|
910
1048
|
system: system // 使用配置的 system
|
|
911
|
-
});
|
|
1049
|
+
}));
|
|
912
1050
|
lastPath = currentPath;
|
|
913
1051
|
}, 0);
|
|
914
1052
|
};
|
|
@@ -921,10 +1059,9 @@ function routerPlugin(tracker) {
|
|
|
921
1059
|
if (shouldTrack(initialPath)) {
|
|
922
1060
|
lastPath = initialPath; // 更新 lastPath 为实际初始路径
|
|
923
1061
|
|
|
924
|
-
tracker.enter({
|
|
925
|
-
module: getModuleName(initialPath),
|
|
1062
|
+
tracker.enter(_objectSpread2(_objectSpread2({}, getModuleInfo(initialPath)), {}, {
|
|
926
1063
|
system: system
|
|
927
|
-
});
|
|
1064
|
+
}));
|
|
928
1065
|
console.log('[Tracker SDK] Initial route tracked:', initialPath);
|
|
929
1066
|
} else {
|
|
930
1067
|
console.log('[Tracker SDK] Initial route filtered out:', initialPath);
|
package/dist/index.esm.js
CHANGED
|
@@ -362,6 +362,17 @@ function refreshSession() {
|
|
|
362
362
|
storage.set(SESSION_TIME_KEY, now());
|
|
363
363
|
}
|
|
364
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
|
+
|
|
365
376
|
/**
|
|
366
377
|
* 节流函数:在指定的时间间隔内,无论触发多少次,函数只执行一次。
|
|
367
378
|
* 包含最后一次触发的延迟执行(trailing edge)。
|
|
@@ -410,6 +421,7 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
410
421
|
* @param {number} [options.delay=1000] - 批量上报延迟
|
|
411
422
|
* @param {number} [options.maxBatchSize=10] - 批量上报最大条数
|
|
412
423
|
* @param {string} [options.token] - 数据上报接口的 Token
|
|
424
|
+
* @param {number} [options.sessionTimeout=1800000] - Session 超时时间(毫秒),默认30分钟
|
|
413
425
|
*/
|
|
414
426
|
function Tracker() {
|
|
415
427
|
var _this = this;
|
|
@@ -420,7 +432,8 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
420
432
|
}
|
|
421
433
|
this.options = _objectSpread2({
|
|
422
434
|
delay: 1000,
|
|
423
|
-
maxBatchSize: 10
|
|
435
|
+
maxBatchSize: 10,
|
|
436
|
+
sessionTimeout: 30 * 60 * 1000
|
|
424
437
|
}, options);
|
|
425
438
|
this.deviceId = uuid();
|
|
426
439
|
this.sessionId = getSessionId();
|
|
@@ -428,6 +441,12 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
428
441
|
this.globalExt = {};
|
|
429
442
|
this.current = null;
|
|
430
443
|
this.lastActiveTime = now();
|
|
444
|
+
|
|
445
|
+
// 超时检测定时器
|
|
446
|
+
this.timeoutTimer = null;
|
|
447
|
+
|
|
448
|
+
// 标记是否因超时而离开(用于重新激活时判断)
|
|
449
|
+
this.isTimedOut = false;
|
|
431
450
|
this.queue = new Queue(function (data, isFlush) {
|
|
432
451
|
if (_this.options.endPoint) {
|
|
433
452
|
// 如果是 flush 触发的(通常是页面关闭),强制使用 sendBeacon
|
|
@@ -442,6 +461,9 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
442
461
|
// 自动绑定活跃检测和生命周期监听
|
|
443
462
|
this._bindActivity();
|
|
444
463
|
this._bindLifecycle();
|
|
464
|
+
|
|
465
|
+
// 启动超时检测
|
|
466
|
+
this._startTimeoutCheck();
|
|
445
467
|
}
|
|
446
468
|
|
|
447
469
|
/***********插件**********/
|
|
@@ -503,6 +525,9 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
503
525
|
enterTime: now()
|
|
504
526
|
});
|
|
505
527
|
|
|
528
|
+
// 清除超时标记
|
|
529
|
+
this.isTimedOut = false;
|
|
530
|
+
|
|
506
531
|
// 构建 context 对象
|
|
507
532
|
var context = {
|
|
508
533
|
device_id: this.deviceId,
|
|
@@ -530,11 +555,15 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
530
555
|
/**
|
|
531
556
|
* 记录离开模块
|
|
532
557
|
* @param {boolean} force - 是否强制使用当前时间作为结束时间(用于页面卸载等场景)
|
|
558
|
+
* @param {boolean} isTimeout - 是否因为超时而离开(内部使用)
|
|
559
|
+
* @param {string} reason - 离开原因(可选):'timeout' | 'visibility' | 'normal'
|
|
533
560
|
*/
|
|
534
561
|
}, {
|
|
535
562
|
key: "leave",
|
|
536
563
|
value: function leave() {
|
|
537
564
|
var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
565
|
+
var isTimeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
566
|
+
var reason = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'normal';
|
|
538
567
|
if (!this.current) {
|
|
539
568
|
console.log('[Tracker SDK] Leave called but no current module');
|
|
540
569
|
return;
|
|
@@ -545,7 +574,9 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
545
574
|
module: this.current.module,
|
|
546
575
|
system: this.current.system,
|
|
547
576
|
duration: duration,
|
|
548
|
-
force: force
|
|
577
|
+
force: force,
|
|
578
|
+
isTimeout: isTimeout,
|
|
579
|
+
reason: reason
|
|
549
580
|
});
|
|
550
581
|
|
|
551
582
|
// 构建 context 对象
|
|
@@ -565,11 +596,22 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
565
596
|
event: 'module_leave',
|
|
566
597
|
timestamp: formatTime(now()),
|
|
567
598
|
context: context,
|
|
568
|
-
ext: extData
|
|
599
|
+
ext: _objectSpread2(_objectSpread2({}, extData), {}, {
|
|
600
|
+
leave_reason: reason // 添加离开原因
|
|
601
|
+
})
|
|
569
602
|
};
|
|
570
603
|
console.log('[Tracker SDK] Pushing module_leave event to queue:', event);
|
|
571
604
|
this.queue.push(event);
|
|
572
|
-
|
|
605
|
+
|
|
606
|
+
// 如果是因为超时而离开,保留 current 信息以便重新激活时使用
|
|
607
|
+
if (isTimeout) {
|
|
608
|
+
this.isTimedOut = true;
|
|
609
|
+
console.log('[Tracker SDK] Module info preserved for re-activation');
|
|
610
|
+
} else {
|
|
611
|
+
// 正常离开,清除 current
|
|
612
|
+
this.current = null;
|
|
613
|
+
this.isTimedOut = false;
|
|
614
|
+
}
|
|
573
615
|
}
|
|
574
616
|
|
|
575
617
|
/**
|
|
@@ -617,8 +659,44 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
617
659
|
value: function _bindActivity() {
|
|
618
660
|
var _this2 = this;
|
|
619
661
|
var update = function update() {
|
|
662
|
+
var wasInactive = _this2._checkInactive();
|
|
620
663
|
_this2.lastActiveTime = now();
|
|
621
664
|
refreshSession(); // 用户活跃时刷新 Session 有效期
|
|
665
|
+
|
|
666
|
+
// 如果之前因为超时而离开了,现在重新激活,需要重新 enter
|
|
667
|
+
if (wasInactive && _this2.isTimedOut && _this2.current) {
|
|
668
|
+
console.log('[Tracker SDK] User became active again after timeout');
|
|
669
|
+
|
|
670
|
+
// 重置超时标记
|
|
671
|
+
_this2.isTimedOut = false;
|
|
672
|
+
|
|
673
|
+
// 重新记录进入时间
|
|
674
|
+
_this2.current.enterTime = now();
|
|
675
|
+
|
|
676
|
+
// 构建 context 对象
|
|
677
|
+
var context = {
|
|
678
|
+
device_id: _this2.deviceId,
|
|
679
|
+
session_id: _this2.sessionId,
|
|
680
|
+
user_id: _this2.userId
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
// 构建 ext 对象
|
|
684
|
+
var extData = _objectSpread2(_objectSpread2({}, _this2.globalExt), _this2.current.ext);
|
|
685
|
+
|
|
686
|
+
// 上报 module_enter 事件
|
|
687
|
+
var event = {
|
|
688
|
+
system: _this2.current.system || '',
|
|
689
|
+
module: _this2.current.module || '',
|
|
690
|
+
sub_module: _this2.current.sub_module || '',
|
|
691
|
+
duration: 0,
|
|
692
|
+
event: 'module_enter',
|
|
693
|
+
timestamp: formatTime(now()),
|
|
694
|
+
context: context,
|
|
695
|
+
ext: extData
|
|
696
|
+
};
|
|
697
|
+
_this2.queue.push(event);
|
|
698
|
+
console.log('[Tracker SDK] Re-entered module after inactivity:', _this2.current.module);
|
|
699
|
+
}
|
|
622
700
|
};
|
|
623
701
|
var events = ['click', 'keydown', 'touchstart'];
|
|
624
702
|
events.forEach(function (event) {
|
|
@@ -636,27 +714,76 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
636
714
|
});
|
|
637
715
|
}
|
|
638
716
|
|
|
717
|
+
/**
|
|
718
|
+
* 检查用户是否处于非活跃状态(超过超时时间)
|
|
719
|
+
* @returns {boolean} 是否是非活跃状态
|
|
720
|
+
*/
|
|
721
|
+
}, {
|
|
722
|
+
key: "_checkInactive",
|
|
723
|
+
value: function _checkInactive() {
|
|
724
|
+
var inactiveTime = now() - this.lastActiveTime;
|
|
725
|
+
return inactiveTime >= this.options.sessionTimeout;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* 启动超时检测定时器
|
|
730
|
+
*/
|
|
731
|
+
}, {
|
|
732
|
+
key: "_startTimeoutCheck",
|
|
733
|
+
value: function _startTimeoutCheck() {
|
|
734
|
+
var _this3 = this;
|
|
735
|
+
// 动态计算检测间隔:超时时间的 1/6,但不超过 30 秒,不少于 5 秒
|
|
736
|
+
var checkInterval = Math.min(Math.max(this.options.sessionTimeout / 6, 5 * 1000), 30 * 1000);
|
|
737
|
+
console.log('[Tracker SDK] Timeout check interval:', (checkInterval / 1000).toFixed(1), 's');
|
|
738
|
+
this.timeoutTimer = setInterval(function () {
|
|
739
|
+
// 只要有 current 且未处于超时状态,就检测是否超时
|
|
740
|
+
if (_this3.current && !_this3.isTimedOut && _this3._checkInactive()) {
|
|
741
|
+
console.log('[Tracker SDK] User inactive for too long, triggering auto leave');
|
|
742
|
+
_this3.leave(true, true); // 强制上报离开事件,标记为超时
|
|
743
|
+
|
|
744
|
+
// 重置 Session,认为会话结束
|
|
745
|
+
_this3.sessionId = resetSession();
|
|
746
|
+
console.log('[Tracker SDK] Session reset due to inactivity, new sessionId:', _this3.sessionId);
|
|
747
|
+
}
|
|
748
|
+
}, checkInterval);
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
/**
|
|
752
|
+
* 停止超时检测
|
|
753
|
+
*/
|
|
754
|
+
}, {
|
|
755
|
+
key: "_stopTimeoutCheck",
|
|
756
|
+
value: function _stopTimeoutCheck() {
|
|
757
|
+
if (this.timeoutTimer) {
|
|
758
|
+
clearInterval(this.timeoutTimer);
|
|
759
|
+
this.timeoutTimer = null;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
639
763
|
/***********生命周期**********/
|
|
640
764
|
}, {
|
|
641
765
|
key: "_bindLifecycle",
|
|
642
766
|
value: function _bindLifecycle() {
|
|
643
|
-
var
|
|
767
|
+
var _this4 = this;
|
|
644
768
|
// 用于保存页面隐藏前的模块信息
|
|
645
769
|
var hiddenModule = null;
|
|
646
770
|
|
|
647
771
|
// 页面卸载或隐藏时,上报当前模块的停留时间并强制刷新队列
|
|
648
772
|
var flushData = function flushData() {
|
|
649
773
|
console.log('[Tracker SDK] Lifecycle: Page hidden/unloading, flushing data...');
|
|
650
|
-
console.log('[Tracker SDK] Current module:',
|
|
651
|
-
console.log('[Tracker SDK] Queue length:',
|
|
774
|
+
console.log('[Tracker SDK] Current module:', _this4.current);
|
|
775
|
+
console.log('[Tracker SDK] Queue length:', _this4.queue.list.length);
|
|
776
|
+
|
|
777
|
+
// 停止超时检测
|
|
778
|
+
_this4._stopTimeoutCheck();
|
|
652
779
|
|
|
653
780
|
// 保存当前模块信息,以便页面恢复时重新 enter
|
|
654
|
-
if (
|
|
655
|
-
hiddenModule = _objectSpread2({},
|
|
781
|
+
if (_this4.current) {
|
|
782
|
+
hiddenModule = _objectSpread2({}, _this4.current);
|
|
656
783
|
console.log('[Tracker SDK] Saved module info for restoration:', hiddenModule.module);
|
|
657
784
|
}
|
|
658
|
-
|
|
659
|
-
|
|
785
|
+
_this4.leave(true, false, 'visibility'); // 标记为可见性变化导致的离开
|
|
786
|
+
_this4.queue.flush(true); // 传入 true,标记为页面关闭场景
|
|
660
787
|
|
|
661
788
|
console.log('[Tracker SDK] Data flushed successfully');
|
|
662
789
|
};
|
|
@@ -669,6 +796,9 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
669
796
|
} else if (document.visibilityState === 'visible') {
|
|
670
797
|
console.log('[Tracker SDK] Page is now visible');
|
|
671
798
|
|
|
799
|
+
// 重新启动超时检测
|
|
800
|
+
_this4._startTimeoutCheck();
|
|
801
|
+
|
|
672
802
|
// 如果之前有保存的模块信息,重新 enter
|
|
673
803
|
if (hiddenModule) {
|
|
674
804
|
console.log('[Tracker SDK] Restoring module:', hiddenModule.module);
|
|
@@ -677,7 +807,7 @@ var Tracker = /*#__PURE__*/function () {
|
|
|
677
807
|
var _hiddenModule = hiddenModule;
|
|
678
808
|
_hiddenModule.enterTime;
|
|
679
809
|
var moduleInfo = _objectWithoutProperties(_hiddenModule, _excluded2);
|
|
680
|
-
|
|
810
|
+
_this4.enter(moduleInfo);
|
|
681
811
|
|
|
682
812
|
// 重置 hiddenModule
|
|
683
813
|
hiddenModule = null;
|
|
@@ -792,15 +922,25 @@ function routerPlugin(tracker) {
|
|
|
792
922
|
var lastPath = window.location.pathname + window.location.search + window.location.hash;
|
|
793
923
|
|
|
794
924
|
/**
|
|
795
|
-
*
|
|
925
|
+
* 根据路径获取模块信息
|
|
796
926
|
*/
|
|
797
|
-
var
|
|
927
|
+
var getModuleInfo = function getModuleInfo(path) {
|
|
798
928
|
// 如果提供了自定义映射函数,使用它
|
|
799
929
|
if (typeof moduleMapper === 'function') {
|
|
800
|
-
|
|
930
|
+
var result = moduleMapper(path);
|
|
931
|
+
if (typeof result === 'string') {
|
|
932
|
+
return {
|
|
933
|
+
module: result
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
return result || {
|
|
937
|
+
module: path
|
|
938
|
+
};
|
|
801
939
|
}
|
|
802
940
|
// 否则使用完整路径
|
|
803
|
-
return
|
|
941
|
+
return {
|
|
942
|
+
module: path
|
|
943
|
+
};
|
|
804
944
|
};
|
|
805
945
|
|
|
806
946
|
/**
|
|
@@ -889,10 +1029,9 @@ function routerPlugin(tracker) {
|
|
|
889
1029
|
// 如果路径没变,但 tracker.current 为空(可能被生命周期清空了),需要重新 enter
|
|
890
1030
|
if (!tracker.current) {
|
|
891
1031
|
console.log('[Tracker SDK] Tracker current is null, re-entering current route');
|
|
892
|
-
tracker.enter({
|
|
893
|
-
module: getModuleName(currentPath),
|
|
1032
|
+
tracker.enter(_objectSpread2(_objectSpread2({}, getModuleInfo(currentPath)), {}, {
|
|
894
1033
|
system: system
|
|
895
|
-
});
|
|
1034
|
+
}));
|
|
896
1035
|
}
|
|
897
1036
|
return;
|
|
898
1037
|
}
|
|
@@ -902,11 +1041,10 @@ function routerPlugin(tracker) {
|
|
|
902
1041
|
if (tracker.current) {
|
|
903
1042
|
tracker.leave();
|
|
904
1043
|
}
|
|
905
|
-
tracker.enter({
|
|
906
|
-
|
|
907
|
-
// 使用映射后的模块名
|
|
1044
|
+
tracker.enter(_objectSpread2(_objectSpread2({}, getModuleInfo(currentPath)), {}, {
|
|
1045
|
+
// 使用映射后的模块信息
|
|
908
1046
|
system: system // 使用配置的 system
|
|
909
|
-
});
|
|
1047
|
+
}));
|
|
910
1048
|
lastPath = currentPath;
|
|
911
1049
|
}, 0);
|
|
912
1050
|
};
|
|
@@ -919,10 +1057,9 @@ function routerPlugin(tracker) {
|
|
|
919
1057
|
if (shouldTrack(initialPath)) {
|
|
920
1058
|
lastPath = initialPath; // 更新 lastPath 为实际初始路径
|
|
921
1059
|
|
|
922
|
-
tracker.enter({
|
|
923
|
-
module: getModuleName(initialPath),
|
|
1060
|
+
tracker.enter(_objectSpread2(_objectSpread2({}, getModuleInfo(initialPath)), {}, {
|
|
924
1061
|
system: system
|
|
925
|
-
});
|
|
1062
|
+
}));
|
|
926
1063
|
console.log('[Tracker SDK] Initial route tracked:', initialPath);
|
|
927
1064
|
} else {
|
|
928
1065
|
console.log('[Tracker SDK] Initial route filtered out:', initialPath);
|