@jasolar/tracker-sdk 1.0.9 → 1.0.11

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 CHANGED
@@ -558,12 +558,14 @@ var Tracker = /*#__PURE__*/function () {
558
558
  * 记录离开模块
559
559
  * @param {boolean} force - 是否强制使用当前时间作为结束时间(用于页面卸载等场景)
560
560
  * @param {boolean} isTimeout - 是否因为超时而离开(内部使用)
561
+ * @param {string} reason - 离开原因(可选):'timeout' | 'visibility' | 'normal'
561
562
  */
562
563
  }, {
563
564
  key: "leave",
564
565
  value: function leave() {
565
566
  var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
566
567
  var isTimeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
568
+ var reason = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'normal';
567
569
  if (!this.current) {
568
570
  console.log('[Tracker SDK] Leave called but no current module');
569
571
  return;
@@ -575,7 +577,8 @@ var Tracker = /*#__PURE__*/function () {
575
577
  system: this.current.system,
576
578
  duration: duration,
577
579
  force: force,
578
- isTimeout: isTimeout
580
+ isTimeout: isTimeout,
581
+ reason: reason
579
582
  });
580
583
 
581
584
  // 构建 context 对象
@@ -595,7 +598,9 @@ var Tracker = /*#__PURE__*/function () {
595
598
  event: 'module_leave',
596
599
  timestamp: formatTime(now()),
597
600
  context: context,
598
- ext: extData
601
+ ext: _objectSpread2(_objectSpread2({}, extData), {}, {
602
+ leave_reason: reason // 添加离开原因
603
+ })
599
604
  };
600
605
  console.log('[Tracker SDK] Pushing module_leave event to queue:', event);
601
606
  this.queue.push(event);
@@ -657,6 +662,14 @@ var Tracker = /*#__PURE__*/function () {
657
662
  var _this2 = this;
658
663
  var update = function update() {
659
664
  var wasInactive = _this2._checkInactive();
665
+
666
+ // 如果确实已经处于非活跃状态太久,但定时器还没来得及触发 (例如电脑休眠唤醒)
667
+ // 此时应主动触发 leave(),并重置会话
668
+ if (wasInactive && !_this2.isTimedOut && _this2.current) {
669
+ console.log('[Tracker SDK] Activity detected after long inactivity, forcing timeout leave');
670
+ _this2.leave(false, true, 'timeout');
671
+ _this2.sessionId = resetSession();
672
+ }
660
673
  _this2.lastActiveTime = now();
661
674
  refreshSession(); // 用户活跃时刷新 Session 有效期
662
675
 
@@ -729,6 +742,8 @@ var Tracker = /*#__PURE__*/function () {
729
742
  key: "_startTimeoutCheck",
730
743
  value: function _startTimeoutCheck() {
731
744
  var _this3 = this;
745
+ this._stopTimeoutCheck(); // 确保不重复启动定时器
746
+
732
747
  // 动态计算检测间隔:超时时间的 1/6,但不超过 30 秒,不少于 5 秒
733
748
  var checkInterval = Math.min(Math.max(this.options.sessionTimeout / 6, 5 * 1000), 30 * 1000);
734
749
  console.log('[Tracker SDK] Timeout check interval:', (checkInterval / 1000).toFixed(1), 's');
@@ -736,7 +751,7 @@ var Tracker = /*#__PURE__*/function () {
736
751
  // 只要有 current 且未处于超时状态,就检测是否超时
737
752
  if (_this3.current && !_this3.isTimedOut && _this3._checkInactive()) {
738
753
  console.log('[Tracker SDK] User inactive for too long, triggering auto leave');
739
- _this3.leave(true, true); // 强制上报离开事件,标记为超时
754
+ _this3.leave(false, true, 'timeout'); // 使用 lastActiveTime 结算,标记为超时
740
755
 
741
756
  // 重置 Session,认为会话结束
742
757
  _this3.sessionId = resetSession();
@@ -779,7 +794,7 @@ var Tracker = /*#__PURE__*/function () {
779
794
  hiddenModule = _objectSpread2({}, _this4.current);
780
795
  console.log('[Tracker SDK] Saved module info for restoration:', hiddenModule.module);
781
796
  }
782
- _this4.leave(true);
797
+ _this4.leave(true, false, 'visibility'); // 标记为可见性变化导致的离开
783
798
  _this4.queue.flush(true); // 传入 true,标记为页面关闭场景
784
799
 
785
800
  console.log('[Tracker SDK] Data flushed successfully');
@@ -919,15 +934,25 @@ function routerPlugin(tracker) {
919
934
  var lastPath = window.location.pathname + window.location.search + window.location.hash;
920
935
 
921
936
  /**
922
- * 根据路径获取模块名称
937
+ * 根据路径获取模块信息
923
938
  */
924
- var getModuleName = function getModuleName(path) {
939
+ var getModuleInfo = function getModuleInfo(path) {
925
940
  // 如果提供了自定义映射函数,使用它
926
941
  if (typeof moduleMapper === 'function') {
927
- return moduleMapper(path);
942
+ var result = moduleMapper(path);
943
+ if (typeof result === 'string') {
944
+ return {
945
+ module: result
946
+ };
947
+ }
948
+ return result || {
949
+ module: path
950
+ };
928
951
  }
929
952
  // 否则使用完整路径
930
- return path;
953
+ return {
954
+ module: path
955
+ };
931
956
  };
932
957
 
933
958
  /**
@@ -1016,10 +1041,9 @@ function routerPlugin(tracker) {
1016
1041
  // 如果路径没变,但 tracker.current 为空(可能被生命周期清空了),需要重新 enter
1017
1042
  if (!tracker.current) {
1018
1043
  console.log('[Tracker SDK] Tracker current is null, re-entering current route');
1019
- tracker.enter({
1020
- module: getModuleName(currentPath),
1044
+ tracker.enter(_objectSpread2(_objectSpread2({}, getModuleInfo(currentPath)), {}, {
1021
1045
  system: system
1022
- });
1046
+ }));
1023
1047
  }
1024
1048
  return;
1025
1049
  }
@@ -1029,11 +1053,10 @@ function routerPlugin(tracker) {
1029
1053
  if (tracker.current) {
1030
1054
  tracker.leave();
1031
1055
  }
1032
- tracker.enter({
1033
- module: getModuleName(currentPath),
1034
- // 使用映射后的模块名
1056
+ tracker.enter(_objectSpread2(_objectSpread2({}, getModuleInfo(currentPath)), {}, {
1057
+ // 使用映射后的模块信息
1035
1058
  system: system // 使用配置的 system
1036
- });
1059
+ }));
1037
1060
  lastPath = currentPath;
1038
1061
  }, 0);
1039
1062
  };
@@ -1046,10 +1069,9 @@ function routerPlugin(tracker) {
1046
1069
  if (shouldTrack(initialPath)) {
1047
1070
  lastPath = initialPath; // 更新 lastPath 为实际初始路径
1048
1071
 
1049
- tracker.enter({
1050
- module: getModuleName(initialPath),
1072
+ tracker.enter(_objectSpread2(_objectSpread2({}, getModuleInfo(initialPath)), {}, {
1051
1073
  system: system
1052
- });
1074
+ }));
1053
1075
  console.log('[Tracker SDK] Initial route tracked:', initialPath);
1054
1076
  } else {
1055
1077
  console.log('[Tracker SDK] Initial route filtered out:', initialPath);
package/dist/index.esm.js CHANGED
@@ -556,12 +556,14 @@ var Tracker = /*#__PURE__*/function () {
556
556
  * 记录离开模块
557
557
  * @param {boolean} force - 是否强制使用当前时间作为结束时间(用于页面卸载等场景)
558
558
  * @param {boolean} isTimeout - 是否因为超时而离开(内部使用)
559
+ * @param {string} reason - 离开原因(可选):'timeout' | 'visibility' | 'normal'
559
560
  */
560
561
  }, {
561
562
  key: "leave",
562
563
  value: function leave() {
563
564
  var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
564
565
  var isTimeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
566
+ var reason = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'normal';
565
567
  if (!this.current) {
566
568
  console.log('[Tracker SDK] Leave called but no current module');
567
569
  return;
@@ -573,7 +575,8 @@ var Tracker = /*#__PURE__*/function () {
573
575
  system: this.current.system,
574
576
  duration: duration,
575
577
  force: force,
576
- isTimeout: isTimeout
578
+ isTimeout: isTimeout,
579
+ reason: reason
577
580
  });
578
581
 
579
582
  // 构建 context 对象
@@ -593,7 +596,9 @@ var Tracker = /*#__PURE__*/function () {
593
596
  event: 'module_leave',
594
597
  timestamp: formatTime(now()),
595
598
  context: context,
596
- ext: extData
599
+ ext: _objectSpread2(_objectSpread2({}, extData), {}, {
600
+ leave_reason: reason // 添加离开原因
601
+ })
597
602
  };
598
603
  console.log('[Tracker SDK] Pushing module_leave event to queue:', event);
599
604
  this.queue.push(event);
@@ -655,6 +660,14 @@ var Tracker = /*#__PURE__*/function () {
655
660
  var _this2 = this;
656
661
  var update = function update() {
657
662
  var wasInactive = _this2._checkInactive();
663
+
664
+ // 如果确实已经处于非活跃状态太久,但定时器还没来得及触发 (例如电脑休眠唤醒)
665
+ // 此时应主动触发 leave(),并重置会话
666
+ if (wasInactive && !_this2.isTimedOut && _this2.current) {
667
+ console.log('[Tracker SDK] Activity detected after long inactivity, forcing timeout leave');
668
+ _this2.leave(false, true, 'timeout');
669
+ _this2.sessionId = resetSession();
670
+ }
658
671
  _this2.lastActiveTime = now();
659
672
  refreshSession(); // 用户活跃时刷新 Session 有效期
660
673
 
@@ -727,6 +740,8 @@ var Tracker = /*#__PURE__*/function () {
727
740
  key: "_startTimeoutCheck",
728
741
  value: function _startTimeoutCheck() {
729
742
  var _this3 = this;
743
+ this._stopTimeoutCheck(); // 确保不重复启动定时器
744
+
730
745
  // 动态计算检测间隔:超时时间的 1/6,但不超过 30 秒,不少于 5 秒
731
746
  var checkInterval = Math.min(Math.max(this.options.sessionTimeout / 6, 5 * 1000), 30 * 1000);
732
747
  console.log('[Tracker SDK] Timeout check interval:', (checkInterval / 1000).toFixed(1), 's');
@@ -734,7 +749,7 @@ var Tracker = /*#__PURE__*/function () {
734
749
  // 只要有 current 且未处于超时状态,就检测是否超时
735
750
  if (_this3.current && !_this3.isTimedOut && _this3._checkInactive()) {
736
751
  console.log('[Tracker SDK] User inactive for too long, triggering auto leave');
737
- _this3.leave(true, true); // 强制上报离开事件,标记为超时
752
+ _this3.leave(false, true, 'timeout'); // 使用 lastActiveTime 结算,标记为超时
738
753
 
739
754
  // 重置 Session,认为会话结束
740
755
  _this3.sessionId = resetSession();
@@ -777,7 +792,7 @@ var Tracker = /*#__PURE__*/function () {
777
792
  hiddenModule = _objectSpread2({}, _this4.current);
778
793
  console.log('[Tracker SDK] Saved module info for restoration:', hiddenModule.module);
779
794
  }
780
- _this4.leave(true);
795
+ _this4.leave(true, false, 'visibility'); // 标记为可见性变化导致的离开
781
796
  _this4.queue.flush(true); // 传入 true,标记为页面关闭场景
782
797
 
783
798
  console.log('[Tracker SDK] Data flushed successfully');
@@ -917,15 +932,25 @@ function routerPlugin(tracker) {
917
932
  var lastPath = window.location.pathname + window.location.search + window.location.hash;
918
933
 
919
934
  /**
920
- * 根据路径获取模块名称
935
+ * 根据路径获取模块信息
921
936
  */
922
- var getModuleName = function getModuleName(path) {
937
+ var getModuleInfo = function getModuleInfo(path) {
923
938
  // 如果提供了自定义映射函数,使用它
924
939
  if (typeof moduleMapper === 'function') {
925
- return moduleMapper(path);
940
+ var result = moduleMapper(path);
941
+ if (typeof result === 'string') {
942
+ return {
943
+ module: result
944
+ };
945
+ }
946
+ return result || {
947
+ module: path
948
+ };
926
949
  }
927
950
  // 否则使用完整路径
928
- return path;
951
+ return {
952
+ module: path
953
+ };
929
954
  };
930
955
 
931
956
  /**
@@ -1014,10 +1039,9 @@ function routerPlugin(tracker) {
1014
1039
  // 如果路径没变,但 tracker.current 为空(可能被生命周期清空了),需要重新 enter
1015
1040
  if (!tracker.current) {
1016
1041
  console.log('[Tracker SDK] Tracker current is null, re-entering current route');
1017
- tracker.enter({
1018
- module: getModuleName(currentPath),
1042
+ tracker.enter(_objectSpread2(_objectSpread2({}, getModuleInfo(currentPath)), {}, {
1019
1043
  system: system
1020
- });
1044
+ }));
1021
1045
  }
1022
1046
  return;
1023
1047
  }
@@ -1027,11 +1051,10 @@ function routerPlugin(tracker) {
1027
1051
  if (tracker.current) {
1028
1052
  tracker.leave();
1029
1053
  }
1030
- tracker.enter({
1031
- module: getModuleName(currentPath),
1032
- // 使用映射后的模块名
1054
+ tracker.enter(_objectSpread2(_objectSpread2({}, getModuleInfo(currentPath)), {}, {
1055
+ // 使用映射后的模块信息
1033
1056
  system: system // 使用配置的 system
1034
- });
1057
+ }));
1035
1058
  lastPath = currentPath;
1036
1059
  }, 0);
1037
1060
  };
@@ -1044,10 +1067,9 @@ function routerPlugin(tracker) {
1044
1067
  if (shouldTrack(initialPath)) {
1045
1068
  lastPath = initialPath; // 更新 lastPath 为实际初始路径
1046
1069
 
1047
- tracker.enter({
1048
- module: getModuleName(initialPath),
1070
+ tracker.enter(_objectSpread2(_objectSpread2({}, getModuleInfo(initialPath)), {}, {
1049
1071
  system: system
1050
- });
1072
+ }));
1051
1073
  console.log('[Tracker SDK] Initial route tracked:', initialPath);
1052
1074
  } else {
1053
1075
  console.log('[Tracker SDK] Initial route filtered out:', initialPath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jasolar/tracker-sdk",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",