@yysports/tools 2.0.0 → 2.0.2

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.
@@ -12,3 +12,6 @@ export declare const DEFAULT_REPLAY_CHECKOUT_NMS: number;
12
12
  export declare const DEFAULT_REPLAY_MAX_EVENTS = 1000;
13
13
  export declare const DEFAULT_REPLAY_MAX_BYTES: number;
14
14
  export declare const DEFAULT_ERROR_THROTTLE_MS = 3000;
15
+ export declare const DEV_DOMAIN = "//track-t.yysports.com";
16
+ export declare const TEST_DOMAIN = "//track-t.yysports.com";
17
+ export declare const PROD_DOMAIN = "//track.yysports.com";
@@ -22,4 +22,13 @@ export var DEFAULT_REPLAY_MAX_EVENTS = 1000;
22
22
  export var DEFAULT_REPLAY_MAX_BYTES = 1024 * 1024; // 1MB
23
23
 
24
24
  // 错误节流默认窗口(毫秒)
25
- export var DEFAULT_ERROR_THROTTLE_MS = 3000;
25
+ export var DEFAULT_ERROR_THROTTLE_MS = 3000;
26
+
27
+ // 开发环境域名
28
+ export var DEV_DOMAIN = '//track-t.yysports.com';
29
+
30
+ // 测试环境域名
31
+ export var TEST_DOMAIN = '//track-t.yysports.com';
32
+
33
+ // 生产环境域名
34
+ export var PROD_DOMAIN = '//track.yysports.com';
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Barrel exports for Track core modules.
3
+ *
4
+ * Purpose:
5
+ * - Provide a single import surface for core primitives (TrackerInstance, queue,
6
+ * replay, transport, lifecycle, etc.).
7
+ * - Make future reorganization easier without touching external callers.
8
+ *
9
+ * Note: this file only re-exports existing modules and does not change runtime
10
+ * behavior. Existing import paths that reference specific files (e.g.
11
+ * './core/instance') will continue to work.
12
+ */
13
+ export { TrackerInstance } from './instance';
14
+ export { TrackQueue } from './queue';
15
+ export { ReplayManager } from './replay';
16
+ export { LifecycleManager } from './lifecycle';
17
+ export * from './transport';
18
+ export * from './auth';
19
+ export * from './envelope';
20
+ export * from './profile';
21
+ export * from './router';
22
+ export * from './constants';
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Barrel exports for Track core modules.
3
+ *
4
+ * Purpose:
5
+ * - Provide a single import surface for core primitives (TrackerInstance, queue,
6
+ * replay, transport, lifecycle, etc.).
7
+ * - Make future reorganization easier without touching external callers.
8
+ *
9
+ * Note: this file only re-exports existing modules and does not change runtime
10
+ * behavior. Existing import paths that reference specific files (e.g.
11
+ * './core/instance') will continue to work.
12
+ */
13
+
14
+ export { TrackerInstance } from "./instance";
15
+ export { TrackQueue } from "./queue";
16
+ export { ReplayManager } from "./replay";
17
+ export { LifecycleManager } from "./lifecycle";
18
+ export * from "./transport";
19
+ export * from "./auth";
20
+ export * from "./envelope";
21
+ export * from "./profile";
22
+ export * from "./router";
23
+ export * from "./constants";
@@ -70,8 +70,8 @@ export declare class TrackerInstance {
70
70
  zoneId: number;
71
71
  zoneName: string;
72
72
  }[]>) | undefined;
73
- domain: string;
74
73
  enableReplay?: boolean | undefined;
74
+ env: "test" | "dev" | "prod";
75
75
  };
76
76
  /** 配置校验:缺少关键字段直接抛错 */
77
77
  private validate;
@@ -7,7 +7,6 @@ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
7
7
  /**
8
8
  * TrackerInstance - 多实例核心组合:权限初始化 + 队列 + 采样 + 生命周期
9
9
  */
10
- // rrweb 录制管理
11
10
 
12
11
  import { initAuthCenterData } from "./auth";
13
12
  import { DEFAULT_ERROR_THROTTLE_MS, DEFAULT_FLUSH_THRESHOLD, DEFAULT_MAX_WAIT_MS } from "./constants";
@@ -133,6 +132,7 @@ export var TrackerInstance = /*#__PURE__*/function () {
133
132
  }, {
134
133
  key: "eventLog",
135
134
  value: function eventLog(_eventLog) {
135
+ if (!this.internal.initialized) return;
136
136
  var data = {
137
137
  module: '',
138
138
  content: ''
@@ -156,6 +156,7 @@ export var TrackerInstance = /*#__PURE__*/function () {
156
156
  }, {
157
157
  key: "visitLog",
158
158
  value: function visitLog(_visitLog) {
159
+ if (!this.internal.initialized) return;
159
160
  var routeInfo = this.routesMap.get(this.getFormatPath());
160
161
  if (routeInfo !== null && routeInfo !== void 0 && routeInfo.isRedirect) return;
161
162
  var mods = this.getFilteredModules();
@@ -183,15 +184,21 @@ export var TrackerInstance = /*#__PURE__*/function () {
183
184
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
184
185
  while (1) switch (_context2.prev = _context2.next) {
185
186
  case 0:
187
+ if (this.internal.initialized) {
188
+ _context2.next = 2;
189
+ break;
190
+ }
191
+ return _context2.abrupt("return");
192
+ case 2:
186
193
  // 简单节流:同一窗口只发送首条
187
194
  throttle = DEFAULT_ERROR_THROTTLE_MS;
188
195
  now = Date.now();
189
196
  if (!(this.internal.lastErrorSentAt && now - this.internal.lastErrorSentAt < throttle)) {
190
- _context2.next = 4;
197
+ _context2.next = 6;
191
198
  break;
192
199
  }
193
200
  return _context2.abrupt("return");
194
- case 4:
201
+ case 6:
195
202
  // 先把报错信息写入回放流,便于回放中定位
196
203
  (_this$replay = this.replay) === null || _this$replay === void 0 || _this$replay.markError(error);
197
204
 
@@ -202,7 +209,7 @@ export var TrackerInstance = /*#__PURE__*/function () {
202
209
 
203
210
  // 上报完成后再更新节流时间戳
204
211
  this.internal.lastErrorSentAt = now;
205
- case 9:
212
+ case 11:
206
213
  case "end":
207
214
  return _context2.stop();
208
215
  }
@@ -220,6 +227,7 @@ export var TrackerInstance = /*#__PURE__*/function () {
220
227
  }, {
221
228
  key: "apiLog",
222
229
  value: function apiLog(_apiLog) {
230
+ if (!this.internal.initialized) return;
223
231
  var env = buildEnvelope(this.config, this.userProfile, 'api', _apiLog);
224
232
  reportApiLog(this.config, _apiLog, env);
225
233
  }
@@ -244,7 +252,7 @@ export var TrackerInstance = /*#__PURE__*/function () {
244
252
  }, {
245
253
  key: "validate",
246
254
  value: function validate() {
247
- if (!this.config.domain) throw new Error('缺少 domain');
255
+ if (!this.config.env) throw new Error('缺少 env');
248
256
  if (!this.config.appId) throw new Error('缺少 appId');
249
257
  }
250
258
 
@@ -303,6 +311,8 @@ export var TrackerInstance = /*#__PURE__*/function () {
303
311
  this.lifecycle.mount();
304
312
  }
305
313
 
314
+ // history 监听功能已移除
315
+
306
316
  /** 启动/重置周期性 flush 计时器 */
307
317
  }, {
308
318
  key: "scheduleFlushTimer",
@@ -5,7 +5,21 @@ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
5
5
  * 传输层:统一封装 sendBeacon 调用与批量 / 版本上报逻辑。
6
6
  */
7
7
  import { version } from "../../../package.json";
8
- import { APP_VERSION_ENDPOINT, ERROR_REPLAY_ENDPOINT, ERROR_TRACK_ENDPOINT, TRACK_ENDPOINT } from "./constants";
8
+ import { APP_VERSION_ENDPOINT, DEV_DOMAIN, ERROR_REPLAY_ENDPOINT, ERROR_TRACK_ENDPOINT, PROD_DOMAIN, TEST_DOMAIN, TRACK_ENDPOINT } from "./constants";
9
+
10
+ /** 根据 TrackConfig.env 解析要使用的域名(兼容向后默认 test) */
11
+ function resolveDomain(config) {
12
+ var env = config && config.env || 'test';
13
+ switch (env) {
14
+ case 'dev':
15
+ return DEV_DOMAIN;
16
+ case 'prod':
17
+ return PROD_DOMAIN;
18
+ case 'test':
19
+ default:
20
+ return TEST_DOMAIN;
21
+ }
22
+ }
9
23
 
10
24
  /**
11
25
  * 包装fetch,方便async/await调用
@@ -66,8 +80,8 @@ export function beaconSend(url, payload) {
66
80
  * 上报当前应用版本:避免重复上报,通过 localStorage 绑定 appId 做缓存
67
81
  */
68
82
  export function reportAppVersion(config) {
69
- var appId = config.appId,
70
- domain = config.domain;
83
+ var appId = config.appId;
84
+ var domain = resolveDomain(config);
71
85
  var storageKey = "__Track__".concat(appId);
72
86
  try {
73
87
  var cached = localStorage.getItem(storageKey);
@@ -99,13 +113,13 @@ export function reportAppVersion(config) {
99
113
  /** 批量上报日志事件数组 */
100
114
  export function reportBatch(config, batch) {
101
115
  if (!batch.length) return;
102
- var domain = config.domain;
116
+ var domain = resolveDomain(config);
103
117
  beaconSend("".concat(domain).concat(TRACK_ENDPOINT), batch);
104
118
  }
105
119
 
106
120
  /** 上报单条 API 日志(独立通道) */
107
121
  export function reportApiLog(config, apiLog, envelope) {
108
- var domain = config.domain;
122
+ var domain = resolveDomain(config);
109
123
  beaconSend("".concat(domain).concat(TRACK_ENDPOINT), [envelope]);
110
124
  }
111
125
 
@@ -119,7 +133,7 @@ function _reportError() {
119
133
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
120
134
  while (1) switch (_context2.prev = _context2.next) {
121
135
  case 0:
122
- domain = config.domain;
136
+ domain = resolveDomain(config);
123
137
  _context2.next = 3;
124
138
  return fetchWrapper("".concat(domain).concat(ERROR_TRACK_ENDPOINT), {
125
139
  method: 'POST',
@@ -1,32 +1,17 @@
1
1
  /**
2
- * 新版多实例埋点入口
3
- * 提供统一 createTracker(config, { lazy }) API
2
+ * 提供统一 Track.init(config) 及若干日志方法,未初始化即抛错
4
3
  */
5
- import { TrackConfig } from '../interfaces/track.interface';
6
- import { TrackerInstance } from './core/instance';
4
+ import { AuthCenterUserProfile, TrackConfig } from '../interfaces/track.interface';
5
+ import { TrackerInstance } from './core';
7
6
  export type { TrackerInstance };
8
- interface CreateTrackerOptions {
9
- lazy?: boolean;
10
- }
11
- /**
12
- * 创建埋点实例
13
- * @param config TrackConfig
14
- * @param options { lazy } 是否延迟 init(默认 false)
15
- * - lazy=false: 返回 Promise<TrackerInstance>(已完成 init)
16
- * - lazy=true: 返回未初始化实例 (TrackerInstance),需外部手动调用 init()
17
- */
18
- export declare function createTracker(config: TrackConfig, options?: CreateTrackerOptions): Promise<TrackerInstance> | TrackerInstance;
19
- declare const _default: {
20
- init: (config: TrackConfig) => Promise<TrackerInstance>;
21
- isReady: () => boolean;
22
- getInstance: () => TrackerInstance;
7
+ type TrackAPI = {
8
+ init: (config: TrackConfig) => Promise<AuthCenterUserProfile>;
23
9
  eventLog: TrackerInstance['eventLog'];
24
10
  visitLog: TrackerInstance['visitLog'];
25
- errorLog: TrackerInstance['errorLog'];
26
- apiLog: TrackerInstance['apiLog'];
27
- flush: () => void;
28
- destroy: () => void;
29
11
  setUserProfile: (profile: Parameters<TrackerInstance['setUserProfile']>[0]) => void;
30
- getConfig: () => TrackConfig | undefined;
12
+ config: TrackConfig;
13
+ };
14
+ declare const Track: TrackAPI & {
15
+ getInstance?: (() => TrackerInstance) | undefined;
31
16
  };
32
- export default _default;
17
+ export default Track;
@@ -1,253 +1,68 @@
1
- import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
- import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
- import _createClass from "@babel/runtime/helpers/esm/createClass";
4
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
5
1
  import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
6
2
  import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
7
3
  /**
8
- * 新版多实例埋点入口
9
- * 提供统一 createTracker(config, { lazy }) API
4
+ * 提供统一 Track.init(config) 及若干日志方法,未初始化即抛错
10
5
  */
11
6
 
12
- import { TrackerInstance } from "./core/instance";
13
-
14
- // ---------------- 多实例工厂 ----------------
15
-
16
- /**
17
- * 创建埋点实例
18
- * @param config TrackConfig
19
- * @param options { lazy } 是否延迟 init(默认 false)
20
- * - lazy=false: 返回 Promise<TrackerInstance>(已完成 init)
21
- * - lazy=true: 返回未初始化实例 (TrackerInstance),需外部手动调用 init()
22
- */
23
- export function createTracker(config, options) {
24
- var _ref = options || {},
25
- lazy = _ref.lazy;
26
- var ins = new TrackerInstance(config);
27
- if (lazy) return ins;
28
- return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
7
+ import { TrackerInstance } from "./core";
8
+ // 模块级单例,简洁实现
9
+ var _instance = null;
10
+ var _profile = null;
11
+ function init(_x) {
12
+ return _init.apply(this, arguments);
13
+ }
14
+ function _init() {
15
+ _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
16
+ var ins, profile;
29
17
  return _regeneratorRuntime().wrap(function _callee$(_context) {
30
18
  while (1) switch (_context.prev = _context.next) {
31
19
  case 0:
32
- _context.next = 2;
33
- return ins.init();
20
+ if (!_instance) {
21
+ _context.next = 2;
22
+ break;
23
+ }
24
+ return _context.abrupt("return", Promise.resolve(_profile));
34
25
  case 2:
35
- return _context.abrupt("return", ins);
36
- case 3:
26
+ ins = new TrackerInstance(config);
27
+ _context.next = 5;
28
+ return ins.init();
29
+ case 5:
30
+ profile = _context.sent;
31
+ _instance = ins;
32
+ _profile = profile;
33
+ return _context.abrupt("return", profile);
34
+ case 9:
37
35
  case "end":
38
36
  return _context.stop();
39
37
  }
40
38
  }, _callee);
41
- }))();
39
+ }));
40
+ return _init.apply(this, arguments);
41
+ }
42
+ function ensure() {
43
+ if (!_instance) throw new Error('Track 未初始化,请先调用 Track.init(config)');
44
+ return _instance;
42
45
  }
43
46
 
44
- // ---------------- 静态外观(兼容方便使用) ----------------
45
- var TrackFacade = /*#__PURE__*/function () {
46
- function TrackFacade() {
47
- _classCallCheck(this, TrackFacade);
48
- }
49
- _createClass(TrackFacade, null, [{
50
- key: "init",
51
- value: ( /** 初始化(必须先调用一次)。重复调用直接返回已存在实例 */function () {
52
- var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(config) {
53
- var _this = this;
54
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
55
- while (1) switch (_context3.prev = _context3.next) {
56
- case 0:
57
- if (!this._instance) {
58
- _context3.next = 2;
59
- break;
60
- }
61
- return _context3.abrupt("return", this._instance);
62
- case 2:
63
- if (!this._initPromise) {
64
- this._initPromise = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
65
- var ins;
66
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
67
- while (1) switch (_context2.prev = _context2.next) {
68
- case 0:
69
- ins = new TrackerInstance(config);
70
- _context2.next = 3;
71
- return ins.init();
72
- case 3:
73
- _this._instance = ins;
74
- // 回放预队列
75
- _this._queue.forEach(function (_ref4) {
76
- var _ref5;
77
- var method = _ref4.method,
78
- args = _ref4.args;
79
- return (_ref5 = ins)[method].apply(_ref5, _toConsumableArray(args));
80
- });
81
- _this._queue.length = 0;
82
- return _context2.abrupt("return", ins);
83
- case 7:
84
- case "end":
85
- return _context2.stop();
86
- }
87
- }, _callee2);
88
- }))();
89
- }
90
- return _context3.abrupt("return", this._initPromise);
91
- case 4:
92
- case "end":
93
- return _context3.stop();
94
- }
95
- }, _callee3, this);
96
- }));
97
- function init(_x) {
98
- return _init.apply(this, arguments);
99
- }
100
- return init;
101
- }() /** 是否已就绪 */)
102
- }, {
103
- key: "isReady",
104
- value: function isReady() {
105
- return !!this._instance;
106
- }
107
-
108
- /** 获取内部实例(未初始化时抛错) */
109
- }, {
110
- key: "getInstance",
111
- value: function getInstance() {
112
- if (!this._instance) throw new Error('Track 未初始化,请先调用 Track.init(config)');
113
- return this._instance;
114
- }
115
-
116
- /** 延迟调用帮助:未 init 时先入队 */
117
- }, {
118
- key: "invoke",
119
- value: function invoke(method) {
120
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
121
- args[_key - 1] = arguments[_key];
122
- }
123
- if (this._instance) {
124
- var _ref6;
125
- (_ref6 = this._instance)[method].apply(_ref6, args);
126
- } else if (this._initPromise) {
127
- // 初始化进行中,进入队列
128
- this._queue.push({
129
- method: method,
130
- args: args
131
- });
132
- } else {
133
- // 尚未开始 init,仍入队,提示一次
134
- this._queue.push({
135
- method: method,
136
- args: args
137
- });
138
- if (!this._warned) {
139
- console.warn('[track] 在 Track.init 前调用了日志方法,已暂存至初始化后回放');
140
- this._warned = true;
141
- }
142
- }
143
- }
47
+ // 更安全的导出:显式 API 形状 + 绑定 this
144
48
 
145
- /**
146
- * 发送事件日志(通用事件)
147
- * - 若未初始化:自动排队等待 init 完成后回放
148
- */
149
- }, {
150
- key: "eventLog",
151
- value: function eventLog() {
152
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
153
- args[_key2] = arguments[_key2];
154
- }
155
- this.invoke.apply(this, ['eventLog'].concat(args));
156
- }
157
- /**
158
- * 发送访问/页面曝光日志
159
- * - 用于记录页面 / 场景进入等行为
160
- */
161
- }, {
162
- key: "visitLog",
163
- value: function visitLog() {
164
- for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
165
- args[_key3] = arguments[_key3];
166
- }
167
- this.invoke.apply(this, ['visitLog'].concat(args));
168
- }
169
- /**
170
- * 发送错误日志
171
- * - 适用于运行时异常、资源加载失败、接口异常包装等
172
- */
173
- }, {
174
- key: "errorLog",
175
- value: function errorLog() {
176
- for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
177
- args[_key4] = arguments[_key4];
49
+ // 懒代理:除 init 外,所有方法直接代理到内部单例实例(调用时会 ensure)
50
+ var Track = new Proxy({}, {
51
+ get: function get(_, prop) {
52
+ if (prop === 'init') return init;
53
+ if (prop === 'config') return ensure().getConfig();
54
+ // 返回一个函数,调用时转发到实例对应方法
55
+ return function () {
56
+ var inst = ensure();
57
+ var fn = inst[prop];
58
+ if (typeof fn !== 'function') {
59
+ throw new Error("Track: no method named \"".concat(String(prop), "\" on instance"));
178
60
  }
179
- this.invoke.apply(this, ['errorLog'].concat(args));
180
- }
181
- /**
182
- * 发送接口调用相关日志
183
- * - 记录接口耗时 / 结果状态
184
- */
185
- }, {
186
- key: "apiLog",
187
- value: function apiLog() {
188
- for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
189
- args[_key5] = arguments[_key5];
61
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
62
+ args[_key] = arguments[_key];
190
63
  }
191
- this.invoke.apply(this, ['apiLog'].concat(args));
192
- }
193
- /**
194
- * 强制立即 flush 已缓冲的日志(若实例策略存在缓冲)
195
- */
196
- }, {
197
- key: "flush",
198
- value: function flush() {
199
- if (!this._instance) return;
200
- this._instance.flush();
201
- }
202
- /**
203
- * 销毁当前单例实例
204
- * - 停止上报 / 释放资源
205
- * - 再次使用需重新 init
206
- */
207
- }, {
208
- key: "destroy",
209
- value: function destroy() {
210
- if (!this._instance) return;
211
- this._instance.destroy();
212
- this._instance = null;
213
- this._initPromise = null;
214
- }
215
- /**
216
- * 设置用户画像 / 用户基础属性
217
- * - 若在 init 前调用:会延迟到实例 ready 后再真正执行
218
- */
219
- }, {
220
- key: "setUserProfile",
221
- value: function setUserProfile(profile) {
222
- if (!this._instance) {
223
- // 允许提前设置,将其转换为 eventLog 之前的一个补丁:加入队列尾部一个函数式调用
224
- this._queue.push({
225
- method: 'eventLog',
226
- args: []
227
- }); // 占位避免丢失顺序
228
- // 等 init 后再真正 set(用回放后 hook)
229
- var prevInit = this._initPromise;
230
- if (prevInit) {
231
- prevInit.then(function (ins) {
232
- return ins.setUserProfile(profile);
233
- });
234
- }
235
- return;
236
- }
237
- this._instance.setUserProfile(profile);
238
- }
239
- /** 获取当前配置(未初始化时返回 undefined) */
240
- }, {
241
- key: "getConfig",
242
- value: function getConfig() {
243
- var _this$_instance;
244
- return (_this$_instance = this._instance) === null || _this$_instance === void 0 ? void 0 : _this$_instance.getConfig();
245
- }
246
- }]);
247
- return TrackFacade;
248
- }(); // 默认导出静态外观,满足 import Track 后直接 Track.eventLog()
249
- _defineProperty(TrackFacade, "_instance", null);
250
- _defineProperty(TrackFacade, "_initPromise", null);
251
- _defineProperty(TrackFacade, "_queue", []);
252
- _defineProperty(TrackFacade, "_warned", false);
253
- export default TrackFacade;
64
+ return fn.apply(inst, args);
65
+ };
66
+ }
67
+ });
68
+ export default Track;
@@ -31,8 +31,8 @@ export interface TrackConfig {
31
31
  zoneId: number;
32
32
  zoneName: string;
33
33
  }[]>;
34
- domain: string;
35
34
  enableReplay?: boolean;
35
+ env: 'dev' | 'test' | 'prod';
36
36
  }
37
37
  export declare enum TrackTypeEnum {
38
38
  error = "\u9519\u8BEF\u4FE1\u606F",
@@ -12,3 +12,6 @@ export declare const DEFAULT_REPLAY_CHECKOUT_NMS: number;
12
12
  export declare const DEFAULT_REPLAY_MAX_EVENTS = 1000;
13
13
  export declare const DEFAULT_REPLAY_MAX_BYTES: number;
14
14
  export declare const DEFAULT_ERROR_THROTTLE_MS = 3000;
15
+ export declare const DEV_DOMAIN = "//track-t.yysports.com";
16
+ export declare const TEST_DOMAIN = "//track-t.yysports.com";
17
+ export declare const PROD_DOMAIN = "//track.yysports.com";
@@ -26,8 +26,11 @@ __export(constants_exports, {
26
26
  DEFAULT_REPLAY_CHECKOUT_NMS: () => DEFAULT_REPLAY_CHECKOUT_NMS,
27
27
  DEFAULT_REPLAY_MAX_BYTES: () => DEFAULT_REPLAY_MAX_BYTES,
28
28
  DEFAULT_REPLAY_MAX_EVENTS: () => DEFAULT_REPLAY_MAX_EVENTS,
29
+ DEV_DOMAIN: () => DEV_DOMAIN,
29
30
  ERROR_REPLAY_ENDPOINT: () => ERROR_REPLAY_ENDPOINT,
30
31
  ERROR_TRACK_ENDPOINT: () => ERROR_TRACK_ENDPOINT,
32
+ PROD_DOMAIN: () => PROD_DOMAIN,
33
+ TEST_DOMAIN: () => TEST_DOMAIN,
31
34
  TRACK_ENDPOINT: () => TRACK_ENDPOINT
32
35
  });
33
36
  module.exports = __toCommonJS(constants_exports);
@@ -41,6 +44,9 @@ var DEFAULT_REPLAY_CHECKOUT_NMS = 5 * 60 * 1e3;
41
44
  var DEFAULT_REPLAY_MAX_EVENTS = 1e3;
42
45
  var DEFAULT_REPLAY_MAX_BYTES = 1024 * 1024;
43
46
  var DEFAULT_ERROR_THROTTLE_MS = 3e3;
47
+ var DEV_DOMAIN = "//track-t.yysports.com";
48
+ var TEST_DOMAIN = "//track-t.yysports.com";
49
+ var PROD_DOMAIN = "//track.yysports.com";
44
50
  // Annotate the CommonJS export names for ESM import in node:
45
51
  0 && (module.exports = {
46
52
  APP_VERSION_ENDPOINT,
@@ -50,7 +56,10 @@ var DEFAULT_ERROR_THROTTLE_MS = 3e3;
50
56
  DEFAULT_REPLAY_CHECKOUT_NMS,
51
57
  DEFAULT_REPLAY_MAX_BYTES,
52
58
  DEFAULT_REPLAY_MAX_EVENTS,
59
+ DEV_DOMAIN,
53
60
  ERROR_REPLAY_ENDPOINT,
54
61
  ERROR_TRACK_ENDPOINT,
62
+ PROD_DOMAIN,
63
+ TEST_DOMAIN,
55
64
  TRACK_ENDPOINT
56
65
  });
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Barrel exports for Track core modules.
3
+ *
4
+ * Purpose:
5
+ * - Provide a single import surface for core primitives (TrackerInstance, queue,
6
+ * replay, transport, lifecycle, etc.).
7
+ * - Make future reorganization easier without touching external callers.
8
+ *
9
+ * Note: this file only re-exports existing modules and does not change runtime
10
+ * behavior. Existing import paths that reference specific files (e.g.
11
+ * './core/instance') will continue to work.
12
+ */
13
+ export { TrackerInstance } from './instance';
14
+ export { TrackQueue } from './queue';
15
+ export { ReplayManager } from './replay';
16
+ export { LifecycleManager } from './lifecycle';
17
+ export * from './transport';
18
+ export * from './auth';
19
+ export * from './envelope';
20
+ export * from './profile';
21
+ export * from './router';
22
+ export * from './constants';
@@ -0,0 +1,51 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/Track/core/index.ts
21
+ var core_exports = {};
22
+ __export(core_exports, {
23
+ LifecycleManager: () => import_lifecycle.LifecycleManager,
24
+ ReplayManager: () => import_replay.ReplayManager,
25
+ TrackQueue: () => import_queue.TrackQueue,
26
+ TrackerInstance: () => import_instance.TrackerInstance
27
+ });
28
+ module.exports = __toCommonJS(core_exports);
29
+ var import_instance = require("./instance");
30
+ var import_queue = require("./queue");
31
+ var import_replay = require("./replay");
32
+ var import_lifecycle = require("./lifecycle");
33
+ __reExport(core_exports, require("./transport"), module.exports);
34
+ __reExport(core_exports, require("./auth"), module.exports);
35
+ __reExport(core_exports, require("./envelope"), module.exports);
36
+ __reExport(core_exports, require("./profile"), module.exports);
37
+ __reExport(core_exports, require("./router"), module.exports);
38
+ __reExport(core_exports, require("./constants"), module.exports);
39
+ // Annotate the CommonJS export names for ESM import in node:
40
+ 0 && (module.exports = {
41
+ LifecycleManager,
42
+ ReplayManager,
43
+ TrackQueue,
44
+ TrackerInstance,
45
+ ...require("./transport"),
46
+ ...require("./auth"),
47
+ ...require("./envelope"),
48
+ ...require("./profile"),
49
+ ...require("./router"),
50
+ ...require("./constants")
51
+ });
@@ -70,8 +70,8 @@ export declare class TrackerInstance {
70
70
  zoneId: number;
71
71
  zoneName: string;
72
72
  }[]>) | undefined;
73
- domain: string;
74
73
  enableReplay?: boolean | undefined;
74
+ env: "test" | "dev" | "prod";
75
75
  };
76
76
  /** 配置校验:缺少关键字段直接抛错 */
77
77
  private validate;
@@ -100,6 +100,8 @@ var TrackerInstance = class {
100
100
  * - 自动补全 module(基于当前路由)
101
101
  */
102
102
  eventLog(eventLog) {
103
+ if (!this.internal.initialized)
104
+ return;
103
105
  const data = { module: "", content: "" };
104
106
  if (typeof eventLog === "string") {
105
107
  const action = this.authMap.get(eventLog);
@@ -117,6 +119,8 @@ var TrackerInstance = class {
117
119
  * - 若路由标记 isRedirect 则跳过(避免中转页统计)
118
120
  */
119
121
  visitLog(visitLog) {
122
+ if (!this.internal.initialized)
123
+ return;
120
124
  const routeInfo = this.routesMap.get(this.getFormatPath());
121
125
  if (routeInfo == null ? void 0 : routeInfo.isRedirect)
122
126
  return;
@@ -138,6 +142,8 @@ var TrackerInstance = class {
138
142
  */
139
143
  async errorLog(error) {
140
144
  var _a;
145
+ if (!this.internal.initialized)
146
+ return;
141
147
  const throttle = import_constants.DEFAULT_ERROR_THROTTLE_MS;
142
148
  const now = Date.now();
143
149
  if (this.internal.lastErrorSentAt && now - this.internal.lastErrorSentAt < throttle) {
@@ -153,6 +159,8 @@ var TrackerInstance = class {
153
159
  * API 日志:直接即时上报(不进入公共队列)
154
160
  */
155
161
  apiLog(apiLog) {
162
+ if (!this.internal.initialized)
163
+ return;
156
164
  const env = (0, import_envelope.buildEnvelope)(this.config, this.userProfile, "api", apiLog);
157
165
  (0, import_transport.reportApiLog)(this.config, apiLog, env);
158
166
  }
@@ -168,8 +176,8 @@ var TrackerInstance = class {
168
176
  }
169
177
  /** 配置校验:缺少关键字段直接抛错 */
170
178
  validate() {
171
- if (!this.config.domain)
172
- throw new Error("缺少 domain");
179
+ if (!this.config.env)
180
+ throw new Error("缺少 env");
173
181
  if (!this.config.appId)
174
182
  throw new Error("缺少 appId");
175
183
  }
@@ -212,6 +220,7 @@ var TrackerInstance = class {
212
220
  });
213
221
  this.lifecycle.mount();
214
222
  }
223
+ // history 监听功能已移除
215
224
  /** 启动/重置周期性 flush 计时器 */
216
225
  scheduleFlushTimer() {
217
226
  this.internal.flushTimer && clearInterval(this.internal.flushTimer);
@@ -28,6 +28,18 @@ __export(transport_exports, {
28
28
  module.exports = __toCommonJS(transport_exports);
29
29
  var import_package = require("../../../package.json");
30
30
  var import_constants = require("./constants");
31
+ function resolveDomain(config) {
32
+ const env = config && config.env || "test";
33
+ switch (env) {
34
+ case "dev":
35
+ return import_constants.DEV_DOMAIN;
36
+ case "prod":
37
+ return import_constants.PROD_DOMAIN;
38
+ case "test":
39
+ default:
40
+ return import_constants.TEST_DOMAIN;
41
+ }
42
+ }
31
43
  async function fetchWrapper(url, options) {
32
44
  return new Promise((resolve, reject) => {
33
45
  fetch(url, options).then((response) => {
@@ -54,7 +66,8 @@ function beaconSend(url, payload) {
54
66
  }
55
67
  }
56
68
  function reportAppVersion(config) {
57
- const { appId, domain } = config;
69
+ const { appId } = config;
70
+ const domain = resolveDomain(config);
58
71
  const storageKey = `__Track__${appId}`;
59
72
  try {
60
73
  const cached = localStorage.getItem(storageKey);
@@ -79,15 +92,15 @@ function reportAppVersion(config) {
79
92
  function reportBatch(config, batch) {
80
93
  if (!batch.length)
81
94
  return;
82
- const { domain } = config;
95
+ const domain = resolveDomain(config);
83
96
  beaconSend(`${domain}${import_constants.TRACK_ENDPOINT}`, batch);
84
97
  }
85
98
  function reportApiLog(config, apiLog, envelope) {
86
- const { domain } = config;
99
+ const domain = resolveDomain(config);
87
100
  beaconSend(`${domain}${import_constants.TRACK_ENDPOINT}`, [envelope]);
88
101
  }
89
102
  async function reportError(config, envelope, replayPayload) {
90
- const { domain } = config;
103
+ const domain = resolveDomain(config);
91
104
  const { data: id } = await fetchWrapper(`${domain}${import_constants.ERROR_TRACK_ENDPOINT}`, {
92
105
  method: "POST",
93
106
  headers: {
@@ -1,32 +1,17 @@
1
1
  /**
2
- * 新版多实例埋点入口
3
- * 提供统一 createTracker(config, { lazy }) API
2
+ * 提供统一 Track.init(config) 及若干日志方法,未初始化即抛错
4
3
  */
5
- import { TrackConfig } from '../interfaces/track.interface';
6
- import { TrackerInstance } from './core/instance';
4
+ import { AuthCenterUserProfile, TrackConfig } from '../interfaces/track.interface';
5
+ import { TrackerInstance } from './core';
7
6
  export type { TrackerInstance };
8
- interface CreateTrackerOptions {
9
- lazy?: boolean;
10
- }
11
- /**
12
- * 创建埋点实例
13
- * @param config TrackConfig
14
- * @param options { lazy } 是否延迟 init(默认 false)
15
- * - lazy=false: 返回 Promise<TrackerInstance>(已完成 init)
16
- * - lazy=true: 返回未初始化实例 (TrackerInstance),需外部手动调用 init()
17
- */
18
- export declare function createTracker(config: TrackConfig, options?: CreateTrackerOptions): Promise<TrackerInstance> | TrackerInstance;
19
- declare const _default: {
20
- init: (config: TrackConfig) => Promise<TrackerInstance>;
21
- isReady: () => boolean;
22
- getInstance: () => TrackerInstance;
7
+ type TrackAPI = {
8
+ init: (config: TrackConfig) => Promise<AuthCenterUserProfile>;
23
9
  eventLog: TrackerInstance['eventLog'];
24
10
  visitLog: TrackerInstance['visitLog'];
25
- errorLog: TrackerInstance['errorLog'];
26
- apiLog: TrackerInstance['apiLog'];
27
- flush: () => void;
28
- destroy: () => void;
29
11
  setUserProfile: (profile: Parameters<TrackerInstance['setUserProfile']>[0]) => void;
30
- getConfig: () => TrackConfig | undefined;
12
+ config: TrackConfig;
13
+ };
14
+ declare const Track: TrackAPI & {
15
+ getInstance?: (() => TrackerInstance) | undefined;
31
16
  };
32
- export default _default;
17
+ export default Track;
@@ -19,141 +19,45 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  // src/Track/index.ts
20
20
  var Track_exports = {};
21
21
  __export(Track_exports, {
22
- createTracker: () => createTracker,
23
22
  default: () => Track_default
24
23
  });
25
24
  module.exports = __toCommonJS(Track_exports);
26
- var import_instance = require("./core/instance");
27
- function createTracker(config, options) {
28
- const { lazy } = options || {};
29
- const ins = new import_instance.TrackerInstance(config);
30
- if (lazy)
31
- return ins;
32
- return (async () => {
33
- await ins.init();
34
- return ins;
35
- })();
25
+ var import_core = require("./core");
26
+ var _instance = null;
27
+ var _profile = null;
28
+ async function init(config) {
29
+ if (_instance)
30
+ return Promise.resolve(_profile);
31
+ const ins = new import_core.TrackerInstance(config);
32
+ const profile = await ins.init();
33
+ _instance = ins;
34
+ _profile = profile;
35
+ return profile;
36
36
  }
37
- var TrackFacade = class {
38
- /** 初始化(必须先调用一次)。重复调用直接返回已存在实例 */
39
- static async init(config) {
40
- if (this._instance)
41
- return this._instance;
42
- if (!this._initPromise) {
43
- this._initPromise = (async () => {
44
- const ins = new import_instance.TrackerInstance(config);
45
- await ins.init();
46
- this._instance = ins;
47
- this._queue.forEach(
48
- ({ method, args }) => ins[method](...args)
49
- );
50
- this._queue.length = 0;
51
- return ins;
52
- })();
53
- }
54
- return this._initPromise;
55
- }
56
- /** 是否已就绪 */
57
- static isReady() {
58
- return !!this._instance;
59
- }
60
- /** 获取内部实例(未初始化时抛错) */
61
- static getInstance() {
62
- if (!this._instance)
63
- throw new Error("Track 未初始化,请先调用 Track.init(config)");
64
- return this._instance;
65
- }
66
- /** 延迟调用帮助:未 init 时先入队 */
67
- static invoke(method, ...args) {
68
- if (this._instance) {
69
- this._instance[method](...args);
70
- } else if (this._initPromise) {
71
- this._queue.push({ method, args });
72
- } else {
73
- this._queue.push({ method, args });
74
- if (!this._warned) {
75
- console.warn(
76
- "[track] 在 Track.init 前调用了日志方法,已暂存至初始化后回放"
77
- );
78
- this._warned = true;
79
- }
80
- }
81
- }
82
- /**
83
- * 发送事件日志(通用事件)
84
- * - 若未初始化:自动排队等待 init 完成后回放
85
- */
86
- static eventLog(...args) {
87
- this.invoke("eventLog", ...args);
88
- }
89
- /**
90
- * 发送访问/页面曝光日志
91
- * - 用于记录页面 / 场景进入等行为
92
- */
93
- static visitLog(...args) {
94
- this.invoke("visitLog", ...args);
95
- }
96
- /**
97
- * 发送错误日志
98
- * - 适用于运行时异常、资源加载失败、接口异常包装等
99
- */
100
- static errorLog(...args) {
101
- this.invoke("errorLog", ...args);
102
- }
103
- /**
104
- * 发送接口调用相关日志
105
- * - 记录接口耗时 / 结果状态
106
- */
107
- static apiLog(...args) {
108
- this.invoke("apiLog", ...args);
109
- }
110
- /**
111
- * 强制立即 flush 已缓冲的日志(若实例策略存在缓冲)
112
- */
113
- static flush() {
114
- if (!this._instance)
115
- return;
116
- this._instance.flush();
117
- }
118
- /**
119
- * 销毁当前单例实例
120
- * - 停止上报 / 释放资源
121
- * - 再次使用需重新 init
122
- */
123
- static destroy() {
124
- if (!this._instance)
125
- return;
126
- this._instance.destroy();
127
- this._instance = null;
128
- this._initPromise = null;
129
- }
130
- /**
131
- * 设置用户画像 / 用户基础属性
132
- * - 若在 init 前调用:会延迟到实例 ready 后再真正执行
133
- */
134
- static setUserProfile(profile) {
135
- if (!this._instance) {
136
- this._queue.push({ method: "eventLog", args: [] });
137
- const prevInit = this._initPromise;
138
- if (prevInit) {
139
- prevInit.then((ins) => ins.setUserProfile(profile));
140
- }
141
- return;
37
+ function ensure() {
38
+ if (!_instance)
39
+ throw new Error("Track 未初始化,请先调用 Track.init(config)");
40
+ return _instance;
41
+ }
42
+ var Track = new Proxy(
43
+ {},
44
+ {
45
+ get(_, prop) {
46
+ if (prop === "init")
47
+ return init;
48
+ if (prop === "config")
49
+ return ensure().getConfig();
50
+ return (...args) => {
51
+ const inst = ensure();
52
+ const fn = inst[prop];
53
+ if (typeof fn !== "function") {
54
+ throw new Error(
55
+ `Track: no method named "${String(prop)}" on instance`
56
+ );
57
+ }
58
+ return fn.apply(inst, args);
59
+ };
142
60
  }
143
- this._instance.setUserProfile(profile);
144
- }
145
- /** 获取当前配置(未初始化时返回 undefined) */
146
- static getConfig() {
147
- var _a;
148
- return (_a = this._instance) == null ? void 0 : _a.getConfig();
149
61
  }
150
- };
151
- TrackFacade._instance = null;
152
- TrackFacade._initPromise = null;
153
- TrackFacade._queue = [];
154
- TrackFacade._warned = false;
155
- var Track_default = TrackFacade;
156
- // Annotate the CommonJS export names for ESM import in node:
157
- 0 && (module.exports = {
158
- createTracker
159
- });
62
+ );
63
+ var Track_default = Track;
@@ -31,8 +31,8 @@ export interface TrackConfig {
31
31
  zoneId: number;
32
32
  zoneName: string;
33
33
  }[]>;
34
- domain: string;
35
34
  enableReplay?: boolean;
35
+ env: 'dev' | 'test' | 'prod';
36
36
  }
37
37
  export declare enum TrackTypeEnum {
38
38
  error = "\u9519\u8BEF\u4FE1\u606F",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yysports/tools",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "常用工具函数库",
5
5
  "homepage": "https://git.pousheng.com/sport-service-department/pousheng-guard/yysports_tools#readme",
6
6
  "bugs": {
@@ -69,13 +69,13 @@
69
69
  "@ant-design/pro-components": "^2.8.10",
70
70
  "@commitlint/cli": "^19.8.1",
71
71
  "@commitlint/config-conventional": "^19.8.1",
72
- "@types/react": "^18.3.23",
72
+ "@types/react": "^18.3.24",
73
73
  "@types/react-dom": "^18.3.7",
74
74
  "@umijs/lint": "^4.4.12",
75
- "antd": "^5.27.0",
75
+ "antd": "^5.27.1",
76
76
  "dumi": "^2.4.21",
77
- "eslint": "^9.33.0",
78
- "father": "^4.6.2",
77
+ "eslint": "^9.34.0",
78
+ "father": "^4.6.3",
79
79
  "husky": "^9.1.7",
80
80
  "lint-staged": "^16.1.5",
81
81
  "prettier": "^3.6.2",