@jasolar/tracker-sdk 1.0.6 → 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/README.md CHANGED
@@ -72,7 +72,28 @@ import { performancePlugin, errorPlugin, routerPlugin, autoTracker } from '@jaso
72
72
 
73
73
  tracker.use(performancePlugin);
74
74
  tracker.use(errorPlugin);
75
+
76
+ // 路由监控 - 基础用法(监听所有路由)
75
77
  tracker.use(routerPlugin);
78
+
79
+ // 路由监控 - 只监听指定路由(白名单)
80
+ tracker.use(routerPlugin, {
81
+ system: 'my-app',
82
+ include: ['/dashboard', '/user/*', /^\/product\//]
83
+ });
84
+
85
+ // 路由监控 - 排除指定路由(黑名单)
86
+ tracker.use(routerPlugin, {
87
+ system: 'my-app',
88
+ exclude: ['/login', '/register', '/admin/*']
89
+ });
90
+
91
+ // 路由监控 - 自定义过滤函数
92
+ tracker.use(routerPlugin, {
93
+ system: 'my-app',
94
+ filter: (path) => !path.startsWith('/test-') // 排除测试路由
95
+ });
96
+
76
97
  tracker.use(autoTracker);
77
98
  ```
78
99
 
@@ -118,6 +139,52 @@ tracker.enter({
118
139
  加载并启用插件。
119
140
  - **plugin**: 插件函数。用于扩展 SDK 的采集能力(如 `performancePlugin`, `errorPlugin` 等)。
120
141
 
142
+ #### Router Plugin 配置选项
143
+
144
+ `routerPlugin` 支持以下配置项:
145
+
146
+ ```javascript
147
+ tracker.use(routerPlugin, {
148
+ system: 'my-app', // 系统标识,默认 'router'
149
+
150
+ // 模块名称映射函数(可选)
151
+ moduleMapper: (path) => {
152
+ if (path === '/dashboard') return '仪表盘';
153
+ return path;
154
+ },
155
+
156
+ // 白名单:只监听这些路由(可选)
157
+ include: [
158
+ '/dashboard', // 完全匹配
159
+ '/user/*', // 前缀匹配(所有 /user/ 开头的路径)
160
+ /^\/product\/\d+$/ // 正则表达式匹配
161
+ ],
162
+
163
+ // 黑名单:排除这些路由(可选)
164
+ exclude: [
165
+ '/login',
166
+ '/admin/*'
167
+ ],
168
+
169
+ // 自定义过滤函数(优先级最高)
170
+ filter: (path) => {
171
+ // 返回 true 表示追踪,false 表示不追踪
172
+ return !path.startsWith('/test-');
173
+ }
174
+ });
175
+ ```
176
+
177
+ **过滤选项说明:**
178
+ - **include**(白名单):只有匹配的路由会被追踪,其他路由被忽略
179
+ - **exclude**(黑名单):匹配的路由不会被追踪,其他路由都被追踪
180
+ - **filter**(自定义函数):最灵活的方式,可以实现任意复杂的过滤逻辑
181
+ - **优先级**:filter > include > exclude(如果同时配置多个,只有优先级最高的生效)
182
+
183
+ **匹配模式:**
184
+ - 字符串完全匹配:`'/dashboard'` 匹配 `/dashboard`
185
+ - 字符串前缀匹配:`'/user/*'` 匹配所有以 `/user/` 开头的路径
186
+ - 正则表达式:`/^\/product\/\d+$/` 匹配 `/product/123` 等
187
+
121
188
  ## 插件系统
122
189
 
123
190
  SDK 通过插件机制扩展功能。目前内置支持:
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
  }
@@ -712,13 +773,22 @@ function errorPlugin(tracker) {
712
773
  * @param {Object} options - 插件配置项
713
774
  * @param {string} [options.system='router'] - 系统标识,默认为 'router'
714
775
  * @param {Function} [options.moduleMapper] - 自定义模块名称映射函数,接收路径作为参数,返回模块名称
776
+ * @param {Array<string>} [options.include] - 白名单:只监听匹配的路由(支持字符串前缀匹配或正则表达式)
777
+ * @param {Array<string>} [options.exclude] - 黑名单:排除匹配的路由(支持字符串前缀匹配或正则表达式)
778
+ * @param {Function} [options.filter] - 自定义过滤函数,接收路径作为参数,返回 true 表示需要追踪
715
779
  */
716
780
  function routerPlugin(tracker) {
717
781
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
718
782
  var _options$system = options.system,
719
783
  system = _options$system === void 0 ? 'router' : _options$system,
720
784
  _options$moduleMapper = options.moduleMapper,
721
- moduleMapper = _options$moduleMapper === void 0 ? null : _options$moduleMapper;
785
+ moduleMapper = _options$moduleMapper === void 0 ? null : _options$moduleMapper,
786
+ _options$include = options.include,
787
+ include = _options$include === void 0 ? null : _options$include,
788
+ _options$exclude = options.exclude,
789
+ exclude = _options$exclude === void 0 ? null : _options$exclude,
790
+ _options$filter = options.filter,
791
+ filter = _options$filter === void 0 ? null : _options$filter;
722
792
 
723
793
  // 获取完整路径:pathname + search + hash
724
794
  var lastPath = window.location.pathname + window.location.search + window.location.hash;
@@ -735,6 +805,66 @@ function routerPlugin(tracker) {
735
805
  return path;
736
806
  };
737
807
 
808
+ /**
809
+ * 检查路径是否应该被追踪
810
+ * @param {string} path - 要检查的路径
811
+ * @returns {boolean} - 是否应该追踪
812
+ */
813
+ var shouldTrack = function shouldTrack(path) {
814
+ // 如果提供了自定义过滤函数,优先使用
815
+ if (typeof filter === 'function') {
816
+ return filter(path);
817
+ }
818
+
819
+ // 检查白名单
820
+ if (include && include.length > 0) {
821
+ var isIncluded = include.some(function (pattern) {
822
+ return matchPath(path, pattern);
823
+ });
824
+ if (!isIncluded) {
825
+ console.log('[Tracker SDK] Route excluded by whitelist:', path);
826
+ return false;
827
+ }
828
+ }
829
+
830
+ // 检查黑名单
831
+ if (exclude && exclude.length > 0) {
832
+ var isExcluded = exclude.some(function (pattern) {
833
+ return matchPath(path, pattern);
834
+ });
835
+ if (isExcluded) {
836
+ console.log('[Tracker SDK] Route excluded by blacklist:', path);
837
+ return false;
838
+ }
839
+ }
840
+ return true;
841
+ };
842
+
843
+ /**
844
+ * 匹配路径和模式
845
+ * @param {string} path - 路径
846
+ * @param {string|RegExp} pattern - 匹配模式(字符串或正则表达式)
847
+ * @returns {boolean} - 是否匹配
848
+ */
849
+ var matchPath = function matchPath(path, pattern) {
850
+ // 如果是正则表达式
851
+ if (pattern instanceof RegExp) {
852
+ return pattern.test(path);
853
+ }
854
+
855
+ // 如果是字符串,进行前缀匹配或完全匹配
856
+ if (typeof pattern === 'string') {
857
+ // 如果模式以 * 结尾,进行前缀匹配
858
+ if (pattern.endsWith('*')) {
859
+ var prefix = pattern.slice(0, -1);
860
+ return path.startsWith(prefix);
861
+ }
862
+ // 否则进行完全匹配
863
+ return path === pattern || path.startsWith(pattern + '?') || path.startsWith(pattern + '#');
864
+ }
865
+ return false;
866
+ };
867
+
738
868
  /**
739
869
  * 追踪页面视图变化
740
870
  */
@@ -748,6 +878,12 @@ function routerPlugin(tracker) {
748
878
  trackerCurrent: tracker.current
749
879
  });
750
880
 
881
+ // 检查是否应该追踪此路由
882
+ if (!shouldTrack(currentPath)) {
883
+ console.log('[Tracker SDK] Route filtered out:', currentPath);
884
+ return;
885
+ }
886
+
751
887
  // 避免重复追踪相同的路径
752
888
  if (currentPath === lastPath) {
753
889
  console.log('[Tracker SDK] Route unchanged:', currentPath, moduleMapper ? moduleMapper(currentPath) : currentPath);
@@ -780,13 +916,19 @@ function routerPlugin(tracker) {
780
916
  // 1. 追踪初始页面加载(延迟执行,确保 DOM 和路由已就绪)
781
917
  setTimeout(function () {
782
918
  var initialPath = window.location.pathname + window.location.search + window.location.hash;
783
- lastPath = initialPath; // 更新 lastPath 为实际初始路径
784
919
 
785
- tracker.enter({
786
- module: getModuleName(initialPath),
787
- system: system
788
- });
789
- console.log('[Tracker SDK] Initial route tracked:', initialPath);
920
+ // 检查初始页面是否应该被追踪
921
+ if (shouldTrack(initialPath)) {
922
+ lastPath = initialPath; // 更新 lastPath 为实际初始路径
923
+
924
+ tracker.enter({
925
+ module: getModuleName(initialPath),
926
+ system: system
927
+ });
928
+ console.log('[Tracker SDK] Initial route tracked:', initialPath);
929
+ } else {
930
+ console.log('[Tracker SDK] Initial route filtered out:', initialPath);
931
+ }
790
932
  }, 0);
791
933
 
792
934
  // 2. 监听 Hash 模式路由变化
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
  }
@@ -710,13 +771,22 @@ function errorPlugin(tracker) {
710
771
  * @param {Object} options - 插件配置项
711
772
  * @param {string} [options.system='router'] - 系统标识,默认为 'router'
712
773
  * @param {Function} [options.moduleMapper] - 自定义模块名称映射函数,接收路径作为参数,返回模块名称
774
+ * @param {Array<string>} [options.include] - 白名单:只监听匹配的路由(支持字符串前缀匹配或正则表达式)
775
+ * @param {Array<string>} [options.exclude] - 黑名单:排除匹配的路由(支持字符串前缀匹配或正则表达式)
776
+ * @param {Function} [options.filter] - 自定义过滤函数,接收路径作为参数,返回 true 表示需要追踪
713
777
  */
714
778
  function routerPlugin(tracker) {
715
779
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
716
780
  var _options$system = options.system,
717
781
  system = _options$system === void 0 ? 'router' : _options$system,
718
782
  _options$moduleMapper = options.moduleMapper,
719
- moduleMapper = _options$moduleMapper === void 0 ? null : _options$moduleMapper;
783
+ moduleMapper = _options$moduleMapper === void 0 ? null : _options$moduleMapper,
784
+ _options$include = options.include,
785
+ include = _options$include === void 0 ? null : _options$include,
786
+ _options$exclude = options.exclude,
787
+ exclude = _options$exclude === void 0 ? null : _options$exclude,
788
+ _options$filter = options.filter,
789
+ filter = _options$filter === void 0 ? null : _options$filter;
720
790
 
721
791
  // 获取完整路径:pathname + search + hash
722
792
  var lastPath = window.location.pathname + window.location.search + window.location.hash;
@@ -733,6 +803,66 @@ function routerPlugin(tracker) {
733
803
  return path;
734
804
  };
735
805
 
806
+ /**
807
+ * 检查路径是否应该被追踪
808
+ * @param {string} path - 要检查的路径
809
+ * @returns {boolean} - 是否应该追踪
810
+ */
811
+ var shouldTrack = function shouldTrack(path) {
812
+ // 如果提供了自定义过滤函数,优先使用
813
+ if (typeof filter === 'function') {
814
+ return filter(path);
815
+ }
816
+
817
+ // 检查白名单
818
+ if (include && include.length > 0) {
819
+ var isIncluded = include.some(function (pattern) {
820
+ return matchPath(path, pattern);
821
+ });
822
+ if (!isIncluded) {
823
+ console.log('[Tracker SDK] Route excluded by whitelist:', path);
824
+ return false;
825
+ }
826
+ }
827
+
828
+ // 检查黑名单
829
+ if (exclude && exclude.length > 0) {
830
+ var isExcluded = exclude.some(function (pattern) {
831
+ return matchPath(path, pattern);
832
+ });
833
+ if (isExcluded) {
834
+ console.log('[Tracker SDK] Route excluded by blacklist:', path);
835
+ return false;
836
+ }
837
+ }
838
+ return true;
839
+ };
840
+
841
+ /**
842
+ * 匹配路径和模式
843
+ * @param {string} path - 路径
844
+ * @param {string|RegExp} pattern - 匹配模式(字符串或正则表达式)
845
+ * @returns {boolean} - 是否匹配
846
+ */
847
+ var matchPath = function matchPath(path, pattern) {
848
+ // 如果是正则表达式
849
+ if (pattern instanceof RegExp) {
850
+ return pattern.test(path);
851
+ }
852
+
853
+ // 如果是字符串,进行前缀匹配或完全匹配
854
+ if (typeof pattern === 'string') {
855
+ // 如果模式以 * 结尾,进行前缀匹配
856
+ if (pattern.endsWith('*')) {
857
+ var prefix = pattern.slice(0, -1);
858
+ return path.startsWith(prefix);
859
+ }
860
+ // 否则进行完全匹配
861
+ return path === pattern || path.startsWith(pattern + '?') || path.startsWith(pattern + '#');
862
+ }
863
+ return false;
864
+ };
865
+
736
866
  /**
737
867
  * 追踪页面视图变化
738
868
  */
@@ -746,6 +876,12 @@ function routerPlugin(tracker) {
746
876
  trackerCurrent: tracker.current
747
877
  });
748
878
 
879
+ // 检查是否应该追踪此路由
880
+ if (!shouldTrack(currentPath)) {
881
+ console.log('[Tracker SDK] Route filtered out:', currentPath);
882
+ return;
883
+ }
884
+
749
885
  // 避免重复追踪相同的路径
750
886
  if (currentPath === lastPath) {
751
887
  console.log('[Tracker SDK] Route unchanged:', currentPath, moduleMapper ? moduleMapper(currentPath) : currentPath);
@@ -778,13 +914,19 @@ function routerPlugin(tracker) {
778
914
  // 1. 追踪初始页面加载(延迟执行,确保 DOM 和路由已就绪)
779
915
  setTimeout(function () {
780
916
  var initialPath = window.location.pathname + window.location.search + window.location.hash;
781
- lastPath = initialPath; // 更新 lastPath 为实际初始路径
782
917
 
783
- tracker.enter({
784
- module: getModuleName(initialPath),
785
- system: system
786
- });
787
- console.log('[Tracker SDK] Initial route tracked:', initialPath);
918
+ // 检查初始页面是否应该被追踪
919
+ if (shouldTrack(initialPath)) {
920
+ lastPath = initialPath; // 更新 lastPath 为实际初始路径
921
+
922
+ tracker.enter({
923
+ module: getModuleName(initialPath),
924
+ system: system
925
+ });
926
+ console.log('[Tracker SDK] Initial route tracked:', initialPath);
927
+ } else {
928
+ console.log('[Tracker SDK] Initial route filtered out:', initialPath);
929
+ }
788
930
  }, 0);
789
931
 
790
932
  // 2. 监听 Hash 模式路由变化
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jasolar/tracker-sdk",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",