@jasolar/tracker-sdk 1.0.7 → 1.0.8

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
@@ -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) return;
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
- return navigator.sendBeacon(beaconUrl, blob);
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
@@ -382,7 +399,8 @@ function throttle(fn, delay) {
382
399
  };
383
400
  }
384
401
 
385
- var _excluded = ["system", "module", "sub_module"];
402
+ var _excluded = ["system", "module", "sub_module"],
403
+ _excluded2 = ["enterTime"];
386
404
 
387
405
  /**
388
406
  * Tracker 核心类
@@ -520,10 +538,17 @@ var Tracker = /*#__PURE__*/function () {
520
538
  value: function leave() {
521
539
  var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
522
540
  if (!this.current) {
541
+ console.log('[Tracker SDK] Leave called but no current module');
523
542
  return;
524
543
  }
525
544
  var endTime = force ? now() : this.lastActiveTime;
526
545
  var duration = Math.max(0, endTime - this.current.enterTime);
546
+ console.log('[Tracker SDK] Leaving module:', {
547
+ module: this.current.module,
548
+ system: this.current.system,
549
+ duration: duration,
550
+ force: force
551
+ });
527
552
 
528
553
  // 构建 context 对象
529
554
  var context = {
@@ -544,6 +569,7 @@ var Tracker = /*#__PURE__*/function () {
544
569
  context: context,
545
570
  ext: extData
546
571
  };
572
+ console.log('[Tracker SDK] Pushing module_leave event to queue:', event);
547
573
  this.queue.push(event);
548
574
  this.current = null;
549
575
  }
@@ -617,15 +643,50 @@ var Tracker = /*#__PURE__*/function () {
617
643
  key: "_bindLifecycle",
618
644
  value: function _bindLifecycle() {
619
645
  var _this3 = this;
646
+ // 用于保存页面隐藏前的模块信息
647
+ var hiddenModule = null;
648
+
620
649
  // 页面卸载或隐藏时,上报当前模块的停留时间并强制刷新队列
621
650
  var flushData = function flushData() {
651
+ 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);
654
+
655
+ // 保存当前模块信息,以便页面恢复时重新 enter
656
+ if (_this3.current) {
657
+ hiddenModule = _objectSpread2({}, _this3.current);
658
+ console.log('[Tracker SDK] Saved module info for restoration:', hiddenModule.module);
659
+ }
622
660
  _this3.leave(true);
623
661
  _this3.queue.flush(true); // 传入 true,标记为页面关闭场景
662
+
663
+ console.log('[Tracker SDK] Data flushed successfully');
624
664
  };
625
665
  window.addEventListener('beforeunload', flushData);
626
666
  document.addEventListener('visibilitychange', function () {
667
+ console.log('[Tracker SDK] Visibility changed:', document.visibilityState);
627
668
  if (document.visibilityState === 'hidden') {
669
+ console.log('[Tracker SDK] Page is now hidden, triggering flush...');
628
670
  flushData();
671
+ } else if (document.visibilityState === 'visible') {
672
+ console.log('[Tracker SDK] Page is now visible');
673
+
674
+ // 如果之前有保存的模块信息,重新 enter
675
+ if (hiddenModule) {
676
+ console.log('[Tracker SDK] Restoring module:', hiddenModule.module);
677
+
678
+ // 清除 enterTime,使用当前时间作为新的进入时间
679
+ var _hiddenModule = hiddenModule;
680
+ _hiddenModule.enterTime;
681
+ var moduleInfo = _objectWithoutProperties(_hiddenModule, _excluded2);
682
+ _this3.enter(moduleInfo);
683
+
684
+ // 重置 hiddenModule
685
+ hiddenModule = null;
686
+ console.log('[Tracker SDK] Module restored successfully');
687
+ } else {
688
+ console.log('[Tracker SDK] No module to restore');
689
+ }
629
690
  }
630
691
  });
631
692
  }
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) return;
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
- return navigator.sendBeacon(beaconUrl, blob);
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
@@ -380,7 +397,8 @@ function throttle(fn, delay) {
380
397
  };
381
398
  }
382
399
 
383
- var _excluded = ["system", "module", "sub_module"];
400
+ var _excluded = ["system", "module", "sub_module"],
401
+ _excluded2 = ["enterTime"];
384
402
 
385
403
  /**
386
404
  * Tracker 核心类
@@ -518,10 +536,17 @@ var Tracker = /*#__PURE__*/function () {
518
536
  value: function leave() {
519
537
  var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
520
538
  if (!this.current) {
539
+ console.log('[Tracker SDK] Leave called but no current module');
521
540
  return;
522
541
  }
523
542
  var endTime = force ? now() : this.lastActiveTime;
524
543
  var duration = Math.max(0, endTime - this.current.enterTime);
544
+ console.log('[Tracker SDK] Leaving module:', {
545
+ module: this.current.module,
546
+ system: this.current.system,
547
+ duration: duration,
548
+ force: force
549
+ });
525
550
 
526
551
  // 构建 context 对象
527
552
  var context = {
@@ -542,6 +567,7 @@ var Tracker = /*#__PURE__*/function () {
542
567
  context: context,
543
568
  ext: extData
544
569
  };
570
+ console.log('[Tracker SDK] Pushing module_leave event to queue:', event);
545
571
  this.queue.push(event);
546
572
  this.current = null;
547
573
  }
@@ -615,15 +641,50 @@ var Tracker = /*#__PURE__*/function () {
615
641
  key: "_bindLifecycle",
616
642
  value: function _bindLifecycle() {
617
643
  var _this3 = this;
644
+ // 用于保存页面隐藏前的模块信息
645
+ var hiddenModule = null;
646
+
618
647
  // 页面卸载或隐藏时,上报当前模块的停留时间并强制刷新队列
619
648
  var flushData = function flushData() {
649
+ 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);
652
+
653
+ // 保存当前模块信息,以便页面恢复时重新 enter
654
+ if (_this3.current) {
655
+ hiddenModule = _objectSpread2({}, _this3.current);
656
+ console.log('[Tracker SDK] Saved module info for restoration:', hiddenModule.module);
657
+ }
620
658
  _this3.leave(true);
621
659
  _this3.queue.flush(true); // 传入 true,标记为页面关闭场景
660
+
661
+ console.log('[Tracker SDK] Data flushed successfully');
622
662
  };
623
663
  window.addEventListener('beforeunload', flushData);
624
664
  document.addEventListener('visibilitychange', function () {
665
+ console.log('[Tracker SDK] Visibility changed:', document.visibilityState);
625
666
  if (document.visibilityState === 'hidden') {
667
+ console.log('[Tracker SDK] Page is now hidden, triggering flush...');
626
668
  flushData();
669
+ } else if (document.visibilityState === 'visible') {
670
+ console.log('[Tracker SDK] Page is now visible');
671
+
672
+ // 如果之前有保存的模块信息,重新 enter
673
+ if (hiddenModule) {
674
+ console.log('[Tracker SDK] Restoring module:', hiddenModule.module);
675
+
676
+ // 清除 enterTime,使用当前时间作为新的进入时间
677
+ var _hiddenModule = hiddenModule;
678
+ _hiddenModule.enterTime;
679
+ var moduleInfo = _objectWithoutProperties(_hiddenModule, _excluded2);
680
+ _this3.enter(moduleInfo);
681
+
682
+ // 重置 hiddenModule
683
+ hiddenModule = null;
684
+ console.log('[Tracker SDK] Module restored successfully');
685
+ } else {
686
+ console.log('[Tracker SDK] No module to restore');
687
+ }
627
688
  }
628
689
  });
629
690
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jasolar/tracker-sdk",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",