monitor-track 1.10.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.
Files changed (65) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/README.md +101 -0
  3. package/dist/CHANGELOG.md +21 -0
  4. package/dist/README.md +101 -0
  5. package/dist/cjs/config/global.d.ts +17 -0
  6. package/dist/cjs/config/index.d.ts +33 -0
  7. package/dist/cjs/constant.d.ts +2 -0
  8. package/dist/cjs/handlers/error.d.ts +23 -0
  9. package/dist/cjs/handlers/index.d.ts +3 -0
  10. package/dist/cjs/handlers/pv.d.ts +24 -0
  11. package/dist/cjs/handlers/user-activity.d.ts +9 -0
  12. package/dist/cjs/index.d.ts +30 -0
  13. package/dist/cjs/index.js +1485 -0
  14. package/dist/cjs/main.d.ts +1 -0
  15. package/dist/cjs/reporter.d.ts +198 -0
  16. package/dist/cjs/types/config.d.ts +59 -0
  17. package/dist/cjs/types/global.d.ts +70 -0
  18. package/dist/cjs/types/index.d.ts +189 -0
  19. package/dist/cjs/utils/index.d.ts +45 -0
  20. package/dist/esm/_virtual/_tslib.js +26 -0
  21. package/dist/esm/config/global.d.ts +17 -0
  22. package/dist/esm/config/global.js +271 -0
  23. package/dist/esm/config/index.d.ts +33 -0
  24. package/dist/esm/config/index.js +32 -0
  25. package/dist/esm/constant.d.ts +2 -0
  26. package/dist/esm/constant.js +4 -0
  27. package/dist/esm/handlers/error.d.ts +23 -0
  28. package/dist/esm/handlers/error.js +181 -0
  29. package/dist/esm/handlers/index.d.ts +3 -0
  30. package/dist/esm/handlers/pv.d.ts +24 -0
  31. package/dist/esm/handlers/pv.js +99 -0
  32. package/dist/esm/handlers/user-activity.d.ts +9 -0
  33. package/dist/esm/handlers/user-activity.js +66 -0
  34. package/dist/esm/index.d.ts +30 -0
  35. package/dist/esm/index.js +115 -0
  36. package/dist/esm/main.d.ts +1 -0
  37. package/dist/esm/package.json.js +3 -0
  38. package/dist/esm/reporter.d.ts +198 -0
  39. package/dist/esm/reporter.js +359 -0
  40. package/dist/esm/types/config.d.ts +59 -0
  41. package/dist/esm/types/global.d.ts +70 -0
  42. package/dist/esm/types/index.d.ts +189 -0
  43. package/dist/esm/utils/index.d.ts +45 -0
  44. package/dist/esm/utils/index.js +354 -0
  45. package/dist/index.js +1534 -0
  46. package/dist/package.json +41 -0
  47. package/package.json +42 -0
  48. package/rollup.config.js +66 -0
  49. package/scripts/cp.js +11 -0
  50. package/src/config/global.ts +309 -0
  51. package/src/config/index.ts +53 -0
  52. package/src/constant.ts +2 -0
  53. package/src/handlers/error.ts +187 -0
  54. package/src/handlers/index.ts +3 -0
  55. package/src/handlers/pv.ts +124 -0
  56. package/src/handlers/user-activity.ts +69 -0
  57. package/src/index.ts +127 -0
  58. package/src/main.ts +8 -0
  59. package/src/reporter.ts +372 -0
  60. package/src/types/config.ts +58 -0
  61. package/src/types/global.ts +73 -0
  62. package/src/types/index.ts +227 -0
  63. package/src/typing.d.ts +15 -0
  64. package/src/utils/index.ts +371 -0
  65. package/tsconfig.esm.json +14 -0
@@ -0,0 +1,1485 @@
1
+ 'use strict';
2
+
3
+ var axios = require('axios');
4
+ var uuid = require('uuid');
5
+ var uaParserJs = require('ua-parser-js');
6
+ var rrweb = require('rrweb');
7
+ var ErrorStackParser = require('error-stack-parser');
8
+ var html2canvas = require('html2canvas');
9
+
10
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
+
12
+ function _interopNamespace(e) {
13
+ if (e && e.__esModule) return e;
14
+ var n = Object.create(null);
15
+ if (e) {
16
+ Object.keys(e).forEach(function (k) {
17
+ if (k !== 'default') {
18
+ var d = Object.getOwnPropertyDescriptor(e, k);
19
+ Object.defineProperty(n, k, d.get ? d : {
20
+ enumerable: true,
21
+ get: function () { return e[k]; }
22
+ });
23
+ }
24
+ });
25
+ }
26
+ n["default"] = e;
27
+ return Object.freeze(n);
28
+ }
29
+
30
+ var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
31
+ var rrweb__namespace = /*#__PURE__*/_interopNamespace(rrweb);
32
+ var ErrorStackParser__default = /*#__PURE__*/_interopDefaultLegacy(ErrorStackParser);
33
+ var html2canvas__default = /*#__PURE__*/_interopDefaultLegacy(html2canvas);
34
+
35
+ /*! *****************************************************************************
36
+ Copyright (c) Microsoft Corporation.
37
+
38
+ Permission to use, copy, modify, and/or distribute this software for any
39
+ purpose with or without fee is hereby granted.
40
+
41
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
42
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
43
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
44
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
45
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
46
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
47
+ PERFORMANCE OF THIS SOFTWARE.
48
+ ***************************************************************************** */
49
+
50
+ function __awaiter(thisArg, _arguments, P, generator) {
51
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
52
+ return new (P || (P = Promise))(function (resolve, reject) {
53
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
54
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
55
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
56
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
57
+ });
58
+ }
59
+
60
+ const shuyunTrackId = 'shuyun-track-id';
61
+ const shuyunTrackSessionId = 'shuyun-track-session-id';
62
+
63
+ /*
64
+ * @Author: Mark.Zhang
65
+ * @Description 配置项相关参数及方法
66
+ */
67
+ /** */
68
+ /**
69
+ * @description config配置项默认值
70
+ */
71
+ const Config = {
72
+ reportUrl: '',
73
+ projectID: '',
74
+ maxLength: 1000,
75
+ spa: false,
76
+ hash: false,
77
+ enableBehavior: true,
78
+ enableError: true,
79
+ enableVisualTrack: false,
80
+ ignore: {
81
+ urls: [],
82
+ errors: [],
83
+ apis: [],
84
+ },
85
+ };
86
+ /**
87
+ * @description 设置config配置项
88
+ * @param config 配置项
89
+ */
90
+ function setConfig(config) {
91
+ Object.assign(Config, config);
92
+ }
93
+
94
+ /*
95
+ * @Author: Mark.Zhang
96
+ * @Date: 2020-10-26 11:10:04
97
+ * @Description 资源加载错误及js错误相关的处理方法
98
+ */
99
+ const eventsMatrix = [[]];
100
+ rrweb__namespace.record({
101
+ emit(event, isCheckout) {
102
+ // isCheckout 是一个标识,告诉你重新制作了快照
103
+ if (isCheckout) {
104
+ eventsMatrix.push([]);
105
+ }
106
+ if (eventsMatrix.length > 2) {
107
+ eventsMatrix.shift();
108
+ }
109
+ const lastEvents = eventsMatrix[eventsMatrix.length - 1];
110
+ lastEvents.push(event);
111
+ },
112
+ // 每30秒重新制作快照
113
+ checkoutEveryNms: 30 * 1000,
114
+ });
115
+ /**
116
+ * @description 错误事件触发后的操作
117
+ */
118
+ function handleError(event) {
119
+ setReportValue('type', 'error');
120
+ if (event.type === 'error') {
121
+ if (event instanceof ErrorEvent) {
122
+ setCaughtError(event);
123
+ }
124
+ else {
125
+ setResourceError(event);
126
+ }
127
+ }
128
+ else if (event.type === 'unhandledrejection') {
129
+ setPromiseError(event);
130
+ }
131
+ }
132
+ /**
133
+ * @description 设置错误信息
134
+ */
135
+ function setCaughtError(event) {
136
+ var _a, _b;
137
+ if (ignoreError(event.message)) {
138
+ return;
139
+ }
140
+ let filename = event.filename, line = event.lineno, column = event.colno, functionName = '', stackFrame = JSON.stringify([]);
141
+ if (event.error && event.error.stack) {
142
+ const error = ErrorStackParser__default["default"].parse(event.error);
143
+ stackFrame = JSON.stringify(error);
144
+ const info = error.find((item) => item.fileName && !/node_modules|vendor|bundle/.test(item.fileName));
145
+ if (info) {
146
+ filename = info.fileName;
147
+ line = Number(info.lineNumber);
148
+ column = Number(info.columnNumber);
149
+ functionName = info.functionName || '';
150
+ }
151
+ }
152
+ setReportValue('error', {
153
+ subType: 'js',
154
+ line,
155
+ column,
156
+ message: event.message,
157
+ filename,
158
+ functionName,
159
+ stack: ((_b = (_a = event === null || event === void 0 ? void 0 : event.error) === null || _a === void 0 ? void 0 : _a.stack) === null || _b === void 0 ? void 0 : _b.substring(0, Config.maxLength)) || '',
160
+ stackFrame,
161
+ events: getUserEvents(false),
162
+ });
163
+ report(getReport());
164
+ }
165
+ /**
166
+ * @description 设置异步错误信息
167
+ */
168
+ function setPromiseError(event) {
169
+ let message = event.reason;
170
+ if (event.reason && typeof event.reason === 'object') {
171
+ message = JSON.stringify(event.reason);
172
+ if (message === '{}') {
173
+ message = event.reason.stack;
174
+ }
175
+ }
176
+ if (ignoreError(message)) {
177
+ return;
178
+ }
179
+ setReportValue('error', {
180
+ subType: 'async',
181
+ message,
182
+ events: getUserEvents(false),
183
+ });
184
+ report(getReport());
185
+ }
186
+ /**
187
+ * @description 设置资源错误信息
188
+ */
189
+ function setResourceError(event) {
190
+ const target = event.target;
191
+ if (ignoreError(target.outerHTML)) {
192
+ return;
193
+ }
194
+ setReportValue('error', {
195
+ subType: 'resource',
196
+ message: target.outerHTML,
197
+ filename: target.src || target.currentSrc,
198
+ stack: target.localName.toUpperCase().substring(0, Config.maxLength),
199
+ events: getUserEvents(false),
200
+ });
201
+ report(getReport());
202
+ }
203
+ /**
204
+ * 忽略的error
205
+ * @param errorMsg 错误信息
206
+ */
207
+ function ignoreError(errorMsg) {
208
+ var _a;
209
+ const errors = ((_a = Config === null || Config === void 0 ? void 0 : Config.ignore) === null || _a === void 0 ? void 0 : _a.errors) || [];
210
+ const existIgnoreError = errors.findIndex((item) => {
211
+ if (typeof item === 'string') {
212
+ return item === errorMsg;
213
+ }
214
+ else if (Object.prototype.toString.call(item) === '[object Object]') {
215
+ if (item.regExp && typeof item.input === 'string') {
216
+ const regex = new RegExp(item.input, 'g');
217
+ return regex.test(errorMsg);
218
+ }
219
+ return false;
220
+ }
221
+ else {
222
+ return false;
223
+ }
224
+ });
225
+ return existIgnoreError > -1;
226
+ }
227
+ /**
228
+ * 获取用户最近得操作,视频录像时间应当在30秒到60秒之间
229
+ */
230
+ let lastGetTime = Date.now(); //缓存操作录像,防止段时间内大量获取录像数据导滞内存溢出
231
+ function getUserEvents(notUseCache) {
232
+ let events;
233
+ if (Date.now() - lastGetTime > 2000 || notUseCache) {
234
+ lastGetTime = Date.now();
235
+ if (eventsMatrix.length >= 2) {
236
+ const finalVideoData = eventsMatrix[eventsMatrix.length - 1];
237
+ events = JSON.stringify(eventsMatrix[eventsMatrix.length - 2].concat(finalVideoData));
238
+ if (events.length > 1024 * 1024 * 15 && JSON.stringify(finalVideoData).length > 1024 * 1024) {
239
+ //如果录像数据量太大且最后那个分片的数据量不算小,那么缩短上报的录像时长
240
+ events = JSON.stringify(eventsMatrix[eventsMatrix.length - 1]);
241
+ }
242
+ }
243
+ else {
244
+ events = JSON.stringify(eventsMatrix);
245
+ }
246
+ }
247
+ return events;
248
+ }
249
+ function getFullScreenShoot(filename) {
250
+ const name = filename || uuid.v4();
251
+ return html2canvas__default["default"](document.body).then(function (canvas) {
252
+ var _a;
253
+ const urlData = canvas.toDataURL('image/png', 1);
254
+ const bytes = window.atob(urlData.split(',')[1]);
255
+ const mime = (_a = urlData.split(',')[0].match(/:(.*?);/)) === null || _a === void 0 ? void 0 : _a[1];
256
+ const ab = new ArrayBuffer(bytes.length);
257
+ const ia = new Uint8Array(ab);
258
+ for (let i = 0; i < bytes.length; i++) {
259
+ ia[i] = bytes.charCodeAt(i);
260
+ }
261
+ const file = new File([ab], `${name}.png`, { type: mime });
262
+ return file;
263
+ });
264
+ }
265
+
266
+ /** 上报数据映射 */
267
+ const reportMap = {
268
+ /** SDK 版本 */
269
+ version: '_v',
270
+ /** 项目ID */
271
+ projectID: 'pid',
272
+ /** location.host */
273
+ host: 'host',
274
+ /** 用户uuid */
275
+ uuid: '_id',
276
+ /** 上报日志的时间戳 */
277
+ time: 'time',
278
+ /** 源页面 */
279
+ originPage: 'o',
280
+ /** 当前页面 */
281
+ page: 'p',
282
+ /** 自定义payload */
283
+ customPayload: 'cP',
284
+ /** 信息类型 */
285
+ type: 't',
286
+ /** viewport的宽高 */
287
+ viewport: 'vp',
288
+ /** 屏幕宽高 */
289
+ screen: 'sc',
290
+ /** 页面title */
291
+ pageTitle: 'pt',
292
+ /** 页面referrer */
293
+ referrer: 'ref',
294
+ /** 页面编码 */
295
+ charset: 'char',
296
+ /** language */
297
+ language: 'lan',
298
+ /** vendor, 浏览器厂商 */
299
+ navigatorVendor: 'nV',
300
+ /** 网络连接类型 */
301
+ connectionType: 'cT',
302
+ /** 浏览器名称 */
303
+ browserName: 'bN',
304
+ /** 浏览器版本 */
305
+ browserVersion: 'bV',
306
+ /** 浏览器引擎名称 */
307
+ engineName: 'enN',
308
+ /** 浏览器引擎版本 */
309
+ engineVersion: 'enV',
310
+ /** 操作系统名称 */
311
+ osName: 'osN',
312
+ /** 操作系统版本 */
313
+ osVersion: 'osV',
314
+ pv: {
315
+ /** 子类型 */
316
+ subType: 'suT',
317
+ /** 停留时间 */
318
+ stayTime: 'sT',
319
+ },
320
+ error: {
321
+ /** 子类型 */
322
+ subType: 'suT',
323
+ /** 错误行 */
324
+ line: 'line',
325
+ /** 错误列 */
326
+ column: 'col',
327
+ /** 错误信息 */
328
+ message: 'msg',
329
+ /** 错误文件 */
330
+ filename: 'fil',
331
+ /** 错误函数名 */
332
+ functionName: 'fn',
333
+ /** 错误栈信息 */
334
+ stack: '_st',
335
+ /** 错误帧 */
336
+ stackFrame: '_sf',
337
+ /** 视频录制数据 */
338
+ events: 'evt',
339
+ /** 页面截图 */
340
+ picture: 'pic',
341
+ },
342
+ ua: {
343
+ /** 子类型 */
344
+ subType: 'suT',
345
+ /** x轴坐标 */
346
+ x: 'x',
347
+ /** y轴坐标 */
348
+ y: 'y',
349
+ /** 元素路径 */
350
+ path: 'path',
351
+ },
352
+ /** 性能数据 */
353
+ perf: {
354
+ navigation: {
355
+ /**
356
+ *一个无符号短整型,表示是如何导航到这个页面的。可能的值如下:
357
+ TYPE_NAVIGATE (0)
358
+ 当前页面是通过点击链接,书签和表单提交,或者脚本操作,或者在url中直接输入地址,type值为0
359
+ TYPE_RELOAD (1)
360
+ 点击刷新页面按钮或者通过Location.reload()方法显示的页面,type值为1
361
+ The page was accessed by clicking the Reload button or via the Location.reload() method.
362
+ TYPE_BACK_FORWARD (2)
363
+ 页面通过历史记录和前进后退访问时。type值为2
364
+ TYPE_RESERVED (255)
365
+ 任何其他方式,type值为255
366
+ */
367
+ type: 't',
368
+ /** 无符号短整型,表示在到达这个页面之前重定向了多少次。 */
369
+ redirectCount: 'rc',
370
+ },
371
+ memory: {
372
+ /** 上下文内可用堆的最大体积,以字节计算。 */
373
+ jsHeapSizeLimit: 'hsl',
374
+ /** 已分配的堆体积,以字节计算。 */
375
+ totalJSHeapSize: 'ths',
376
+ /** 当前 JS 堆活跃段(segment)的体积,以字节计算。 */
377
+ usedJSHeapSize: 'uhs',
378
+ },
379
+ /** 返回性能测量开始时的时间的高精度时间戳。 */
380
+ timeOrigin: 'to',
381
+ timing: {
382
+ /** 是一个无符号long long 型的毫秒数,返回浏览器与服务器之间的连接建立时的Unix毫秒时间戳。如果建立的是持久连接,则返回值等同于fetchStart属性的值。连接建立指的是所有握手和认证过程全部结束。 */
383
+ connectEnd: 'ce',
384
+ /** 是一个无符号long long 型的毫秒数,返回HTTP请求开始向服务器发送时的Unix毫秒时间戳。如果使用持久连接(persistent connection),则返回值等同于fetchStart属性的值 */
385
+ connectStart: 'cs',
386
+ /** 是一个无符号long long 型的毫秒数,返回当前文档解析完成,即Document.readyState 变为 'complete'且相对应的readystatechange (en-US) 被触发时的Unix毫秒时间戳。 */
387
+ domComplete: 'dc',
388
+ /** 是一个无符号long long 型的毫秒数,返回当所有需要立即执行的脚本已经被执行(不论执行顺序)时的Unix毫秒时间戳。 */
389
+ domContentLoadedEventEnd: 'scLee',
390
+ /** 是一个无符号long long 型的毫秒数,返回当解析器发送DOMContentLoaded (en-US) 事件,即所有需要被执行的脚本已经被解析时的Unix毫秒时间戳。 */
391
+ domContentLoadedEventStart: 'dcLes',
392
+ /** 是一个无符号long long 型的毫秒数,返回当前网页DOM结构结束解析、开始加载内嵌资源时(即Document.readyState属性变为“interactive”、相应的readystatechange (en-US)事件触发时)的Unix毫秒时间戳。 */
393
+ domInteractive: 'di',
394
+ /** 是一个无符号long long 型的毫秒数,返回当前网页DOM结构开始解析时(即Document.readyState属性变为“loading”、相应的 readystatechange (en-US)事件触发时)的Unix毫秒时间戳。 */
395
+ domLoading: 'dl',
396
+ /** 是一个无符号long long 型的毫秒数,表征了域名查询结束的UNIX时间戳。如果使用了持续连接(persistent connection),或者这个信息存储到了缓存或者本地资源上,这个值将和 PerformanceTiming.fetchStart一致。 */
397
+ domainLookupEnd: 'dle',
398
+ /** 是一个无符号long long 型的毫秒数,表征了域名查询开始的UNIX时间戳。如果使用了持续连接(persistent connection),或者这个信息存储到了缓存或者本地资源上,这个值将和 PerformanceTiming.fetchStart一致。 */
399
+ domainLookupStart: 'dls',
400
+ /** 是一个无符号long long 型的毫秒数,表征了浏览器准备好使用HTTP请求来获取(fetch)文档的UNIX时间戳。这个时间点会在检查任何应用缓存之前。 */
401
+ fetchStart: 'fs',
402
+ /** 是一个无符号long long 型的毫秒数,返回当load (en-US)事件结束,即加载事件完成时的Unix毫秒时间戳。如果这个事件还未被发送,或者尚未完成,它的值将会是0. */
403
+ loadEventEnd: 'lee',
404
+ /** 是一个无符号long long 型的毫秒数,返回该文档下,load (en-US)事件被发送时的Unix毫秒时间戳。如果这个事件还未被发送,它的值将会是0。 */
405
+ loadEventStart: 'les',
406
+ /** 是一个无符号long long 型的毫秒数,表征了从同一个浏览器上下文的上一个文档卸载(unload)结束时的UNIX时间戳。如果没有上一个文档,这个值会和PerformanceTiming.fetchStart相同 */
407
+ navigationStart: 'ns',
408
+ /** 是一个无符号long long 型的毫秒数,表征了最后一个HTTP重定向完成时(也就是说是HTTP响应的最后一个比特直接被收到的时间)的UNIX时间戳。如果没有重定向,或者重定向中的一个不同源,这个值会返回0. */
409
+ redirectEnd: 're',
410
+ /** 是一个无符号long long 型的毫秒数,表征了第一个HTTP重定向开始时的UNIX时间戳。如果没有重定向,或者重定向中的一个不同源,这个值会返回0. */
411
+ redirectStart: 'redS',
412
+ /** 是一个无符号long long 型的毫秒数,返回浏览器向服务器发出HTTP请求时(或开始读取本地缓存时)的Unix毫秒时间戳。 */
413
+ requestStart: 'reqS',
414
+ /** 是一个无符号long long 型的毫秒数,返回浏览器从服务器收到(或从本地缓存读取,或从本地资源读取)最后一个字节时(如果在此之前HTTP连接已经关闭,则返回关闭时)的Unix毫秒时间戳。 */
415
+ responseEnd: 'resE',
416
+ /** 是一个无符号long long 型的毫秒数,返回浏览器从服务器收到(或从本地缓存读取)第一个字节时的Unix毫秒时间戳。如果传输层在开始请求之后失败并且连接被重开,该属性将会被数制成新的请求的相对应的发起时间。 */
417
+ responseStart: 'resS',
418
+ /** 是一个无符号long long 型的毫秒数,返回浏览器与服务器开始安全链接的握手时的Unix毫秒时间戳。如果当前网页不要求安全连接,则返回0。 */
419
+ secureConnectionStart: 'scs',
420
+ /** 是一个无符号long long 型的毫秒数,表征了unload (en-US)事件处理完成时的UNIX时间戳。如果没有上一个文档,or if the previous document, or one of the needed redirects, is not of the same origin, 这个值会返回0. */
421
+ unloadEventEnd: 'uee',
422
+ /** 是一个无符号long long 型的毫秒数,表征了unload (en-US)事件抛出时的UNIX时间戳。如果没有上一个文档,or if the previous document, or one of the needed redirects, is not of the same origin, 这个值会返回0. */
423
+ unloadEventStart: 'ees',
424
+ },
425
+ },
426
+ /** api请求相关 */
427
+ req: {
428
+ /** 请求类型,xhr或fetch */
429
+ requestType: 'rT',
430
+ /** 请求地址 */
431
+ responseURL: 'url',
432
+ /** 请求结果 */
433
+ status: 'stat',
434
+ /** 请求耗时 */
435
+ loadTime: 'lT',
436
+ /** 请求状态 */
437
+ statusText: 'sT',
438
+ /** 上报原因, slow或failed */
439
+ reason: 're',
440
+ /** 上报时带上的详细信息 */
441
+ detail: 'de',
442
+ },
443
+ vD: {
444
+ /** 埋点名称 */
445
+ trackName: 'tN',
446
+ /** 埋点参数 */
447
+ params: 'params',
448
+ },
449
+ manualReport: 'mR',
450
+ lifeCycleId: 'li',
451
+ sessionId: 'sI',
452
+ };
453
+ const xhrFunc = (filename, userId, asyncUpdateId, file, res, callback, rest) => {
454
+ const formData = new FormData();
455
+ for (const i in rest) {
456
+ if (Object.prototype.hasOwnProperty.call(rest, i)) {
457
+ formData.append(i, rest[i]);
458
+ }
459
+ }
460
+ formData.append('filename', filename);
461
+ formData.append('asyncUpdateId', asyncUpdateId); //用于上传到oss后异步更新到db的id
462
+ formData.append('userId', userId);
463
+ formData.append('files', file);
464
+ const xhr = new XMLHttpRequest();
465
+ xhr.addEventListener('error', () => res(), false);
466
+ xhr.open('POST', Config.reportUrl);
467
+ xhr.onreadystatechange = function () {
468
+ var _a;
469
+ if (xhr.readyState === 4) {
470
+ if (xhr.status === 200) {
471
+ try {
472
+ const response = xhr.responseText;
473
+ const resp = JSON.parse(response);
474
+ callback((_a = resp.result) === null || _a === void 0 ? void 0 : _a.response);
475
+ }
476
+ catch (_err) {
477
+ //
478
+ }
479
+ res();
480
+ }
481
+ else {
482
+ res();
483
+ }
484
+ }
485
+ };
486
+ xhr.send(formData);
487
+ };
488
+ const ERROR_MESSAGE_MAP = {};
489
+ function reportFunc(data) {
490
+ var _a, _b, _c;
491
+ const payload = convertToSchema(data, reportMap);
492
+ payload.time = new Date().getTime(); // 设置上报时间
493
+ let picturePromise = Promise.resolve();
494
+ let hasErrorEvent = false;
495
+ const filename = uuid.v4();
496
+ if (data.type === 'error') {
497
+ let message = ((_a = data.error) === null || _a === void 0 ? void 0 : _a.message) || 'default';
498
+ if (((_b = data.error) === null || _b === void 0 ? void 0 : _b.subType) === 'resource') {
499
+ message = 'resource';
500
+ }
501
+ if (ERROR_MESSAGE_MAP[message]) ;
502
+ else {
503
+ ERROR_MESSAGE_MAP[message] = true;
504
+ setTimeout(() => {
505
+ //节流效果,每隔3秒上报一次重复的错误录像和错误截图
506
+ delete ERROR_MESSAGE_MAP[message];
507
+ }, 3000);
508
+ const { p, pid, host, bN, pt } = payload;
509
+ if ((_c = data.error) === null || _c === void 0 ? void 0 : _c.events) {
510
+ hasErrorEvent = true;
511
+ const errorEvent = data.error.events;
512
+ const delayTime = Math.ceil(Math.random() * 10000);
513
+ setTimeout(() => {
514
+ const blob = new Blob([errorEvent], { type: 'text/json' });
515
+ return new Promise((res) => {
516
+ xhrFunc(`${filename}.json`, data.uuid, filename, blob, res, () => { }, { p, pid, host, bN, pt });
517
+ });
518
+ }, delayTime < 3500 ? 3500 : delayTime);
519
+ }
520
+ picturePromise = getFullScreenShoot(filename)
521
+ .then((file) => {
522
+ return new Promise((res) => {
523
+ xhrFunc(file.name, data.uuid, filename, file, res, (result) => {
524
+ payload.error.pic = result;
525
+ }, { p, pid, host, bN, pt });
526
+ });
527
+ })
528
+ .catch((err) => {
529
+ payload.error.picError = (err === null || err === void 0 ? void 0 : err.stack) || (err === null || err === void 0 ? void 0 : err.toString());
530
+ });
531
+ }
532
+ }
533
+ return picturePromise
534
+ .then(() => {
535
+ var _a;
536
+ if ((_a = payload.error) === null || _a === void 0 ? void 0 : _a.evt)
537
+ delete payload.error.evt;
538
+ if (window && window.navigator && typeof window.navigator.sendBeacon === 'function') {
539
+ const formData = new FormData();
540
+ Object.keys(payload).forEach((key) => {
541
+ let value = payload[key];
542
+ if (value !== null && value !== undefined) {
543
+ if (typeof value === 'object') {
544
+ value = JSON.stringify(value);
545
+ }
546
+ formData.append(key, value);
547
+ }
548
+ });
549
+ //eventErrorFilename,将错误录像的文件名附在错误上报的埋点里,便于将错误录像上报回调地址异步的更新到db
550
+ if (hasErrorEvent)
551
+ formData.append('eef', filename);
552
+ window.navigator.sendBeacon(Config.reportUrl, formData);
553
+ }
554
+ else {
555
+ if (hasErrorEvent)
556
+ payload.eef = filename;
557
+ new Image().src = `${Config.reportUrl}?${serialize(payload)}`;
558
+ }
559
+ })
560
+ .catch((_err) => {
561
+ //
562
+ });
563
+ }
564
+ // 上报
565
+ function report(data) {
566
+ return new Promise((res) => {
567
+ //在帧的空闲时间上报
568
+ const dataCopy = JSON.parse(JSON.stringify(data));
569
+ if (typeof window.requestIdleCallback === 'function') {
570
+ window.requestIdleCallback(() => reportFunc(dataCopy).then(res), { timeout: 2000 });
571
+ }
572
+ else {
573
+ setTimeout(() => reportFunc(dataCopy).then(res), 0);
574
+ }
575
+ });
576
+ }
577
+ /**
578
+ * @description 将数据转换成最终提交的数据,主要目的是简化数据的key的长度,从而降低荷载大小
579
+ * @param data 要转换的数据
580
+ * @param map 要转换的数据的字段映射
581
+ * @param redundancyData 冗余数据
582
+ */
583
+ function convertToSchema(data, map, redundancyData) {
584
+ const reportData = {};
585
+ if (redundancyData) {
586
+ map = Object.assign(Object.assign({}, map), redundancyData);
587
+ }
588
+ for (const result of Object.entries(map)) {
589
+ const [key, mapKey] = result;
590
+ if (key === 'manualReport') {
591
+ reportData[mapKey] = data[key];
592
+ }
593
+ else {
594
+ //字段值为数组的场景,直接转换
595
+ if (typeof data[key] === 'object' && !Array.isArray(data[key])) {
596
+ if (data[key]) {
597
+ if (redundancyData && Object.prototype.hasOwnProperty.call(redundancyData, key)) {
598
+ reportData[key] = data[key];
599
+ }
600
+ else {
601
+ reportData[key] = convertToSchema(data[key], map[key]);
602
+ }
603
+ }
604
+ else {
605
+ reportData[key] = data[key];
606
+ }
607
+ }
608
+ else {
609
+ // 排除undefined
610
+ if (data[key] !== undefined) {
611
+ reportData[mapKey] = data[key];
612
+ }
613
+ }
614
+ }
615
+ }
616
+ return reportData;
617
+ }
618
+
619
+ /* eslint-disable prefer-rest-params */
620
+ /** 路由栈数组,存储路由变化信息 */
621
+ let routerStack = [];
622
+ /** 路由触发的时间 */
623
+ let time = 0;
624
+ /**
625
+ * @description 派发pushState, replaceState 的监听
626
+ * @param type
627
+ */
628
+ function _history(type) {
629
+ const origin = history[type];
630
+ return function () {
631
+ // @ts-ignore
632
+ const r = origin.apply(this, arguments);
633
+ const e = new Event(type);
634
+ // @ts-ignore
635
+ e.arguments = arguments;
636
+ window.dispatchEvent(e);
637
+ return r;
638
+ };
639
+ }
640
+ /**
641
+ * @description 处理history变化
642
+ * @param e history事件
643
+ */
644
+ function handleHistoryChange(__e) {
645
+ visualTrackFunc();
646
+ setReport();
647
+ }
648
+ /**
649
+ * 设置信息并触发上报
650
+ */
651
+ function setReport() {
652
+ let pageUrl = location.href;
653
+ /**
654
+ * 此判断的逻辑是因为spa且hash模式下,路由搜索参数更改也会触发handleHistoryChange事件
655
+ * 所以路由栈中的信息只保存无搜索参数的地址
656
+ */
657
+ if (Config.hash && Config.spa) {
658
+ const currentUrlSplit = location.href.split('#');
659
+ let currentUrlPostfix = '';
660
+ if (currentUrlSplit[1]) {
661
+ const index = currentUrlSplit[1].indexOf('?');
662
+ if (index > -1) {
663
+ currentUrlPostfix = currentUrlSplit[1].substring(0, index);
664
+ }
665
+ else {
666
+ currentUrlPostfix = currentUrlSplit[1];
667
+ }
668
+ }
669
+ const currentUrlResult = `${currentUrlSplit[0]}#${currentUrlPostfix}`;
670
+ pageUrl = currentUrlResult;
671
+ }
672
+ const { originPage, page } = setRouteStack(pageUrl);
673
+ // 愿页面等于当前页面,说明无路由变更。只有spa且hash模式下才会出现
674
+ if (originPage === page) {
675
+ return;
676
+ }
677
+ const currentTime = new Date().getTime();
678
+ // 页面停留时间
679
+ const stayTime = currentTime - time;
680
+ // 设置触发路由变化的时间
681
+ time = currentTime;
682
+ // 设置信息
683
+ setReportValue('originPage', originPage);
684
+ setReportValue('page', page);
685
+ setReportValue('type', 'pv');
686
+ setReportValue('pv', { stayTime });
687
+ // 上报数据
688
+ report(getReport());
689
+ }
690
+ function setPVTime() {
691
+ time = new Date().getTime();
692
+ }
693
+ /**
694
+ * @description 添加路由栈信息
695
+ * @param page 页面信息
696
+ */
697
+ function setRouteStack(page) {
698
+ if (typeof page === 'string') {
699
+ routerStack.push(page);
700
+ }
701
+ else {
702
+ routerStack = routerStack.concat(page);
703
+ }
704
+ // 路由栈只需保留两个来作为源页面和当前页面
705
+ routerStack = routerStack.slice(-2);
706
+ return {
707
+ originPage: routerStack[0],
708
+ page: routerStack[1] || routerStack[0],
709
+ };
710
+ }
711
+
712
+ var version = "1.10.0";
713
+
714
+ /*
715
+ * @Author: Mark.Zhang
716
+ * @Date: 2020-10-26 15:40:16
717
+ * @Description 全局配置项
718
+ */
719
+ // 上报数据
720
+ let Report = {
721
+ uuid: '',
722
+ projectID: '',
723
+ host: '',
724
+ originPage: '',
725
+ page: '',
726
+ time: 0,
727
+ browserName: '',
728
+ browserVersion: '',
729
+ engineName: '',
730
+ engineVersion: '',
731
+ language: '',
732
+ navigatorVendor: '',
733
+ connectionType: '2g',
734
+ osName: '',
735
+ osVersion: '',
736
+ type: 'init',
737
+ viewport: '',
738
+ screen: '',
739
+ version: '',
740
+ charset: '',
741
+ pageTitle: '',
742
+ referrer: '',
743
+ pv: null,
744
+ ua: null,
745
+ error: null,
746
+ dpr: 1,
747
+ perf: null,
748
+ vD: undefined,
749
+ manualReport: undefined,
750
+ lifeCycleId: uuid.v4(),
751
+ sessionId: getSessionId(),
752
+ };
753
+ /**
754
+ * @description 初始化上报数据并上报
755
+ */
756
+ function initReport() {
757
+ if (document.readyState === 'complete') {
758
+ initReportFunc();
759
+ }
760
+ else {
761
+ // 注意:这里不要使用window.onload,因为一个项目window.onload只能用一次,如果这里用了就会影响宿主项目的功能
762
+ window.addEventListener('load', initReportFunc);
763
+ }
764
+ }
765
+ function initReportFunc() {
766
+ setRouteStack([location.href, location.href]);
767
+ setPVTime();
768
+ Report = Object.assign(Object.assign({}, getReport()), {
769
+ page: location.href,
770
+ originPage: location.href,
771
+ type: 'init',
772
+ perf: performance,
773
+ sessionId: getSessionId(),
774
+ lifeCycleId: uuid.v4(),
775
+ });
776
+ report(Report);
777
+ }
778
+ /**
779
+ * @description 设置Report的值
780
+ * @param key Report key
781
+ * @param value Report 值
782
+ */
783
+ function setReportValue(key, value) {
784
+ if (Object.prototype.hasOwnProperty.call(Report, key)) {
785
+ if (['pv', 'ua', 'error', 'request'].includes(key)) {
786
+ Report = Object.assign(Object.assign({}, Report), {
787
+ pv: null,
788
+ ua: null,
789
+ error: null,
790
+ req: null,
791
+ manualReport: undefined,
792
+ perf: null,
793
+ });
794
+ }
795
+ Report[key] = value;
796
+ }
797
+ }
798
+ /**
799
+ * @description 获取上报数据
800
+ */
801
+ function getReport() {
802
+ const nav = getNavigator();
803
+ const viewport = getViewport();
804
+ return Object.assign(Object.assign(Object.assign({}, Report), nav), {
805
+ version,
806
+ projectID: Config.projectID,
807
+ host: location.host,
808
+ uuid: getUid(),
809
+ viewport,
810
+ screen: `${screen.width} x ${screen.height}`,
811
+ pageTitle: document.title,
812
+ referrer: document.referrer,
813
+ charset: document.charset,
814
+ customPayload: Config.customPayload,
815
+ dpr: window.devicePixelRatio,
816
+ });
817
+ }
818
+ function ajaxEventTrigger(event) {
819
+ const ajaxEvent = new CustomEvent(event, {
820
+ detail: this,
821
+ });
822
+ window.dispatchEvent(ajaxEvent);
823
+ }
824
+ const OldXHR = window.XMLHttpRequest;
825
+ function newXHR() {
826
+ const realXHR = new OldXHR();
827
+ realXHR.addEventListener('loadstart', function () {
828
+ ajaxEventTrigger.call(this, 'ajaxLoadStart');
829
+ }, false);
830
+ realXHR.addEventListener('loadend', function () {
831
+ ajaxEventTrigger.call(this, 'ajaxLoadEnd');
832
+ }, false);
833
+ // 此处的捕获的异常会连日志接口也一起捕获,如果日志上报接口异常了,就会导致死循环了。
834
+ realXHR.onerror = function (e) {
835
+ // eslint-disable-next-line no-console
836
+ console.warn('realXHR.onerror, e', e);
837
+ };
838
+ return realXHR;
839
+ }
840
+ /**
841
+ * 页面接口请求监控
842
+ */
843
+ const tempUrlInfo = {};
844
+ const recordXMLHttpRequestLog = (XMLHttpRequestTimeout) => {
845
+ XMLHttpRequestTimeout = typeof XMLHttpRequestTimeout === 'number' ? XMLHttpRequestTimeout : 1000;
846
+ const timeRecordArray = [];
847
+ window.__XMLHttpRequest__ = window.XMLHttpRequest;
848
+ window.XMLHttpRequest = newXHR;
849
+ window.addEventListener('ajaxLoadStart', function (e) {
850
+ const tempObj = {
851
+ timeStamp: new Date().getTime(),
852
+ event: e,
853
+ };
854
+ timeRecordArray.push(tempObj);
855
+ });
856
+ window.addEventListener('ajaxLoadEnd', function () {
857
+ const timeRecordArrayCopy = [].concat(timeRecordArray);
858
+ for (let i = 0; i < timeRecordArrayCopy.length; i++) {
859
+ if (timeRecordArrayCopy[i].event.detail && timeRecordArrayCopy[i].event.detail.status > 0) {
860
+ const currentTime = new Date().getTime();
861
+ const { responseURL, status, statusText, timeStamp } = timeRecordArrayCopy[i].event.detail;
862
+ const previousTime = timeStamp || timeRecordArrayCopy[i].timeStamp;
863
+ const loadTime = currentTime - previousTime;
864
+ const request = {
865
+ requestType: 'xhr',
866
+ responseURL,
867
+ status,
868
+ loadTime,
869
+ statusText,
870
+ reason: '',
871
+ detail: '',
872
+ };
873
+ if (loadTime && loadTime > XMLHttpRequestTimeout) {
874
+ request.reason = 'slow';
875
+ request.detail = `request is too slow, XMLHttpRequestTimeout: ${XMLHttpRequestTimeout}`;
876
+ }
877
+ else if (status && status >= 300 && statusText && statusText.toLowerCase() !== 'ok') {
878
+ request.reason = 'failed';
879
+ request.detail = `request is failed, status: ${status}, statusText: ${statusText}`;
880
+ }
881
+ else ;
882
+ if (request.reason) {
883
+ if (!tempUrlInfo[responseURL]) {
884
+ tempUrlInfo[responseURL] = true;
885
+ setTimeout(() => {
886
+ delete tempUrlInfo[responseURL];
887
+ }, 10);
888
+ setReportValue('error', null);
889
+ report(Object.assign(Object.assign({}, getReport()), {
890
+ page: location.href,
891
+ originPage: location.href,
892
+ type: 'request',
893
+ req: request,
894
+ }));
895
+ }
896
+ }
897
+ }
898
+ // 当前请求成功后就在数组中移除掉
899
+ timeRecordArray.splice(i, 1);
900
+ }
901
+ });
902
+ };
903
+ const hackFetch = (XMLHttpRequestTimeout) => {
904
+ XMLHttpRequestTimeout = typeof XMLHttpRequestTimeout === 'number' ? XMLHttpRequestTimeout : 1000;
905
+ if (typeof window.fetch === 'function') {
906
+ const __fetch__ = window.fetch;
907
+ window.__fetch__ = __fetch__;
908
+ window.fetch = function (t, ...args) {
909
+ const begin = Date.now();
910
+ //禁用数组的扩展运算符,否则portal的生产环境会报Uncaught TypeError: Object(...) is not a function,
911
+ //编译后的__spreadArray函数有问题,而这个函数来自于tslib.具体报错原因不知.
912
+ //其他平台使用数组的扩展运算符没有问题,比如前端监控平台的管理界面
913
+ const params = [].concat(t).concat(args);
914
+ return __fetch__
915
+ .apply(window, params)
916
+ .then(function (res) {
917
+ const response = res.clone();
918
+ const headers = response.headers;
919
+ if (headers && typeof headers.get === 'function') {
920
+ const ct = headers.get('content-type');
921
+ if (ct && !/(text)|(json)/.test(ct)) {
922
+ return res;
923
+ }
924
+ }
925
+ const loadTime = Date.now() - begin;
926
+ response
927
+ .text()
928
+ .then(function (result) {
929
+ const { url, status, statusText, ok } = response;
930
+ const request = {
931
+ requestType: 'fetch',
932
+ responseURL: url,
933
+ status,
934
+ loadTime,
935
+ statusText,
936
+ reason: '',
937
+ detail: '',
938
+ };
939
+ if (!ok || status >= 300) {
940
+ request.reason = 'failed';
941
+ request.detail = `request is failed, status: ${status}, statusText: ${statusText}`;
942
+ }
943
+ else if (loadTime > XMLHttpRequestTimeout) {
944
+ request.reason = 'slow';
945
+ request.detail = `request is too slow, XMLHttpRequestTimeout: ${XMLHttpRequestTimeout}, result: ${result === null || result === void 0 ? void 0 : result.slice(0, 500)}`;
946
+ }
947
+ if (request.reason) {
948
+ if (!tempUrlInfo[url]) {
949
+ tempUrlInfo[url] = true;
950
+ setTimeout(() => {
951
+ delete tempUrlInfo[url];
952
+ }, 10);
953
+ setReportValue('error', null);
954
+ report(Object.assign(Object.assign({}, getReport()), {
955
+ page: location.href,
956
+ originPage: location.href,
957
+ type: 'request',
958
+ req: request,
959
+ }));
960
+ }
961
+ }
962
+ })
963
+ .catch((err) => {
964
+ // eslint-disable-next-line no-console
965
+ console.log('hackFetch response.text() err', err);
966
+ });
967
+ return res;
968
+ })
969
+ .catch((err) => {
970
+ // eslint-disable-next-line no-console
971
+ console.log('hackFetch err', err);
972
+ });
973
+ };
974
+ }
975
+ };
976
+
977
+ /*
978
+ * @Author: Mark.Zhang
979
+ * @Date: 2020-10-26 11:10:04
980
+ * @Description 用户行为处理方法
981
+ */
982
+ /**
983
+ * @description 点击事件触发后的操作
984
+ */
985
+ function handleClick(event) {
986
+ const target = event.target;
987
+ if (target.nodeName === 'INPUT' || target.nodeName === 'TEXTAREA') {
988
+ return;
989
+ }
990
+ const path = getElmPath(target);
991
+ if (path) {
992
+ setReportValue('type', 'ua');
993
+ setReportValue('ua', {
994
+ subType: 'ui.click',
995
+ x: event.x,
996
+ y: event.y,
997
+ path,
998
+ });
999
+ report(getReport());
1000
+ }
1001
+ }
1002
+ /**
1003
+ * @description 点击触发失焦后的操作
1004
+ */
1005
+ function handleBlur(event) {
1006
+ const target = event.target;
1007
+ if (target.nodeName !== 'INPUT' && target.nodeName !== 'TEXTAREA') {
1008
+ return;
1009
+ }
1010
+ const path = getElmPath(target);
1011
+ if (path) {
1012
+ setReportValue('type', 'ua');
1013
+ setReportValue('ua', {
1014
+ subType: 'ui.blur',
1015
+ x: event.x,
1016
+ y: event.y,
1017
+ path,
1018
+ value: target.value,
1019
+ });
1020
+ report(getReport());
1021
+ }
1022
+ }
1023
+ /**
1024
+ * @description 滚动的操作
1025
+ */
1026
+ let timeout$1;
1027
+ function handleScroll(_event) {
1028
+ clearTimeout(timeout$1);
1029
+ timeout$1 = setTimeout(() => {
1030
+ setReportValue('type', 'ua');
1031
+ setReportValue('ua', {
1032
+ subType: 'ui.scroll',
1033
+ });
1034
+ report(getReport());
1035
+ }, 1000);
1036
+ }
1037
+
1038
+ const parser = new uaParserJs.UAParser();
1039
+ /**
1040
+ * @description 获取uuid
1041
+ */
1042
+ function getUid() {
1043
+ let uid = localStorage.getItem(shuyunTrackId) || '';
1044
+ if (!uid) {
1045
+ uid = uuid.v4();
1046
+ localStorage.setItem(shuyunTrackId, uid);
1047
+ }
1048
+ return uid;
1049
+ }
1050
+ /**
1051
+ * @description 获取session id
1052
+ */
1053
+ function getSessionId() {
1054
+ const sessionId = sessionStorage.getItem(shuyunTrackSessionId);
1055
+ if (sessionId) {
1056
+ return sessionId;
1057
+ }
1058
+ else {
1059
+ const id = uuid.v4();
1060
+ sessionStorage.setItem(shuyunTrackSessionId, id);
1061
+ return id;
1062
+ }
1063
+ }
1064
+ /**
1065
+ * @description 获取浏览器信息
1066
+ */
1067
+ function getNavigator() {
1068
+ var _a;
1069
+ const uaResult = parser.getResult();
1070
+ return {
1071
+ language: navigator.language,
1072
+ navigatorVendor: navigator.vendor,
1073
+ connectionType: ((_a = navigator === null || navigator === void 0 ? void 0 : navigator.connection) === null || _a === void 0 ? void 0 : _a.effectiveType) || '2g',
1074
+ browserName: uaResult.browser.name || '',
1075
+ browserVersion: uaResult.browser.version || '',
1076
+ engineName: uaResult.engine.name || '',
1077
+ engineVersion: uaResult.engine.version || '',
1078
+ osName: uaResult.os.name || '',
1079
+ osVersion: uaResult.os.version || '',
1080
+ };
1081
+ }
1082
+ /**
1083
+ * @description 获取viewport的宽高
1084
+ */
1085
+ function getViewport() {
1086
+ const w = document.documentElement.clientWidth || document.body.clientWidth;
1087
+ const h = document.documentElement.clientHeight || document.body.clientHeight;
1088
+ return `${w} x ${h}`;
1089
+ }
1090
+ /**
1091
+ * @description 获取元素路径,最多保留5层
1092
+ * @param e
1093
+ */
1094
+ const getElmPath = function (target) {
1095
+ if (!target || target.nodeType !== 1) {
1096
+ return '';
1097
+ }
1098
+ const ret = [];
1099
+ // 层数,最多5层
1100
+ let deepLength = 0;
1101
+ // 元素
1102
+ let elm = '';
1103
+ if (typeof target.innerText === 'string') {
1104
+ ret.push(`(${target.innerText.substr(0, 50)})`);
1105
+ }
1106
+ for (let t = target || null; t && deepLength++ < 5 && !((elm = normalTarget(t)) === 'html');) {
1107
+ // eslint-disable-next-line no-sequences
1108
+ ret.push(elm), (t = t.parentNode);
1109
+ }
1110
+ return ret.reverse().join(' > ');
1111
+ };
1112
+ /**
1113
+ * @description 处理html node
1114
+ * @param e
1115
+ */
1116
+ const normalTarget = function (target) {
1117
+ let t, n, r, a, i;
1118
+ const o = [];
1119
+ if (!target || !target.tagName) {
1120
+ return '';
1121
+ }
1122
+ o.push(target.tagName.toLowerCase());
1123
+ if (target.id) {
1124
+ o.push('#'.concat(target.id));
1125
+ }
1126
+ if ((t = target.className) && Object.prototype.toString.call(t) === '[object String]') {
1127
+ for (n = t.split(/\s+/), i = 0; i < n.length; i++) {
1128
+ // className包含active的不加入路径
1129
+ if (n[i].indexOf('active') < 0)
1130
+ o.push('.'.concat(n[i]));
1131
+ }
1132
+ }
1133
+ const s = ['type', 'name', 'title', 'alt'];
1134
+ for (i = 0; i < s.length; i++) {
1135
+ r = s[i];
1136
+ if ((a = target.getAttribute(r))) {
1137
+ o.push('['.concat(r, '="').concat(a, '"]'));
1138
+ }
1139
+ }
1140
+ return o.join('');
1141
+ };
1142
+ /**
1143
+ * @description 序列化对象
1144
+ * @param obj
1145
+ */
1146
+ function serialize(obj) {
1147
+ const str = [];
1148
+ for (const p in obj) {
1149
+ if (Object.prototype.hasOwnProperty.call(obj, p) && typeof obj[p] !== 'undefined') {
1150
+ const value = typeof obj[p] === 'object' ? JSON.stringify(obj[p]) : obj[p];
1151
+ str.push(encodeURIComponent(p) + '=' + encodeURIComponent(value));
1152
+ }
1153
+ }
1154
+ return str.join('&');
1155
+ }
1156
+ /**
1157
+ * @description 注册事件监听
1158
+ * @param event 事件
1159
+ * @param fn 回调方法
1160
+ */
1161
+ const on = function (event, fn) {
1162
+ if (window.addEventListener) {
1163
+ window.addEventListener(event, fn, true);
1164
+ }
1165
+ else {
1166
+ window.attachEvent(`on${event}`, fn);
1167
+ }
1168
+ };
1169
+ /**
1170
+ * @description 移除事件监听
1171
+ * @param event 事件
1172
+ * @param fn 回调方法
1173
+ */
1174
+ const off = function (event, fn) {
1175
+ if (window.removeEventListener) {
1176
+ return window.removeEventListener(event, fn);
1177
+ }
1178
+ else {
1179
+ return window.detachEvent(`on${event}`, fn);
1180
+ }
1181
+ };
1182
+ const formatTrackElementXPath = (xPath) => {
1183
+ const result = [];
1184
+ const array = xPath.split('/');
1185
+ array.forEach((item) => {
1186
+ if (!['svg', 'path', 'g', 'image', 'text', 'line', 'rect', 'polygon', 'circle', 'ellipse'].some((child) => {
1187
+ if (item.toLowerCase().split('[')[0] === child) {
1188
+ result.push(`/*[name()="${child}"]`);
1189
+ return true;
1190
+ }
1191
+ else {
1192
+ return false;
1193
+ }
1194
+ })) {
1195
+ result.push(item);
1196
+ }
1197
+ });
1198
+ return result.join('/');
1199
+ };
1200
+ const formatElementXPath = (info) => {
1201
+ var _a;
1202
+ let elementXPathValue = '';
1203
+ const elementXPath = info.elementXPath;
1204
+ if (elementXPath) {
1205
+ const xpathElement = document.evaluate(formatTrackElementXPath(elementXPath), document).iterateNext();
1206
+ elementXPathValue = xpathElement ? (_a = (xpathElement.textContent || xpathElement.value)) === null || _a === void 0 ? void 0 : _a.trim() : '';
1207
+ }
1208
+ return {
1209
+ elementXPath,
1210
+ elementXPathValue,
1211
+ };
1212
+ };
1213
+ let existTrackListenEvent = {};
1214
+ const elements = [];
1215
+ let timeout;
1216
+ const visualTrackFunc = () => {
1217
+ if (Config.enableVisualTrack) {
1218
+ if (timeout)
1219
+ clearTimeout(timeout);
1220
+ timeout = setTimeout(() => {
1221
+ timeout = null;
1222
+ const url = location.href.split('?')[0];
1223
+ axios__default["default"]
1224
+ .get(`${Config.reportUrl.replace('/s/r', '')}/visual/get_data_list?url=${encodeURIComponent(url)}`)
1225
+ .then((res) => {
1226
+ var _a, _b;
1227
+ const listData = (_b = (_a = res.data.result) === null || _a === void 0 ? void 0 : _a.result) === null || _b === void 0 ? void 0 : _b.dataList;
1228
+ if (Array.isArray(listData)) {
1229
+ try {
1230
+ listData.forEach((item) => {
1231
+ if (item.trackType === 'mount') {
1232
+ visualReportEvent(item);
1233
+ }
1234
+ else {
1235
+ const element = document
1236
+ .evaluate(formatTrackElementXPath(item.trackElementXPath), document)
1237
+ .iterateNext();
1238
+ const key = url + item.trackElementXPath;
1239
+ if (element) {
1240
+ if (!existTrackListenEvent[key]) {
1241
+ existTrackListenEvent[key] = true;
1242
+ elements.push(element);
1243
+ //@ts-ignore
1244
+ element.setAttribute('visual-track-data-attr', JSON.stringify(item));
1245
+ element.addEventListener('click', clickEventFunc);
1246
+ }
1247
+ }
1248
+ else {
1249
+ const func = () => {
1250
+ var _a;
1251
+ const { browserName, browserVersion } = getNavigator();
1252
+ const payload = {
1253
+ projectID: Config.projectID,
1254
+ version,
1255
+ url,
1256
+ trackElementXPath: item.trackElementXPath,
1257
+ trackNameInfo: item.trackNameInfo,
1258
+ params: item.params,
1259
+ customPayload: Config.customPayload,
1260
+ type: 'visual_track_xpath_not_found',
1261
+ browser: `${browserName} - ${browserVersion}`,
1262
+ };
1263
+ if (typeof ((_a = window.navigator) === null || _a === void 0 ? void 0 : _a.sendBeacon) === 'function') {
1264
+ const formData = new FormData();
1265
+ for (const i in payload) {
1266
+ if (Object.prototype.hasOwnProperty.call(payload, i)) {
1267
+ const value = payload[i];
1268
+ formData.append(i, value && typeof value === 'object' ? JSON.stringify(value) : value);
1269
+ }
1270
+ }
1271
+ window.navigator.sendBeacon(Config.reportUrl, formData);
1272
+ }
1273
+ else {
1274
+ new Image().src = `${Config.reportUrl}?${serialize(payload)}`;
1275
+ }
1276
+ };
1277
+ if (typeof window.requestIdleCallback === 'function') {
1278
+ window.requestIdleCallback(() => func(), { timeout: 2000 });
1279
+ }
1280
+ else {
1281
+ setTimeout(() => func(), 0);
1282
+ }
1283
+ }
1284
+ }
1285
+ });
1286
+ }
1287
+ catch (err) {
1288
+ // eslint-disable-next-line no-console
1289
+ console.log('visual track evaluate err', err);
1290
+ }
1291
+ }
1292
+ });
1293
+ }, 1000);
1294
+ }
1295
+ };
1296
+ const visualReportEvent = (item) => {
1297
+ var _a;
1298
+ const { elementXPath, elementXPathValue } = formatElementXPath(item.trackNameInfo);
1299
+ const visualTrackData = {
1300
+ trackType: item.trackType,
1301
+ trackName: elementXPath ? elementXPathValue : item.trackNameInfo.name,
1302
+ params: (_a = item.params) === null || _a === void 0 ? void 0 : _a.map((child) => {
1303
+ const { elementXPath, elementXPathValue } = formatElementXPath(child);
1304
+ return {
1305
+ name: child.name,
1306
+ value: elementXPath ? elementXPathValue : child.value,
1307
+ };
1308
+ }),
1309
+ };
1310
+ setReportValue('type', 'visual');
1311
+ setReportValue('vD', visualTrackData);
1312
+ report(getReport());
1313
+ setReportValue('vD', undefined);
1314
+ };
1315
+ const clickEventFunc = (e) => {
1316
+ //@ts-ignore
1317
+ const item = JSON.parse(e.target.getAttribute('visual-track-data-attr'));
1318
+ visualReportEvent(item);
1319
+ };
1320
+ const initWindowObjectFunction = () => {
1321
+ if (typeof window.manualReportTrackFunc === 'undefined') {
1322
+ window.manualReportTrackFunc = (data) => {
1323
+ if (Object.prototype.toString.call(data) !== '[object Object]') {
1324
+ // eslint-disable-next-line no-console
1325
+ console.warn('manualReportTrackFunc参数必须是一个对象!');
1326
+ }
1327
+ else {
1328
+ setReportValue('type', 'manual');
1329
+ setReportValue('manualReport', data);
1330
+ report(getReport());
1331
+ setReportValue('manualReport', undefined);
1332
+ }
1333
+ };
1334
+ }
1335
+ if (typeof window.getFullScreenShootFunc === 'undefined') {
1336
+ //下载页面截图
1337
+ window.getFullScreenShootFunc = (filename) => __awaiter(void 0, void 0, void 0, function* () {
1338
+ const file = yield getFullScreenShoot(filename);
1339
+ const url = window.URL.createObjectURL(file);
1340
+ const tagA = document.createElement('a');
1341
+ tagA.setAttribute('href', url);
1342
+ tagA.setAttribute('download', file.name);
1343
+ tagA.setAttribute('target', '_blank');
1344
+ document.body.appendChild(tagA);
1345
+ tagA.click();
1346
+ document.body.removeChild(tagA);
1347
+ });
1348
+ }
1349
+ if (typeof window.getRRWebUserEventsCaptureFunc === 'undefined') {
1350
+ window.getRRWebUserEventsCaptureFunc = () => {
1351
+ setReportValue('type', 'error');
1352
+ setReportValue('error', {
1353
+ subType: 'manual',
1354
+ message: 'manual trigger',
1355
+ events: getUserEvents(true),
1356
+ });
1357
+ report(getReport());
1358
+ };
1359
+ }
1360
+ };
1361
+ let prewHref = '';
1362
+ const handleLocationChange = (e) => {
1363
+ const curHref = location.href;
1364
+ // href中`?`后面的值改变,不触发后续动作
1365
+ if (prewHref.split('?')[0] === curHref.split('?')[0]) {
1366
+ return;
1367
+ }
1368
+ prewHref = curHref;
1369
+ handleHistoryChange();
1370
+ elements.forEach((element) => {
1371
+ element === null || element === void 0 ? void 0 : element.removeEventListener('click', clickEventFunc);
1372
+ });
1373
+ //清空可视化埋点之前的数据
1374
+ existTrackListenEvent = {};
1375
+ elements.length = 0;
1376
+ visualTrackFunc();
1377
+ };
1378
+
1379
+ class Track {
1380
+ constructor() {
1381
+ this.visualTrack = () => {
1382
+ window.onload = () => {
1383
+ visualTrackFunc();
1384
+ };
1385
+ };
1386
+ }
1387
+ init(config) {
1388
+ var _a;
1389
+ // 是否开启日志收集
1390
+ if (!config || !config.enable) {
1391
+ return;
1392
+ }
1393
+ // 没有项目ID,则不监听任何事件
1394
+ if (!config.projectID) {
1395
+ // eslint-disable-next-line no-console
1396
+ console.warn('缺少项目ID或token!');
1397
+ return;
1398
+ }
1399
+ // 没有reportUrl
1400
+ if (!config.reportUrl) {
1401
+ // eslint-disable-next-line no-console
1402
+ console.warn('缺少上报地址!');
1403
+ return;
1404
+ }
1405
+ if (this.ignoreUrl(((_a = config === null || config === void 0 ? void 0 : config.ignore) === null || _a === void 0 ? void 0 : _a.urls) || [])) {
1406
+ return;
1407
+ }
1408
+ setConfig(config);
1409
+ initReport();
1410
+ recordXMLHttpRequestLog(config.XMLHttpRequestTimeout);
1411
+ hackFetch(config.XMLHttpRequestTimeout);
1412
+ Config.spa && this.addListenRouterChange();
1413
+ Config.enableBehavior && this.addListenUserActivity();
1414
+ Config.enableError && this.addListenJSUncaught();
1415
+ this.visualTrack();
1416
+ this.addListenUnload();
1417
+ initWindowObjectFunction();
1418
+ }
1419
+ /**
1420
+ * @description 监听错误异常
1421
+ */
1422
+ addListenJSUncaught() {
1423
+ on('error', handleError);
1424
+ on('unhandledrejection', handleError);
1425
+ }
1426
+ /**
1427
+ * @description 监听行为
1428
+ */
1429
+ addListenUserActivity() {
1430
+ on('click', handleClick); // 非输入框点击
1431
+ on('blur', handleBlur); // 输入框失焦
1432
+ on('scroll', handleScroll); // 输入框失焦
1433
+ }
1434
+ /**
1435
+ * @description 监听路由变化
1436
+ */
1437
+ addListenRouterChange() {
1438
+ on('popstate', handleLocationChange);
1439
+ // on('hashchange', pv.handleHashChange); //触发hashchange的同时也会触发popstate
1440
+ if (Config.spa && !Config.hash) {
1441
+ history.pushState = _history('pushState');
1442
+ history.replaceState = _history('replaceState');
1443
+ on('pushState', handleLocationChange);
1444
+ on('replaceState', handleLocationChange);
1445
+ }
1446
+ }
1447
+ /**
1448
+ * @description 页面将要关闭
1449
+ */
1450
+ addListenUnload() {
1451
+ on('beforeunload', () => {
1452
+ this.destroy();
1453
+ });
1454
+ }
1455
+ /**
1456
+ * 忽略的url
1457
+ * @param urls
1458
+ */
1459
+ ignoreUrl(urls) {
1460
+ const someUrl = urls.some((url) => location.href.includes(url));
1461
+ return someUrl;
1462
+ }
1463
+ /**
1464
+ * @description 销毁监听器
1465
+ */
1466
+ destroy() {
1467
+ if (Config.spa) {
1468
+ // off('hashchange', pv.handleHashChange);
1469
+ off('pushState', handleHistoryChange);
1470
+ off('popstate', handleHistoryChange);
1471
+ off('replaceState', handleHistoryChange);
1472
+ }
1473
+ if (Config.enableBehavior) {
1474
+ off('click', handleClick);
1475
+ off('blur', handleBlur);
1476
+ off('scroll', handleScroll);
1477
+ }
1478
+ if (Config.enableError) {
1479
+ off('error', handleError);
1480
+ }
1481
+ sessionStorage.removeItem(shuyunTrackSessionId);
1482
+ }
1483
+ }
1484
+
1485
+ module.exports = Track;