trackersdk2 1.0.0

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.
@@ -0,0 +1,588 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var webVitals = require('web-vitals');
6
+
7
+ class TrackerSDK {
8
+ // #region 构造函数 & 初始化
9
+ constructor() {
10
+ this.autoTrackClickHandler = null;
11
+ // 事件队列
12
+ this.eventQueue = [];
13
+ // 批量上报定时器
14
+ this.batchTimer = null;
15
+ // 页面卸载前是否已发送数据标识
16
+ this.hasSentBeforeUnload = false;
17
+ this.config = {
18
+ appId: "",
19
+ serverUrl: "",
20
+ userId: "",
21
+ debug: true,
22
+ enableAutoClickTagNameList: [],
23
+ autoTrack: false,
24
+ batchSize: 10,
25
+ batchInterval: 5000,
26
+ };
27
+ }
28
+ init(config) {
29
+ if (!window || typeof window !== "object") {
30
+ console.warn("TrackerSDK 仅支持浏览器环境,当前环境已跳过初始化");
31
+ return;
32
+ }
33
+ if (!config.serverUrl) {
34
+ throw new Error("serverUrl is required");
35
+ }
36
+ if (!config.appId) {
37
+ throw new Error("appId is required");
38
+ }
39
+ this.config = {
40
+ ...this.config,
41
+ ...config,
42
+ };
43
+ this.setupErrorTracking();
44
+ this.setupPerformanceTracking();
45
+ this.trackUV();
46
+ // 监听页面卸载
47
+ this.setupPageUnloadHandler();
48
+ // 重试之前失败的批量上报
49
+ this.retryFailedBatches();
50
+ }
51
+ // #endregion
52
+ // #region 埋点api工具方法
53
+ /**
54
+ * @description 重试批量上报之前上报失败的数据
55
+ * @returns Promise<void>
56
+ */
57
+ async retryFailedBatches() {
58
+ try {
59
+ const key = "tracker_failed_batches";
60
+ const batchesStr = localStorage.getItem(key);
61
+ if (!batchesStr)
62
+ return;
63
+ const batches = JSON.parse(batchesStr);
64
+ const remainingBatches = [];
65
+ for (const batch of batches) {
66
+ try {
67
+ await this.fallbackSendBatch(batch);
68
+ }
69
+ catch (err) {
70
+ remainingBatches.push(batch);
71
+ }
72
+ }
73
+ if (remainingBatches.length) {
74
+ // 只保留3批
75
+ const toSave = remainingBatches.splice(-3);
76
+ localStorage.setItem(key, JSON.stringify(toSave));
77
+ }
78
+ else {
79
+ localStorage.removeItem(key);
80
+ }
81
+ }
82
+ catch (err) {
83
+ console.warn("Failed to retry batches:", err);
84
+ }
85
+ }
86
+ /**
87
+ * @description 监听页面卸载事件,确保数据上报
88
+ * @returns void
89
+ */
90
+ setupPageUnloadHandler() {
91
+ const flushAndSend = () => {
92
+ if (this.hasSentBeforeUnload || this.eventQueue.length == 0)
93
+ return;
94
+ // 清除定时器,防止内存泄漏
95
+ if (this.batchTimer) {
96
+ clearTimeout(this.batchTimer);
97
+ this.batchTimer = null;
98
+ }
99
+ // 标记为已上报
100
+ this.hasSentBeforeUnload = true;
101
+ const payload = { events: [...this.eventQueue] };
102
+ // 清空队列
103
+ this.eventQueue = [];
104
+ // this.sendBatch(payload.events);
105
+ if (navigator.sendBeacon) {
106
+ const blob = new Blob([JSON.stringify(payload)], {
107
+ type: "application/json",
108
+ });
109
+ if (!navigator.sendBeacon(this.config.serverUrl, blob)) {
110
+ this.fallbackSendBatch(payload);
111
+ }
112
+ }
113
+ else {
114
+ this.fallbackSendBatch(payload);
115
+ }
116
+ };
117
+ // 页面级别卸载事件
118
+ // beforeunload safari不支持
119
+ window.addEventListener("beforeunload", flushAndSend);
120
+ // pagehide 兜底
121
+ window.addEventListener("pagehide", flushAndSend);
122
+ // 可选:页面最小化、切换页签时触发
123
+ // window.addEventListener("visibilitychange", () => {
124
+ // if (document.hidden) flushAndSend();
125
+ // });
126
+ }
127
+ /**
128
+ * @description 获取设备id
129
+ */
130
+ getDeviceId() {
131
+ let id = localStorage.getItem("tracker_device_id");
132
+ if (!id) {
133
+ id =
134
+ "device_" +
135
+ Date.now() +
136
+ "_" +
137
+ Math.random().toString(32).substring(2, 10);
138
+ localStorage.setItem("tracker_device_id", id);
139
+ }
140
+ return id;
141
+ }
142
+ /**
143
+ * @description navigator.sendBeacon 和 fetch 双通道实现数据上报
144
+ * @param data 上报的负载
145
+ */
146
+ send(data) {
147
+ const payload = {
148
+ ...data,
149
+ userId: this.config.userId || this.getDeviceId(),
150
+ timestamp: Date.now(),
151
+ url: window.location.href,
152
+ title: document.title,
153
+ userAgent: navigator.userAgent,
154
+ appId: this.config.appId,
155
+ };
156
+ // 错误事件立即上报
157
+ if (data.eventType == "error")
158
+ return this.sendImmediately(payload);
159
+ this.enqueue(payload);
160
+ }
161
+ /**
162
+ * @description 入队列
163
+ * @param payload 上报的负载
164
+ */
165
+ enqueue(payload) {
166
+ this.eventQueue.push(payload);
167
+ // 达到数量阈值,立即上报
168
+ if (this.eventQueue.length >= this.config.batchSize) {
169
+ return this.flush();
170
+ }
171
+ // 启动定时器
172
+ if (!this.batchTimer) {
173
+ this.batchTimer = setTimeout(() => {
174
+ this.flush();
175
+ }, this.config.batchInterval);
176
+ }
177
+ }
178
+ /**
179
+ * @description 刷新队列,批量上报
180
+ * @return void
181
+ */
182
+ flush() {
183
+ if (this.eventQueue.length === 0)
184
+ return;
185
+ const batchPayload = [...this.eventQueue];
186
+ this.eventQueue = [];
187
+ if (this.batchTimer) {
188
+ clearTimeout(this.batchTimer);
189
+ this.batchTimer = null;
190
+ }
191
+ this.sendBatch(batchPayload);
192
+ }
193
+ /**
194
+ * @description navigator.sendBeacon单条错误上报失败的fetch回退方案
195
+ * @param payload any
196
+ * */
197
+ async fallbackSend(payload) {
198
+ try {
199
+ await fetch(this.config.serverUrl, {
200
+ method: "POST",
201
+ headers: {
202
+ "Content-Type": "application/json",
203
+ },
204
+ body: JSON.stringify(payload),
205
+ keepalive: true,
206
+ // credentials: "include",
207
+ });
208
+ }
209
+ catch (error) {
210
+ if (this.config.debug) {
211
+ console.error("Single event fallback send failed:", error);
212
+ }
213
+ this.saveFailedBatch({ events: [payload] });
214
+ }
215
+ }
216
+ /**
217
+ * @description 错误事件立即上报
218
+ * @param data 上报的负载
219
+ * @returns void
220
+ */
221
+ sendImmediately(payload) {
222
+ // if (navigator.sendBeacon) {
223
+ // const blob = new Blob([JSON.stringify(payload)], {
224
+ // type: "application/json",
225
+ // });
226
+ // if (!navigator.sendBeacon(this.config.serverUrl, blob)) {
227
+ // this.fallbackSend(payload);
228
+ // }
229
+ // } else {
230
+ // this.fallbackSend(payload);
231
+ // }
232
+ /* 改用 fetch api */
233
+ this.fallbackSend(payload);
234
+ }
235
+ /**
236
+ * @description 批量上报
237
+ * @param events 上报的事件数组
238
+ * @returns Promise<void>
239
+ */
240
+ async sendBatch(events) {
241
+ const payload = { events };
242
+ // if (navigator.sendBeacon) {
243
+ // const blob = new Blob([JSON.stringify(payload)], {
244
+ // type: "application/json",
245
+ // });
246
+ // if (!navigator.sendBeacon(this.config.serverUrl, blob)) {
247
+ // await this.fallbackSendBatch(payload);
248
+ // }
249
+ // } else {
250
+ // await this.fallbackSendBatch(payload);
251
+ // }
252
+ /* 改用 fetch api */
253
+ await this.fallbackSendBatch(payload);
254
+ }
255
+ /**
256
+ * @description 批量上报的fetch回退方案
257
+ * @param payload 上报的负载
258
+ * @returns Promise<void>
259
+ */
260
+ async fallbackSendBatch(payload) {
261
+ try {
262
+ await fetch(this.config.serverUrl, {
263
+ method: "POST",
264
+ headers: {
265
+ "Content-Type": "application/json",
266
+ },
267
+ body: JSON.stringify(payload),
268
+ keepalive: true,
269
+ // credentials: "include",
270
+ });
271
+ }
272
+ catch (error) {
273
+ if (this.config.debug) {
274
+ console.error("[fallbackSendBatch] fecth批量上报失败:", error);
275
+ }
276
+ // 可选:存入 localStorage 用于下次重试
277
+ this.saveFailedBatch(payload);
278
+ }
279
+ }
280
+ saveFailedBatch(payload) {
281
+ try {
282
+ const key = "tracker_failed_batches";
283
+ const existing = localStorage.getItem(key);
284
+ const batches = existing ? JSON.parse(existing) : [];
285
+ batches.push(payload);
286
+ if (batches.length > 3)
287
+ batches.shift(); // 限制最多3批
288
+ localStorage.setItem(key, JSON.stringify(batches));
289
+ }
290
+ catch (e) {
291
+ // localStorage 可能满或禁用,静默忽略
292
+ }
293
+ }
294
+ /**
295
+ * @description 自定义点击事件
296
+ */
297
+ track(event, properties = {}) {
298
+ this.send({
299
+ eventType: "custom",
300
+ event,
301
+ properties,
302
+ });
303
+ }
304
+ /**
305
+ * @description 用于自动点击埋点
306
+ *
307
+ * @param element 点击的DOM元素
308
+ */
309
+ trackClick(element) {
310
+ var _a;
311
+ const tagName = element.tagName.toLowerCase();
312
+ const id = element.id || "";
313
+ const classes = Array.from(element.classList).join(" ");
314
+ const text = ["button", "a"].includes(tagName)
315
+ ? (_a = element.innerText) === null || _a === void 0 ? void 0 : _a.trim().slice(0, 20)
316
+ : "";
317
+ if (this.config.enableAutoClickTagNameList.includes(tagName)) {
318
+ this.send({
319
+ eventType: "click",
320
+ event: "auto_click",
321
+ properties: {
322
+ tagName,
323
+ id,
324
+ classes,
325
+ text,
326
+ },
327
+ });
328
+ }
329
+ else {
330
+ if (this.config.debug) {
331
+ console.log("点击元素不触发自动埋点");
332
+ }
333
+ }
334
+ }
335
+ // #endregion
336
+ // #region pv & uv
337
+ /**
338
+ * @description 自动pv
339
+ *
340
+ * @param path pv的页面路径
341
+ * @param properties 上报数据
342
+ */
343
+ trackPV(path, properties = {}) {
344
+ this.send({
345
+ eventType: "pv",
346
+ properties: {
347
+ path,
348
+ referrer: document.referrer,
349
+ ...properties,
350
+ },
351
+ });
352
+ }
353
+ /**
354
+ * @description 统计uv(每天每个设备只上报一次)
355
+ *
356
+ */
357
+ trackUV() {
358
+ const deviceId = this.getDeviceId();
359
+ const now = new Date().getTime();
360
+ const beijingTime = new Date(now + 8 * 3600 * 1000);
361
+ const today = new Date(beijingTime).toISOString().split("T")[0];
362
+ const uvKey = `tracker_uv_${today}`;
363
+ if (!localStorage.getItem(uvKey)) {
364
+ this.send({
365
+ eventType: "uv",
366
+ properties: {
367
+ deviceId,
368
+ date: today,
369
+ },
370
+ });
371
+ localStorage.setItem(uvKey, "1");
372
+ }
373
+ }
374
+ // #endregion
375
+ // #region 错误监控
376
+ /** @description 初始化错误事件监听
377
+ *
378
+ */
379
+ setupErrorTracking() {
380
+ // 全局 JS 错误
381
+ window.addEventListener("error", ({ message, filename, lineno, colno, error }) => {
382
+ console.log("🚀 ~ '全局 JS 错误捕获了'");
383
+ this.send({
384
+ eventType: "error",
385
+ event: "js_error",
386
+ properties: {
387
+ message,
388
+ filename,
389
+ lineno,
390
+ colno,
391
+ stack: (error === null || error === void 0 ? void 0 : error.stack) || "no stack",
392
+ },
393
+ });
394
+ });
395
+ // 资源加载错误
396
+ window.addEventListener("error", (e) => {
397
+ var _a;
398
+ if ((_a = e.target) === null || _a === void 0 ? void 0 : _a.localName) {
399
+ let url = e.target.src || e.target.href || "";
400
+ url = url.trim();
401
+ const tag = e.target;
402
+ const originalSrc = tag.getAttribute("src");
403
+ const originalHref = tag.getAttribute("href");
404
+ // 如果原始值是空字符串(空 src 会被解析成当前页)、null、undefined,则是伪错误
405
+ if ((originalSrc != null && originalSrc.trim() === "") ||
406
+ (originalHref != null && originalHref.trim() === "")) {
407
+ return false; // 过滤掉
408
+ }
409
+ if (url == "")
410
+ return false;
411
+ try {
412
+ // 验证是否是合法 URL
413
+ new URL(url);
414
+ console.log("🚀 ~ '资源加载错误捕获了'");
415
+ this.send({
416
+ eventType: "error",
417
+ event: "resource_load_error",
418
+ properties: {
419
+ resourceUrl: url,
420
+ tageName: e.target.localName,
421
+ },
422
+ });
423
+ }
424
+ catch (err) {
425
+ return false;
426
+ }
427
+ }
428
+ }, true);
429
+ // Promise unhandled rejection
430
+ window.addEventListener("unhandledrejection", (event) => {
431
+ "Promise unhandled rejection捕获了";
432
+ var _a;
433
+ console.log("🚀 ~ 'Promise unhandled rejection捕获了'");
434
+ this.send({
435
+ eventType: "error",
436
+ event: "promise_unhandled_rejection",
437
+ properties: {
438
+ reason: String(event.reason),
439
+ stack: ((_a = event.reason) === null || _a === void 0 ? void 0 : _a.stack) || "no stack",
440
+ },
441
+ });
442
+ // 阻止打印报错信息
443
+ event.preventDefault();
444
+ });
445
+ }
446
+ /**
447
+ * @description vue3 全局错误处理器
448
+ */
449
+ setupVueErrorHandlder(app) {
450
+ app.config.errorHandler = (err, instance, info) => {
451
+ var _a;
452
+ console.log("🚀 ~ vue3 全局错误处理器捕获了");
453
+ // setTimeout(() => {
454
+ // throw err;
455
+ // }, 0);
456
+ this.send({
457
+ eventType: "error",
458
+ event: "vue_component_error",
459
+ properties: {
460
+ message: err.message,
461
+ stack: err.stack || "no stack",
462
+ componentInfo: info,
463
+ componentName: ((_a = instance === null || instance === void 0 ? void 0 : instance.$options) === null || _a === void 0 ? void 0 : _a.name) || "unknown",
464
+ },
465
+ });
466
+ };
467
+ }
468
+ // #endregion
469
+ // #region 性能监控
470
+ setupPerformanceTracking() {
471
+ // if (!("performance" in window)) return;
472
+ // const perf: any = performance.getEntriesByType("navigation")[0];
473
+ // if (!perf) return;
474
+ // this.send({
475
+ // eventType: "performance",
476
+ // event: "page_performance",
477
+ // properties: {
478
+ // dns: perf.domainLookupEnd - perf.domainLookupStart,
479
+ // tcpAndTls: perf.connectEnd - perf.connectStart,
480
+ // domReady: perf.domContentLoadedEventEnd - perf.fetchStart,
481
+ // load: perf.loadEventEnd - perf.fetchStart,
482
+ // },
483
+ // });
484
+ this.observeFCP();
485
+ this.observeLCP();
486
+ this.observeCLS();
487
+ this.observeTTFB();
488
+ }
489
+ /**
490
+ * @description 监控FCP
491
+ * @returns void
492
+ */
493
+ observeFCP() {
494
+ webVitals.onFCP((metric) => {
495
+ this.send({
496
+ eventType: "performance",
497
+ event: "first_contentful_paint",
498
+ properties: metric,
499
+ });
500
+ });
501
+ }
502
+ /**
503
+ * @description 监控LCP
504
+ * @returns void
505
+ */
506
+ observeLCP() {
507
+ webVitals.onLCP((metric) => {
508
+ this.send({
509
+ eventType: "performance",
510
+ event: "largest_contentful_paint",
511
+ properties: metric,
512
+ });
513
+ });
514
+ }
515
+ /**
516
+ * @description 监控CLS
517
+ * @returns void
518
+ */
519
+ observeCLS() {
520
+ webVitals.onCLS((metric) => {
521
+ this.send({
522
+ eventType: "performance",
523
+ event: "cumulative_layout_shift",
524
+ properties: {
525
+ ...metric,
526
+ entries: undefined, // 去掉 entries,数据量可能很庞大
527
+ },
528
+ });
529
+ });
530
+ }
531
+ /**
532
+ * @description 监控TTFB
533
+ * @returns void
534
+ */
535
+ observeTTFB() {
536
+ webVitals.onTTFB((metric) => {
537
+ this.send({
538
+ eventType: "performance",
539
+ event: "time_to_first_byte",
540
+ properties: metric,
541
+ });
542
+ });
543
+ }
544
+ // #endregion
545
+ // #region 全埋点
546
+ /**
547
+ * @description 全埋点(谨慎使用,存在性能问题)
548
+ */
549
+ enableAutoTracking() {
550
+ // 防止重复绑定
551
+ if (this.autoTrackClickHandler)
552
+ return;
553
+ this.autoTrackClickHandler = (e) => {
554
+ if (e.target instanceof Element) {
555
+ this.trackClick(e.target);
556
+ }
557
+ };
558
+ document.documentElement.addEventListener("click", this.autoTrackClickHandler);
559
+ }
560
+ /**
561
+ * @description: 设置可触发自动埋点的DOM元素标签名称白名单
562
+ */
563
+ setEnableAutoClickTagNameList(tagNameList) {
564
+ if (this.config.autoTrack) {
565
+ this.config.enableAutoClickTagNameList = tagNameList;
566
+ }
567
+ }
568
+ }
569
+ // 单例导出
570
+ const tracker = new TrackerSDK();
571
+
572
+ var index = {
573
+ install(app, options) {
574
+ tracker.init(options);
575
+ app.provide("tracker", tracker);
576
+ // 自动化埋点,存在 performance
577
+ if (options.autoTrack) {
578
+ tracker.enableAutoTracking();
579
+ }
580
+ },
581
+ };
582
+ function useTracker() {
583
+ return tracker;
584
+ }
585
+
586
+ exports.default = index;
587
+ exports.tracker = tracker;
588
+ exports.useTracker = useTracker;