@jasolar/tracker-sdk 1.0.6 → 1.0.7
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 +67 -0
- package/dist/index.cjs.js +88 -7
- package/dist/index.esm.js +88 -7
- package/package.json +1 -1
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
|
@@ -712,13 +712,22 @@ function errorPlugin(tracker) {
|
|
|
712
712
|
* @param {Object} options - 插件配置项
|
|
713
713
|
* @param {string} [options.system='router'] - 系统标识,默认为 'router'
|
|
714
714
|
* @param {Function} [options.moduleMapper] - 自定义模块名称映射函数,接收路径作为参数,返回模块名称
|
|
715
|
+
* @param {Array<string>} [options.include] - 白名单:只监听匹配的路由(支持字符串前缀匹配或正则表达式)
|
|
716
|
+
* @param {Array<string>} [options.exclude] - 黑名单:排除匹配的路由(支持字符串前缀匹配或正则表达式)
|
|
717
|
+
* @param {Function} [options.filter] - 自定义过滤函数,接收路径作为参数,返回 true 表示需要追踪
|
|
715
718
|
*/
|
|
716
719
|
function routerPlugin(tracker) {
|
|
717
720
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
718
721
|
var _options$system = options.system,
|
|
719
722
|
system = _options$system === void 0 ? 'router' : _options$system,
|
|
720
723
|
_options$moduleMapper = options.moduleMapper,
|
|
721
|
-
moduleMapper = _options$moduleMapper === void 0 ? null : _options$moduleMapper
|
|
724
|
+
moduleMapper = _options$moduleMapper === void 0 ? null : _options$moduleMapper,
|
|
725
|
+
_options$include = options.include,
|
|
726
|
+
include = _options$include === void 0 ? null : _options$include,
|
|
727
|
+
_options$exclude = options.exclude,
|
|
728
|
+
exclude = _options$exclude === void 0 ? null : _options$exclude,
|
|
729
|
+
_options$filter = options.filter,
|
|
730
|
+
filter = _options$filter === void 0 ? null : _options$filter;
|
|
722
731
|
|
|
723
732
|
// 获取完整路径:pathname + search + hash
|
|
724
733
|
var lastPath = window.location.pathname + window.location.search + window.location.hash;
|
|
@@ -735,6 +744,66 @@ function routerPlugin(tracker) {
|
|
|
735
744
|
return path;
|
|
736
745
|
};
|
|
737
746
|
|
|
747
|
+
/**
|
|
748
|
+
* 检查路径是否应该被追踪
|
|
749
|
+
* @param {string} path - 要检查的路径
|
|
750
|
+
* @returns {boolean} - 是否应该追踪
|
|
751
|
+
*/
|
|
752
|
+
var shouldTrack = function shouldTrack(path) {
|
|
753
|
+
// 如果提供了自定义过滤函数,优先使用
|
|
754
|
+
if (typeof filter === 'function') {
|
|
755
|
+
return filter(path);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
// 检查白名单
|
|
759
|
+
if (include && include.length > 0) {
|
|
760
|
+
var isIncluded = include.some(function (pattern) {
|
|
761
|
+
return matchPath(path, pattern);
|
|
762
|
+
});
|
|
763
|
+
if (!isIncluded) {
|
|
764
|
+
console.log('[Tracker SDK] Route excluded by whitelist:', path);
|
|
765
|
+
return false;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
// 检查黑名单
|
|
770
|
+
if (exclude && exclude.length > 0) {
|
|
771
|
+
var isExcluded = exclude.some(function (pattern) {
|
|
772
|
+
return matchPath(path, pattern);
|
|
773
|
+
});
|
|
774
|
+
if (isExcluded) {
|
|
775
|
+
console.log('[Tracker SDK] Route excluded by blacklist:', path);
|
|
776
|
+
return false;
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
return true;
|
|
780
|
+
};
|
|
781
|
+
|
|
782
|
+
/**
|
|
783
|
+
* 匹配路径和模式
|
|
784
|
+
* @param {string} path - 路径
|
|
785
|
+
* @param {string|RegExp} pattern - 匹配模式(字符串或正则表达式)
|
|
786
|
+
* @returns {boolean} - 是否匹配
|
|
787
|
+
*/
|
|
788
|
+
var matchPath = function matchPath(path, pattern) {
|
|
789
|
+
// 如果是正则表达式
|
|
790
|
+
if (pattern instanceof RegExp) {
|
|
791
|
+
return pattern.test(path);
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
// 如果是字符串,进行前缀匹配或完全匹配
|
|
795
|
+
if (typeof pattern === 'string') {
|
|
796
|
+
// 如果模式以 * 结尾,进行前缀匹配
|
|
797
|
+
if (pattern.endsWith('*')) {
|
|
798
|
+
var prefix = pattern.slice(0, -1);
|
|
799
|
+
return path.startsWith(prefix);
|
|
800
|
+
}
|
|
801
|
+
// 否则进行完全匹配
|
|
802
|
+
return path === pattern || path.startsWith(pattern + '?') || path.startsWith(pattern + '#');
|
|
803
|
+
}
|
|
804
|
+
return false;
|
|
805
|
+
};
|
|
806
|
+
|
|
738
807
|
/**
|
|
739
808
|
* 追踪页面视图变化
|
|
740
809
|
*/
|
|
@@ -748,6 +817,12 @@ function routerPlugin(tracker) {
|
|
|
748
817
|
trackerCurrent: tracker.current
|
|
749
818
|
});
|
|
750
819
|
|
|
820
|
+
// 检查是否应该追踪此路由
|
|
821
|
+
if (!shouldTrack(currentPath)) {
|
|
822
|
+
console.log('[Tracker SDK] Route filtered out:', currentPath);
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
|
|
751
826
|
// 避免重复追踪相同的路径
|
|
752
827
|
if (currentPath === lastPath) {
|
|
753
828
|
console.log('[Tracker SDK] Route unchanged:', currentPath, moduleMapper ? moduleMapper(currentPath) : currentPath);
|
|
@@ -780,13 +855,19 @@ function routerPlugin(tracker) {
|
|
|
780
855
|
// 1. 追踪初始页面加载(延迟执行,确保 DOM 和路由已就绪)
|
|
781
856
|
setTimeout(function () {
|
|
782
857
|
var initialPath = window.location.pathname + window.location.search + window.location.hash;
|
|
783
|
-
lastPath = initialPath; // 更新 lastPath 为实际初始路径
|
|
784
858
|
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
859
|
+
// 检查初始页面是否应该被追踪
|
|
860
|
+
if (shouldTrack(initialPath)) {
|
|
861
|
+
lastPath = initialPath; // 更新 lastPath 为实际初始路径
|
|
862
|
+
|
|
863
|
+
tracker.enter({
|
|
864
|
+
module: getModuleName(initialPath),
|
|
865
|
+
system: system
|
|
866
|
+
});
|
|
867
|
+
console.log('[Tracker SDK] Initial route tracked:', initialPath);
|
|
868
|
+
} else {
|
|
869
|
+
console.log('[Tracker SDK] Initial route filtered out:', initialPath);
|
|
870
|
+
}
|
|
790
871
|
}, 0);
|
|
791
872
|
|
|
792
873
|
// 2. 监听 Hash 模式路由变化
|
package/dist/index.esm.js
CHANGED
|
@@ -710,13 +710,22 @@ function errorPlugin(tracker) {
|
|
|
710
710
|
* @param {Object} options - 插件配置项
|
|
711
711
|
* @param {string} [options.system='router'] - 系统标识,默认为 'router'
|
|
712
712
|
* @param {Function} [options.moduleMapper] - 自定义模块名称映射函数,接收路径作为参数,返回模块名称
|
|
713
|
+
* @param {Array<string>} [options.include] - 白名单:只监听匹配的路由(支持字符串前缀匹配或正则表达式)
|
|
714
|
+
* @param {Array<string>} [options.exclude] - 黑名单:排除匹配的路由(支持字符串前缀匹配或正则表达式)
|
|
715
|
+
* @param {Function} [options.filter] - 自定义过滤函数,接收路径作为参数,返回 true 表示需要追踪
|
|
713
716
|
*/
|
|
714
717
|
function routerPlugin(tracker) {
|
|
715
718
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
716
719
|
var _options$system = options.system,
|
|
717
720
|
system = _options$system === void 0 ? 'router' : _options$system,
|
|
718
721
|
_options$moduleMapper = options.moduleMapper,
|
|
719
|
-
moduleMapper = _options$moduleMapper === void 0 ? null : _options$moduleMapper
|
|
722
|
+
moduleMapper = _options$moduleMapper === void 0 ? null : _options$moduleMapper,
|
|
723
|
+
_options$include = options.include,
|
|
724
|
+
include = _options$include === void 0 ? null : _options$include,
|
|
725
|
+
_options$exclude = options.exclude,
|
|
726
|
+
exclude = _options$exclude === void 0 ? null : _options$exclude,
|
|
727
|
+
_options$filter = options.filter,
|
|
728
|
+
filter = _options$filter === void 0 ? null : _options$filter;
|
|
720
729
|
|
|
721
730
|
// 获取完整路径:pathname + search + hash
|
|
722
731
|
var lastPath = window.location.pathname + window.location.search + window.location.hash;
|
|
@@ -733,6 +742,66 @@ function routerPlugin(tracker) {
|
|
|
733
742
|
return path;
|
|
734
743
|
};
|
|
735
744
|
|
|
745
|
+
/**
|
|
746
|
+
* 检查路径是否应该被追踪
|
|
747
|
+
* @param {string} path - 要检查的路径
|
|
748
|
+
* @returns {boolean} - 是否应该追踪
|
|
749
|
+
*/
|
|
750
|
+
var shouldTrack = function shouldTrack(path) {
|
|
751
|
+
// 如果提供了自定义过滤函数,优先使用
|
|
752
|
+
if (typeof filter === 'function') {
|
|
753
|
+
return filter(path);
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// 检查白名单
|
|
757
|
+
if (include && include.length > 0) {
|
|
758
|
+
var isIncluded = include.some(function (pattern) {
|
|
759
|
+
return matchPath(path, pattern);
|
|
760
|
+
});
|
|
761
|
+
if (!isIncluded) {
|
|
762
|
+
console.log('[Tracker SDK] Route excluded by whitelist:', path);
|
|
763
|
+
return false;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
// 检查黑名单
|
|
768
|
+
if (exclude && exclude.length > 0) {
|
|
769
|
+
var isExcluded = exclude.some(function (pattern) {
|
|
770
|
+
return matchPath(path, pattern);
|
|
771
|
+
});
|
|
772
|
+
if (isExcluded) {
|
|
773
|
+
console.log('[Tracker SDK] Route excluded by blacklist:', path);
|
|
774
|
+
return false;
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
return true;
|
|
778
|
+
};
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* 匹配路径和模式
|
|
782
|
+
* @param {string} path - 路径
|
|
783
|
+
* @param {string|RegExp} pattern - 匹配模式(字符串或正则表达式)
|
|
784
|
+
* @returns {boolean} - 是否匹配
|
|
785
|
+
*/
|
|
786
|
+
var matchPath = function matchPath(path, pattern) {
|
|
787
|
+
// 如果是正则表达式
|
|
788
|
+
if (pattern instanceof RegExp) {
|
|
789
|
+
return pattern.test(path);
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
// 如果是字符串,进行前缀匹配或完全匹配
|
|
793
|
+
if (typeof pattern === 'string') {
|
|
794
|
+
// 如果模式以 * 结尾,进行前缀匹配
|
|
795
|
+
if (pattern.endsWith('*')) {
|
|
796
|
+
var prefix = pattern.slice(0, -1);
|
|
797
|
+
return path.startsWith(prefix);
|
|
798
|
+
}
|
|
799
|
+
// 否则进行完全匹配
|
|
800
|
+
return path === pattern || path.startsWith(pattern + '?') || path.startsWith(pattern + '#');
|
|
801
|
+
}
|
|
802
|
+
return false;
|
|
803
|
+
};
|
|
804
|
+
|
|
736
805
|
/**
|
|
737
806
|
* 追踪页面视图变化
|
|
738
807
|
*/
|
|
@@ -746,6 +815,12 @@ function routerPlugin(tracker) {
|
|
|
746
815
|
trackerCurrent: tracker.current
|
|
747
816
|
});
|
|
748
817
|
|
|
818
|
+
// 检查是否应该追踪此路由
|
|
819
|
+
if (!shouldTrack(currentPath)) {
|
|
820
|
+
console.log('[Tracker SDK] Route filtered out:', currentPath);
|
|
821
|
+
return;
|
|
822
|
+
}
|
|
823
|
+
|
|
749
824
|
// 避免重复追踪相同的路径
|
|
750
825
|
if (currentPath === lastPath) {
|
|
751
826
|
console.log('[Tracker SDK] Route unchanged:', currentPath, moduleMapper ? moduleMapper(currentPath) : currentPath);
|
|
@@ -778,13 +853,19 @@ function routerPlugin(tracker) {
|
|
|
778
853
|
// 1. 追踪初始页面加载(延迟执行,确保 DOM 和路由已就绪)
|
|
779
854
|
setTimeout(function () {
|
|
780
855
|
var initialPath = window.location.pathname + window.location.search + window.location.hash;
|
|
781
|
-
lastPath = initialPath; // 更新 lastPath 为实际初始路径
|
|
782
856
|
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
857
|
+
// 检查初始页面是否应该被追踪
|
|
858
|
+
if (shouldTrack(initialPath)) {
|
|
859
|
+
lastPath = initialPath; // 更新 lastPath 为实际初始路径
|
|
860
|
+
|
|
861
|
+
tracker.enter({
|
|
862
|
+
module: getModuleName(initialPath),
|
|
863
|
+
system: system
|
|
864
|
+
});
|
|
865
|
+
console.log('[Tracker SDK] Initial route tracked:', initialPath);
|
|
866
|
+
} else {
|
|
867
|
+
console.log('[Tracker SDK] Initial route filtered out:', initialPath);
|
|
868
|
+
}
|
|
788
869
|
}, 0);
|
|
789
870
|
|
|
790
871
|
// 2. 监听 Hash 模式路由变化
|