@jasolar/tracker-sdk 1.0.8 → 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 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,13 @@ var Tracker = /*#__PURE__*/function () {
532
557
  /**
533
558
  * 记录离开模块
534
559
  * @param {boolean} force - 是否强制使用当前时间作为结束时间(用于页面卸载等场景)
560
+ * @param {boolean} isTimeout - 是否因为超时而离开(内部使用)
535
561
  */
536
562
  }, {
537
563
  key: "leave",
538
564
  value: function leave() {
539
565
  var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
566
+ var isTimeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
540
567
  if (!this.current) {
541
568
  console.log('[Tracker SDK] Leave called but no current module');
542
569
  return;
@@ -547,7 +574,8 @@ var Tracker = /*#__PURE__*/function () {
547
574
  module: this.current.module,
548
575
  system: this.current.system,
549
576
  duration: duration,
550
- force: force
577
+ force: force,
578
+ isTimeout: isTimeout
551
579
  });
552
580
 
553
581
  // 构建 context 对象
@@ -571,7 +599,16 @@ var Tracker = /*#__PURE__*/function () {
571
599
  };
572
600
  console.log('[Tracker SDK] Pushing module_leave event to queue:', event);
573
601
  this.queue.push(event);
574
- this.current = null;
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
+ }
575
612
  }
576
613
 
577
614
  /**
@@ -619,8 +656,44 @@ var Tracker = /*#__PURE__*/function () {
619
656
  value: function _bindActivity() {
620
657
  var _this2 = this;
621
658
  var update = function update() {
659
+ var wasInactive = _this2._checkInactive();
622
660
  _this2.lastActiveTime = now();
623
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
+ }
624
697
  };
625
698
  var events = ['click', 'keydown', 'touchstart'];
626
699
  events.forEach(function (event) {
@@ -638,27 +711,76 @@ var Tracker = /*#__PURE__*/function () {
638
711
  });
639
712
  }
640
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
+
641
760
  /***********生命周期**********/
642
761
  }, {
643
762
  key: "_bindLifecycle",
644
763
  value: function _bindLifecycle() {
645
- var _this3 = this;
764
+ var _this4 = this;
646
765
  // 用于保存页面隐藏前的模块信息
647
766
  var hiddenModule = null;
648
767
 
649
768
  // 页面卸载或隐藏时,上报当前模块的停留时间并强制刷新队列
650
769
  var flushData = function flushData() {
651
770
  console.log('[Tracker SDK] Lifecycle: Page hidden/unloading, flushing data...');
652
- console.log('[Tracker SDK] Current module:', _this3.current);
653
- console.log('[Tracker SDK] Queue length:', _this3.queue.list.length);
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();
654
776
 
655
777
  // 保存当前模块信息,以便页面恢复时重新 enter
656
- if (_this3.current) {
657
- hiddenModule = _objectSpread2({}, _this3.current);
778
+ if (_this4.current) {
779
+ hiddenModule = _objectSpread2({}, _this4.current);
658
780
  console.log('[Tracker SDK] Saved module info for restoration:', hiddenModule.module);
659
781
  }
660
- _this3.leave(true);
661
- _this3.queue.flush(true); // 传入 true,标记为页面关闭场景
782
+ _this4.leave(true);
783
+ _this4.queue.flush(true); // 传入 true,标记为页面关闭场景
662
784
 
663
785
  console.log('[Tracker SDK] Data flushed successfully');
664
786
  };
@@ -671,6 +793,9 @@ var Tracker = /*#__PURE__*/function () {
671
793
  } else if (document.visibilityState === 'visible') {
672
794
  console.log('[Tracker SDK] Page is now visible');
673
795
 
796
+ // 重新启动超时检测
797
+ _this4._startTimeoutCheck();
798
+
674
799
  // 如果之前有保存的模块信息,重新 enter
675
800
  if (hiddenModule) {
676
801
  console.log('[Tracker SDK] Restoring module:', hiddenModule.module);
@@ -679,7 +804,7 @@ var Tracker = /*#__PURE__*/function () {
679
804
  var _hiddenModule = hiddenModule;
680
805
  _hiddenModule.enterTime;
681
806
  var moduleInfo = _objectWithoutProperties(_hiddenModule, _excluded2);
682
- _this3.enter(moduleInfo);
807
+ _this4.enter(moduleInfo);
683
808
 
684
809
  // 重置 hiddenModule
685
810
  hiddenModule = null;
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,13 @@ var Tracker = /*#__PURE__*/function () {
530
555
  /**
531
556
  * 记录离开模块
532
557
  * @param {boolean} force - 是否强制使用当前时间作为结束时间(用于页面卸载等场景)
558
+ * @param {boolean} isTimeout - 是否因为超时而离开(内部使用)
533
559
  */
534
560
  }, {
535
561
  key: "leave",
536
562
  value: function leave() {
537
563
  var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
564
+ var isTimeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
538
565
  if (!this.current) {
539
566
  console.log('[Tracker SDK] Leave called but no current module');
540
567
  return;
@@ -545,7 +572,8 @@ var Tracker = /*#__PURE__*/function () {
545
572
  module: this.current.module,
546
573
  system: this.current.system,
547
574
  duration: duration,
548
- force: force
575
+ force: force,
576
+ isTimeout: isTimeout
549
577
  });
550
578
 
551
579
  // 构建 context 对象
@@ -569,7 +597,16 @@ var Tracker = /*#__PURE__*/function () {
569
597
  };
570
598
  console.log('[Tracker SDK] Pushing module_leave event to queue:', event);
571
599
  this.queue.push(event);
572
- this.current = null;
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
+ }
573
610
  }
574
611
 
575
612
  /**
@@ -617,8 +654,44 @@ var Tracker = /*#__PURE__*/function () {
617
654
  value: function _bindActivity() {
618
655
  var _this2 = this;
619
656
  var update = function update() {
657
+ var wasInactive = _this2._checkInactive();
620
658
  _this2.lastActiveTime = now();
621
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
+ }
622
695
  };
623
696
  var events = ['click', 'keydown', 'touchstart'];
624
697
  events.forEach(function (event) {
@@ -636,27 +709,76 @@ var Tracker = /*#__PURE__*/function () {
636
709
  });
637
710
  }
638
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
+
639
758
  /***********生命周期**********/
640
759
  }, {
641
760
  key: "_bindLifecycle",
642
761
  value: function _bindLifecycle() {
643
- var _this3 = this;
762
+ var _this4 = this;
644
763
  // 用于保存页面隐藏前的模块信息
645
764
  var hiddenModule = null;
646
765
 
647
766
  // 页面卸载或隐藏时,上报当前模块的停留时间并强制刷新队列
648
767
  var flushData = function flushData() {
649
768
  console.log('[Tracker SDK] Lifecycle: Page hidden/unloading, flushing data...');
650
- console.log('[Tracker SDK] Current module:', _this3.current);
651
- console.log('[Tracker SDK] Queue length:', _this3.queue.list.length);
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();
652
774
 
653
775
  // 保存当前模块信息,以便页面恢复时重新 enter
654
- if (_this3.current) {
655
- hiddenModule = _objectSpread2({}, _this3.current);
776
+ if (_this4.current) {
777
+ hiddenModule = _objectSpread2({}, _this4.current);
656
778
  console.log('[Tracker SDK] Saved module info for restoration:', hiddenModule.module);
657
779
  }
658
- _this3.leave(true);
659
- _this3.queue.flush(true); // 传入 true,标记为页面关闭场景
780
+ _this4.leave(true);
781
+ _this4.queue.flush(true); // 传入 true,标记为页面关闭场景
660
782
 
661
783
  console.log('[Tracker SDK] Data flushed successfully');
662
784
  };
@@ -669,6 +791,9 @@ var Tracker = /*#__PURE__*/function () {
669
791
  } else if (document.visibilityState === 'visible') {
670
792
  console.log('[Tracker SDK] Page is now visible');
671
793
 
794
+ // 重新启动超时检测
795
+ _this4._startTimeoutCheck();
796
+
672
797
  // 如果之前有保存的模块信息,重新 enter
673
798
  if (hiddenModule) {
674
799
  console.log('[Tracker SDK] Restoring module:', hiddenModule.module);
@@ -677,7 +802,7 @@ var Tracker = /*#__PURE__*/function () {
677
802
  var _hiddenModule = hiddenModule;
678
803
  _hiddenModule.enterTime;
679
804
  var moduleInfo = _objectWithoutProperties(_hiddenModule, _excluded2);
680
- _this3.enter(moduleInfo);
805
+ _this4.enter(moduleInfo);
681
806
 
682
807
  // 重置 hiddenModule
683
808
  hiddenModule = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jasolar/tracker-sdk",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",