lee_web_track_sdk 0.0.1

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,1074 @@
1
+ function _array_like_to_array(arr, len) {
2
+ if (len == null || len > arr.length) len = arr.length;
3
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
4
+ return arr2;
5
+ }
6
+ function _array_without_holes(arr) {
7
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
8
+ }
9
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
10
+ try {
11
+ var info = gen[key](arg);
12
+ var value = info.value;
13
+ } catch (error) {
14
+ reject(error);
15
+ return;
16
+ }
17
+ if (info.done) {
18
+ resolve(value);
19
+ } else {
20
+ Promise.resolve(value).then(_next, _throw);
21
+ }
22
+ }
23
+ function _async_to_generator(fn) {
24
+ return function() {
25
+ var self = this, args = arguments;
26
+ return new Promise(function(resolve, reject) {
27
+ var gen = fn.apply(self, args);
28
+ function _next(value) {
29
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
30
+ }
31
+ function _throw(err) {
32
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
33
+ }
34
+ _next(undefined);
35
+ });
36
+ };
37
+ }
38
+ function _class_call_check(instance, Constructor) {
39
+ if (!(instance instanceof Constructor)) {
40
+ throw new TypeError("Cannot call a class as a function");
41
+ }
42
+ }
43
+ function _defineProperties(target, props) {
44
+ for(var i = 0; i < props.length; i++){
45
+ var descriptor = props[i];
46
+ descriptor.enumerable = descriptor.enumerable || false;
47
+ descriptor.configurable = true;
48
+ if ("value" in descriptor) descriptor.writable = true;
49
+ Object.defineProperty(target, descriptor.key, descriptor);
50
+ }
51
+ }
52
+ function _create_class(Constructor, protoProps, staticProps) {
53
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
54
+ if (staticProps) _defineProperties(Constructor, staticProps);
55
+ return Constructor;
56
+ }
57
+ function _iterable_to_array(iter) {
58
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
59
+ }
60
+ function _non_iterable_spread() {
61
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
62
+ }
63
+ function _to_consumable_array(arr) {
64
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
65
+ }
66
+ function _unsupported_iterable_to_array(o, minLen) {
67
+ if (!o) return;
68
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
69
+ var n = Object.prototype.toString.call(o).slice(8, -1);
70
+ if (n === "Object" && o.constructor) n = o.constructor.name;
71
+ if (n === "Map" || n === "Set") return Array.from(n);
72
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
73
+ }
74
+ function _ts_generator(thisArg, body) {
75
+ var f, y, t, _ = {
76
+ label: 0,
77
+ sent: function() {
78
+ if (t[0] & 1) throw t[1];
79
+ return t[1];
80
+ },
81
+ trys: [],
82
+ ops: []
83
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
84
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
85
+ return this;
86
+ }), g;
87
+ function verb(n) {
88
+ return function(v) {
89
+ return step([
90
+ n,
91
+ v
92
+ ]);
93
+ };
94
+ }
95
+ function step(op) {
96
+ if (f) throw new TypeError("Generator is already executing.");
97
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
98
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
99
+ if (y = 0, t) op = [
100
+ op[0] & 2,
101
+ t.value
102
+ ];
103
+ switch(op[0]){
104
+ case 0:
105
+ case 1:
106
+ t = op;
107
+ break;
108
+ case 4:
109
+ _.label++;
110
+ return {
111
+ value: op[1],
112
+ done: false
113
+ };
114
+ case 5:
115
+ _.label++;
116
+ y = op[1];
117
+ op = [
118
+ 0
119
+ ];
120
+ continue;
121
+ case 7:
122
+ op = _.ops.pop();
123
+ _.trys.pop();
124
+ continue;
125
+ default:
126
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
127
+ _ = 0;
128
+ continue;
129
+ }
130
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
131
+ _.label = op[1];
132
+ break;
133
+ }
134
+ if (op[0] === 6 && _.label < t[1]) {
135
+ _.label = t[1];
136
+ t = op;
137
+ break;
138
+ }
139
+ if (t && _.label < t[2]) {
140
+ _.label = t[2];
141
+ _.ops.push(op);
142
+ break;
143
+ }
144
+ if (t[2]) _.ops.pop();
145
+ _.trys.pop();
146
+ continue;
147
+ }
148
+ op = body.call(thisArg, _);
149
+ } catch (e) {
150
+ op = [
151
+ 6,
152
+ e
153
+ ];
154
+ y = 0;
155
+ } finally{
156
+ f = t = 0;
157
+ }
158
+ if (op[0] & 5) throw op[1];
159
+ return {
160
+ value: op[0] ? op[1] : void 0,
161
+ done: true
162
+ };
163
+ }
164
+ }
165
+ // src/index.ts
166
+ import * as localForage3 from "localforage";
167
+ // src/core/track/cache.ts
168
+ import * as localForage2 from "localforage";
169
+ // src/config/constant.ts
170
+ var EVENT_TYPES = [
171
+ "page_view",
172
+ "user_view",
173
+ "page_destroy",
174
+ "click_event",
175
+ "module_click",
176
+ "module_view",
177
+ "login_event",
178
+ "order_event",
179
+ "custom_event",
180
+ "log_event"
181
+ ];
182
+ var CAN_AGGREGATION_TYPES = [
183
+ "page_view",
184
+ "user_view",
185
+ "module_view"
186
+ ];
187
+ var EVENT_KEYS = [
188
+ "project_name",
189
+ "event_type",
190
+ "event_name",
191
+ "event_time",
192
+ "user_timezone",
193
+ "user_id",
194
+ "device_type",
195
+ "app_version",
196
+ "user_agent",
197
+ "screen_resolution",
198
+ "custom_data"
199
+ ];
200
+ var CACHE_DATA_NAME = "fr-track-cache-data";
201
+ var CACHE_ERR_DATA_NAME = "fr-track-cache-err-data";
202
+ // src/utils/utils.ts
203
+ var getVariableType = function(variable) {
204
+ var typeString = Object.prototype.toString.call(variable);
205
+ return typeString.slice(8, -1).toLowerCase();
206
+ };
207
+ var generateFallbackUUID = function() {
208
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
209
+ var r = Math.random() * 16 | 0;
210
+ var v = c === "x" ? r : r & 3 | 8;
211
+ return v.toString(16);
212
+ });
213
+ };
214
+ var generateUserId = function() {
215
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
216
+ return crypto.randomUUID();
217
+ }
218
+ return generateFallbackUUID();
219
+ };
220
+ var overrideHistoryMethods = function(type) {
221
+ var originalMethod = history[type];
222
+ return function overrideHistoryMethods() {
223
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
224
+ args[_key] = arguments[_key];
225
+ }
226
+ var res = originalMethod.apply(this, args);
227
+ var e = new Event(type);
228
+ window.dispatchEvent(e);
229
+ return res;
230
+ };
231
+ };
232
+ var getUserTimeZone = function() {
233
+ try {
234
+ return Intl.DateTimeFormat().resolvedOptions().timeZone;
235
+ } catch (error) {
236
+ console.warn("\u65E0\u6CD5\u83B7\u53D6\u65F6\u533A\u540D\u79F0\uFF0C\u4F7F\u7528\u504F\u79FB\u91CF\u63A8\u7B97:", error);
237
+ var offset = /* @__PURE__ */ new Date().getTimezoneOffset();
238
+ if (offset >= 0) {
239
+ return "西".concat(offset / 60, "区");
240
+ } else {
241
+ return "东".concat(-offset / 60, "区");
242
+ }
243
+ }
244
+ };
245
+ // src/utils/validator.ts
246
+ function validateEventType(eventType) {
247
+ return typeof eventType === "string" && EVENT_TYPES.includes(eventType);
248
+ }
249
+ function validateEventKey(params) {
250
+ var type = getVariableType(params);
251
+ if (!params || type !== "object") {
252
+ console.warn("[Fr-track warning]: Event params must be an object");
253
+ return false;
254
+ }
255
+ if (typeof params[Symbol.iterator] !== "function") {
256
+ console.warn("[Fr-track warning]: Event params must be an iterable object");
257
+ return false;
258
+ }
259
+ var keys = Object.keys(params);
260
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
261
+ try {
262
+ for(var _iterator = keys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
263
+ var key = _step.value;
264
+ if (!EVENT_KEYS.includes(key)) {
265
+ console.warn("[Fr-track warning]: Invalid event key: ".concat(key));
266
+ return false;
267
+ }
268
+ }
269
+ } catch (err) {
270
+ _didIteratorError = true;
271
+ _iteratorError = err;
272
+ } finally{
273
+ try {
274
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
275
+ _iterator.return();
276
+ }
277
+ } finally{
278
+ if (_didIteratorError) {
279
+ throw _iteratorError;
280
+ }
281
+ }
282
+ }
283
+ return true;
284
+ }
285
+ // src/core/report/clean.ts
286
+ var cleanData = function(rawData) {
287
+ try {
288
+ var tempIds = /* @__PURE__ */ new Set();
289
+ var resultData = rawData.filter(function(rawItem) {
290
+ var eventKeyResult = validateEventKey(rawItem);
291
+ if (!eventKeyResult) {
292
+ return false;
293
+ }
294
+ var eventTypeResult = validateEventType(rawItem.event_type);
295
+ if (!eventTypeResult) {
296
+ console.warn("[Fr-track warning]: error event type");
297
+ return false;
298
+ }
299
+ var eventTime = rawItem.event_time;
300
+ var minDateTime = /* @__PURE__ */ new Date("2025-12-01").getTime();
301
+ if (!eventTime || typeof eventTime !== "number" || eventTime < minDateTime) {
302
+ return false;
303
+ }
304
+ var id = "".concat(rawItem.event_type, "-").concat(rawItem.event_name, "-").concat(rawItem.event_time);
305
+ if (tempIds.has(id)) {
306
+ return false;
307
+ } else {
308
+ tempIds.add(id);
309
+ }
310
+ return true;
311
+ });
312
+ return {
313
+ type: "success",
314
+ data: resultData
315
+ };
316
+ } catch (error) {
317
+ console.warn("[Fr-track warning]: cleanData error", error);
318
+ var errReason = error.message || "Unknown error";
319
+ return {
320
+ type: "fail",
321
+ errReason: errReason,
322
+ data: rawData
323
+ };
324
+ }
325
+ };
326
+ // src/core/report/aggregation.ts
327
+ var aggregationData = function(rawData) {
328
+ try {
329
+ var _loop = function(i) {
330
+ var item = rawData[i];
331
+ if (CAN_AGGREGATION_TYPES.includes(item.event_type) || item.is_aggregation) {
332
+ var currentDate = new Date(item.event_time).setHours(0, 0, 0, 0);
333
+ var resultItem = resultData.find(function(r) {
334
+ return r.event_time === currentDate && r.event_type === item.event_type && r.event_name === item.event_name;
335
+ });
336
+ if (!resultItem) {
337
+ item.event_time = currentDate;
338
+ resultData.push(item);
339
+ } else {
340
+ var customData = resultItem.custom_data;
341
+ if (customData && customData.count) {
342
+ customData.count += 1;
343
+ resultItem.custom_data = customData;
344
+ }
345
+ }
346
+ } else {
347
+ resultData.push(item);
348
+ }
349
+ };
350
+ var resultData = [];
351
+ for(var i = 0; i < rawData.length; i++)_loop(i);
352
+ return resultData;
353
+ } catch (error) {
354
+ console.warn("[Fr-track warn]: \u805A\u5408\u5931\u8D25", error);
355
+ return rawData;
356
+ }
357
+ };
358
+ // src/core/report/index.ts
359
+ import * as localForage from "localforage";
360
+ var request = function(url, data) {
361
+ return _async_to_generator(function() {
362
+ return _ts_generator(this, function(_state) {
363
+ return [
364
+ 2,
365
+ new Promise(function(resolve, reject) {
366
+ if ("sendBeacon" in navigator) {
367
+ var dataJson = JSON.stringify(data);
368
+ var blob = new Blob([
369
+ dataJson
370
+ ], {
371
+ type: "application/json"
372
+ });
373
+ var status = navigator.sendBeacon(url, blob);
374
+ if (status) {
375
+ resolve(true);
376
+ } else {
377
+ reject();
378
+ }
379
+ } else {
380
+ var xhr = new XMLHttpRequest();
381
+ xhr.open("POST", url, true);
382
+ xhr.setRequestHeader("Content-Type", "application/json");
383
+ xhr.onload = function() {
384
+ if (xhr.status === 200) {
385
+ resolve(xhr.response);
386
+ } else {
387
+ reject();
388
+ }
389
+ };
390
+ xhr.onerror = function() {
391
+ reject();
392
+ };
393
+ xhr.send(JSON.stringify(data));
394
+ }
395
+ })
396
+ ];
397
+ });
398
+ })();
399
+ };
400
+ var onReportFail = function(url, data, clearCacheKey, retryCount) {
401
+ return _async_to_generator(function() {
402
+ var reportErrorData, _reportErrorData;
403
+ return _ts_generator(this, function(_state) {
404
+ switch(_state.label){
405
+ case 0:
406
+ if (!(retryCount < 3)) return [
407
+ 3,
408
+ 2
409
+ ];
410
+ return [
411
+ 4,
412
+ reportDataRequest(url, data, clearCacheKey, retryCount)
413
+ ];
414
+ case 1:
415
+ _state.sent();
416
+ return [
417
+ 3,
418
+ 5
419
+ ];
420
+ case 2:
421
+ return [
422
+ 4,
423
+ localForage.getItem(CACHE_ERR_DATA_NAME)
424
+ ];
425
+ case 3:
426
+ reportErrorData = _state.sent();
427
+ if (reportErrorData) {
428
+ ;
429
+ (_reportErrorData = reportErrorData).push.apply(_reportErrorData, _to_consumable_array(data));
430
+ } else {
431
+ reportErrorData = data;
432
+ }
433
+ return [
434
+ 4,
435
+ localForage.setItem(CACHE_ERR_DATA_NAME, reportErrorData)
436
+ ];
437
+ case 4:
438
+ _state.sent();
439
+ _state.label = 5;
440
+ case 5:
441
+ return [
442
+ 2
443
+ ];
444
+ }
445
+ });
446
+ })();
447
+ };
448
+ var reportDataRequest = function(url, data) {
449
+ var clearCacheKey = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : "", retryCount = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : 1;
450
+ return _async_to_generator(function() {
451
+ var reportData, error;
452
+ return _ts_generator(this, function(_state) {
453
+ switch(_state.label){
454
+ case 0:
455
+ reportData = Array.isArray(data) ? data : [
456
+ data
457
+ ];
458
+ _state.label = 1;
459
+ case 1:
460
+ _state.trys.push([
461
+ 1,
462
+ 5,
463
+ ,
464
+ 6
465
+ ]);
466
+ return [
467
+ 4,
468
+ request(url, reportData)
469
+ ];
470
+ case 2:
471
+ _state.sent();
472
+ if (!clearCacheKey) return [
473
+ 3,
474
+ 4
475
+ ];
476
+ return [
477
+ 4,
478
+ localForage.removeItem(clearCacheKey)
479
+ ];
480
+ case 3:
481
+ _state.sent();
482
+ _state.label = 4;
483
+ case 4:
484
+ return [
485
+ 3,
486
+ 6
487
+ ];
488
+ case 5:
489
+ error = _state.sent();
490
+ onReportFail(url, reportData, clearCacheKey, retryCount + 1);
491
+ return [
492
+ 3,
493
+ 6
494
+ ];
495
+ case 6:
496
+ return [
497
+ 2
498
+ ];
499
+ }
500
+ });
501
+ })();
502
+ };
503
+ var reportCacheData = function(url) {
504
+ return _async_to_generator(function() {
505
+ var cacheData, cleanInfo, result, error;
506
+ return _ts_generator(this, function(_state) {
507
+ switch(_state.label){
508
+ case 0:
509
+ _state.trys.push([
510
+ 0,
511
+ 4,
512
+ ,
513
+ 5
514
+ ]);
515
+ return [
516
+ 4,
517
+ getCacheData()
518
+ ];
519
+ case 1:
520
+ cacheData = _state.sent();
521
+ if (!(cacheData === null || cacheData === void 0 ? void 0 : cacheData.length)) return [
522
+ 2
523
+ ];
524
+ cleanInfo = cleanData(cacheData);
525
+ if (!(cleanInfo.type !== "success")) return [
526
+ 3,
527
+ 3
528
+ ];
529
+ console.warn("[Fr-track warn]:", cleanInfo.errReason);
530
+ return [
531
+ 4,
532
+ localForage.removeItem(CACHE_DATA_NAME)
533
+ ];
534
+ case 2:
535
+ _state.sent();
536
+ return [
537
+ 2
538
+ ];
539
+ case 3:
540
+ result = aggregationData(cleanInfo.data);
541
+ if (result && result.length) {
542
+ reportDataRequest(url, result, CACHE_DATA_NAME);
543
+ }
544
+ return [
545
+ 3,
546
+ 5
547
+ ];
548
+ case 4:
549
+ error = _state.sent();
550
+ console.error("[Fr-track error]:", error);
551
+ return [
552
+ 3,
553
+ 5
554
+ ];
555
+ case 5:
556
+ return [
557
+ 2
558
+ ];
559
+ }
560
+ });
561
+ })();
562
+ };
563
+ // src/core/track/cache.ts
564
+ var setCacheData = function(data, url, maxLen) {
565
+ var retryCount = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : 1;
566
+ return _async_to_generator(function() {
567
+ var cacheData, error, errMsg;
568
+ return _ts_generator(this, function(_state) {
569
+ switch(_state.label){
570
+ case 0:
571
+ _state.trys.push([
572
+ 0,
573
+ 3,
574
+ ,
575
+ 4
576
+ ]);
577
+ return [
578
+ 4,
579
+ localForage2.getItem(CACHE_DATA_NAME)
580
+ ];
581
+ case 1:
582
+ cacheData = _state.sent();
583
+ if (cacheData) {
584
+ cacheData.push(data);
585
+ cacheData.length >= maxLen && reportCacheData(url);
586
+ } else {
587
+ cacheData = [
588
+ data
589
+ ];
590
+ }
591
+ return [
592
+ 4,
593
+ localForage2.setItem(CACHE_DATA_NAME, cacheData)
594
+ ];
595
+ case 2:
596
+ _state.sent();
597
+ return [
598
+ 3,
599
+ 4
600
+ ];
601
+ case 3:
602
+ error = _state.sent();
603
+ errMsg = retryCount > 3 ? error : "Retrying...";
604
+ if (retryCount <= 3) {
605
+ console.warn("[Fr-track warning]:" + errMsg);
606
+ setCacheData(data, url, retryCount + 1);
607
+ } else {
608
+ console.error("[Fr-track error]:" + errMsg);
609
+ reportDataRequest(url, data);
610
+ }
611
+ return [
612
+ 3,
613
+ 4
614
+ ];
615
+ case 4:
616
+ return [
617
+ 2
618
+ ];
619
+ }
620
+ });
621
+ })();
622
+ };
623
+ var getCacheData = function() {
624
+ return _async_to_generator(function() {
625
+ var cacheData, error;
626
+ return _ts_generator(this, function(_state) {
627
+ switch(_state.label){
628
+ case 0:
629
+ _state.trys.push([
630
+ 0,
631
+ 2,
632
+ ,
633
+ 3
634
+ ]);
635
+ return [
636
+ 4,
637
+ localForage2.getItem(CACHE_DATA_NAME)
638
+ ];
639
+ case 1:
640
+ cacheData = _state.sent();
641
+ return [
642
+ 2,
643
+ cacheData || []
644
+ ];
645
+ case 2:
646
+ error = _state.sent();
647
+ console.error("[Fr-track error]:" + error);
648
+ return [
649
+ 2,
650
+ []
651
+ ];
652
+ case 3:
653
+ return [
654
+ 2
655
+ ];
656
+ }
657
+ });
658
+ })();
659
+ };
660
+ // src/utils/uuid.ts
661
+ var getUserId = function() {
662
+ var track_uuid = localStorage.getItem("track_uuid");
663
+ if (track_uuid) {
664
+ return track_uuid;
665
+ }
666
+ var uuid = generateUserId();
667
+ localStorage.setItem("track_uuid", uuid);
668
+ return uuid;
669
+ };
670
+ // src/utils/browser.ts
671
+ import * as Browser from "bowser";
672
+ var getBrowserAndDeviceInfo = function() {
673
+ var defRes = {
674
+ browserName: "unknown",
675
+ browserVersion: "unknown",
676
+ osName: "unknown",
677
+ osVersion: "unknown",
678
+ platformName: "unknown"
679
+ };
680
+ try {
681
+ var ua = window.navigator.userAgent;
682
+ if (!ua) return defRes;
683
+ var info = Browser.parse(ua);
684
+ var browser = info.browser, os = info.os, platform = info.platform;
685
+ return {
686
+ browserName: browser.name || "unknown",
687
+ browserVersion: browser.version || "unknown",
688
+ osName: os.name || "unknown",
689
+ osVersion: os.version || "unknown",
690
+ platformName: platform.type || "unknown"
691
+ };
692
+ } catch (error) {
693
+ return defRes;
694
+ }
695
+ };
696
+ var getDeviceResolution = function() {
697
+ if (!window) return "unknown";
698
+ var screen = window.screen;
699
+ return "".concat(screen.width, "x").concat(screen.height);
700
+ };
701
+ // src/index.ts
702
+ var FrTrack = /*#__PURE__*/ function() {
703
+ "use strict";
704
+ function FrTrack(options) {
705
+ _class_call_check(this, FrTrack);
706
+ this.option = {
707
+ project_name: "",
708
+ report_url: "",
709
+ app_version: "",
710
+ auto_pv: false,
711
+ maxCacheSize: 15,
712
+ maxCacheTime: 3e5
713
+ };
714
+ this.userId = getUserId();
715
+ this.routeMode = "history";
716
+ this.timer = null;
717
+ this.uvTimer = null;
718
+ this.browserInfo = null;
719
+ var project_name = options.project_name, report_url = options.report_url, _options_app_version = options.app_version, app_version = _options_app_version === void 0 ? "unknown" : _options_app_version, _options_auto_pv = options.auto_pv, auto_pv = _options_auto_pv === void 0 ? false : _options_auto_pv, _options_maxCacheTime = options.maxCacheTime, maxCacheTime = _options_maxCacheTime === void 0 ? 3e5 : _options_maxCacheTime, _options_maxCacheSize = options.maxCacheSize, maxCacheSize = _options_maxCacheSize === void 0 ? 15 : _options_maxCacheSize;
720
+ Object.assign(this.option, {
721
+ project_name: project_name,
722
+ report_url: report_url,
723
+ auto_pv: auto_pv,
724
+ maxCacheTime: maxCacheTime,
725
+ maxCacheSize: maxCacheSize,
726
+ app_version: app_version
727
+ });
728
+ this.init();
729
+ }
730
+ _create_class(FrTrack, [
731
+ {
732
+ key: "reportPrevData",
733
+ value: function reportPrevData() {
734
+ return _async_to_generator(function() {
735
+ var res, res1, err;
736
+ return _ts_generator(this, function(_state) {
737
+ switch(_state.label){
738
+ case 0:
739
+ _state.trys.push([
740
+ 0,
741
+ 3,
742
+ ,
743
+ 4
744
+ ]);
745
+ return [
746
+ 4,
747
+ localForage3.getItem(CACHE_DATA_NAME)
748
+ ];
749
+ case 1:
750
+ res = _state.sent();
751
+ return [
752
+ 4,
753
+ localForage3.getItem(CACHE_ERR_DATA_NAME)
754
+ ];
755
+ case 2:
756
+ res1 = _state.sent();
757
+ if (res && res.length) {
758
+ reportCacheData(this.option.report_url);
759
+ }
760
+ if (res1 && res1.length) {
761
+ reportDataRequest(this.option.report_url, res1, CACHE_ERR_DATA_NAME);
762
+ }
763
+ return [
764
+ 3,
765
+ 4
766
+ ];
767
+ case 3:
768
+ err = _state.sent();
769
+ console.warn("[Fr-track warn]: \u68C0\u9A8C\u5386\u53F2\u6570\u636E\u5931\u8D25");
770
+ return [
771
+ 3,
772
+ 4
773
+ ];
774
+ case 4:
775
+ return [
776
+ 2
777
+ ];
778
+ }
779
+ });
780
+ }).call(this);
781
+ }
782
+ },
783
+ {
784
+ key: "loopReport",
785
+ value: function loopReport() {
786
+ var _this = this;
787
+ this.timer = setInterval(function() {
788
+ reportCacheData(_this.option.report_url);
789
+ }, this.option.maxCacheTime);
790
+ }
791
+ },
792
+ {
793
+ key: "onBrowserClose",
794
+ value: function onBrowserClose() {
795
+ var _this = this;
796
+ window.addEventListener("beforeunload", function() {
797
+ if (_this.timer) {
798
+ clearInterval(_this.timer);
799
+ _this.timer = null;
800
+ }
801
+ if (_this.uvTimer) {
802
+ clearInterval(_this.uvTimer);
803
+ _this.uvTimer = null;
804
+ }
805
+ reportCacheData(_this.option.report_url);
806
+ });
807
+ window.addEventListener("visibilitychange", function() {
808
+ if (document.visibilityState !== "visible") {
809
+ _this.loopReport();
810
+ } else {
811
+ if (_this.timer) {
812
+ clearInterval(_this.timer);
813
+ _this.timer = null;
814
+ }
815
+ reportCacheData(_this.option.report_url);
816
+ }
817
+ });
818
+ }
819
+ },
820
+ {
821
+ key: "init",
822
+ value: function init() {
823
+ this.browserInfo = getBrowserAndDeviceInfo();
824
+ this.reportPrevData();
825
+ this.loopReport();
826
+ this.onBrowserClose();
827
+ this.frUserView();
828
+ if (this.option.auto_pv) {
829
+ if (this.routeMode === "history") {
830
+ window.history.pushState = overrideHistoryMethods("pushState");
831
+ window.history.replaceState = overrideHistoryMethods("replaceState");
832
+ }
833
+ this.frAutoPageView();
834
+ }
835
+ }
836
+ },
837
+ {
838
+ key: "track",
839
+ value: function track(params) {
840
+ var _this_option = this.option, project_name = _this_option.project_name, app_version = _this_option.app_version, maxCacheSize = _this_option.maxCacheSize, report_url = _this_option.report_url;
841
+ var _this_browserInfo = this.browserInfo, osName = _this_browserInfo.osName, osVersion = _this_browserInfo.osVersion, browserName = _this_browserInfo.browserName, browserVersion = _this_browserInfo.browserVersion, platformName = _this_browserInfo.platformName;
842
+ var trackParams = Object.assign({
843
+ project_name: project_name,
844
+ app_version: app_version,
845
+ user_id: this.userId,
846
+ device_type: platformName,
847
+ user_agent: "os: ".concat(osName, "--v").concat(osVersion, ", browser: ").concat(browserName, "--v").concat(browserVersion),
848
+ screen_resolution: getDeviceResolution(),
849
+ user_timezone: getUserTimeZone(),
850
+ event_time: Date.now(),
851
+ custom_data: null
852
+ }, params);
853
+ setCacheData(trackParams, report_url, maxCacheSize);
854
+ }
855
+ },
856
+ {
857
+ key: "singleTrack",
858
+ value: function singleTrack(eventType, params) {
859
+ var baseParams = {
860
+ event_type: eventType
861
+ };
862
+ var trackParams = Object.assign(baseParams, params);
863
+ this.track(trackParams);
864
+ }
865
+ },
866
+ {
867
+ /**
868
+ * @method 自定义用户id
869
+ */ key: "setUser",
870
+ value: function setUser(id) {
871
+ this.userId = id;
872
+ }
873
+ },
874
+ {
875
+ /**
876
+ * @method 自定义路由方式
877
+ */ key: "setRouteMode",
878
+ value: function setRouteMode(mode) {
879
+ this.routeMode = mode;
880
+ }
881
+ },
882
+ {
883
+ /**
884
+ * @method 模块view
885
+ */ key: "frModuleView",
886
+ value: function frModuleView(event_name, custom_data) {
887
+ var params = {
888
+ event_name: event_name,
889
+ custom_data: custom_data
890
+ };
891
+ this.singleTrack("module_view", params);
892
+ }
893
+ },
894
+ {
895
+ /**
896
+ * @method 模块click
897
+ */ key: "frModuleClick",
898
+ value: function frModuleClick(event_name, custom_data) {
899
+ var is_aggregation = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
900
+ var params = {
901
+ event_name: event_name,
902
+ custom_data: custom_data,
903
+ is_aggregation: is_aggregation
904
+ };
905
+ this.singleTrack("module_click", params);
906
+ }
907
+ },
908
+ {
909
+ /**
910
+ * @method 登录事件
911
+ */ key: "frLoginEvent",
912
+ value: function frLoginEvent(event_name, custom_data) {
913
+ var is_aggregation = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
914
+ var params = {
915
+ event_name: event_name,
916
+ custom_data: custom_data,
917
+ is_aggregation: is_aggregation
918
+ };
919
+ this.singleTrack("login_event", params);
920
+ }
921
+ },
922
+ {
923
+ /**
924
+ * @method 生单事件
925
+ */ key: "frOrderEvent",
926
+ value: function frOrderEvent(event_name, custom_data) {
927
+ var is_aggregation = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
928
+ var params = {
929
+ event_name: event_name,
930
+ custom_data: custom_data,
931
+ is_aggregation: is_aggregation
932
+ };
933
+ this.singleTrack("order_event", params);
934
+ }
935
+ },
936
+ {
937
+ /**
938
+ * @method 自定义事件
939
+ */ key: "frCustomEvent",
940
+ value: function frCustomEvent(event_name, custom_data) {
941
+ var is_aggregation = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
942
+ var params = {
943
+ event_name: event_name,
944
+ custom_data: custom_data,
945
+ is_aggregation: is_aggregation
946
+ };
947
+ this.singleTrack("custom_event", params);
948
+ }
949
+ },
950
+ {
951
+ /**
952
+ * @method 日志事件
953
+ */ key: "frLogEvent",
954
+ value: function frLogEvent(event_name, custom_data) {
955
+ var is_aggregation = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
956
+ var params = {
957
+ event_name: event_name,
958
+ custom_data: custom_data,
959
+ is_aggregation: is_aggregation
960
+ };
961
+ this.singleTrack("log_event", params);
962
+ }
963
+ },
964
+ {
965
+ /**
966
+ * @method 点击事件
967
+ */ key: "frClickEvent",
968
+ value: function frClickEvent(event_name, custom_data) {
969
+ var is_aggregation = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
970
+ var params = {
971
+ event_name: event_name,
972
+ custom_data: custom_data,
973
+ is_aggregation: is_aggregation
974
+ };
975
+ this.singleTrack("click_event", params);
976
+ }
977
+ },
978
+ {
979
+ /**
980
+ * @method 自动收集pv事件
981
+ */ key: "frAutoPageView",
982
+ value: function frAutoPageView() {
983
+ var _this = this;
984
+ var methodsList = [];
985
+ if (this.routeMode === "hash") {
986
+ methodsList.push("hashchange");
987
+ } else {
988
+ var _methodsList;
989
+ (_methodsList = methodsList).push.apply(_methodsList, [
990
+ "popstate",
991
+ "pushState",
992
+ "replaceState"
993
+ ]);
994
+ }
995
+ methodsList.forEach(function(method) {
996
+ window.addEventListener(method, function() {
997
+ var href = window.location.href || "";
998
+ var params = {
999
+ event_name: href,
1000
+ custom_data: {
1001
+ page_url: href,
1002
+ page_name: "unknown",
1003
+ count: 1
1004
+ }
1005
+ };
1006
+ _this.singleTrack("page_view", params);
1007
+ });
1008
+ });
1009
+ }
1010
+ },
1011
+ {
1012
+ /**
1013
+ * @method pv事件
1014
+ */ key: "frPageView",
1015
+ value: function frPageView(event_name, custom_data) {
1016
+ var params = {
1017
+ event_name: event_name,
1018
+ custom_data: custom_data
1019
+ };
1020
+ this.singleTrack("page_view", params);
1021
+ }
1022
+ },
1023
+ {
1024
+ /**
1025
+ * @method uv事件
1026
+ */ key: "frUserView",
1027
+ value: function frUserView() {
1028
+ var _this = this;
1029
+ var currentDate = /* @__PURE__ */ new Date().setHours(0, 0, 0, 0);
1030
+ var interval = 60 * 60 * 1e3;
1031
+ var params = {
1032
+ event_name: "user_view"
1033
+ };
1034
+ this.singleTrack("user_view", params);
1035
+ if (this.uvTimer) {
1036
+ clearInterval(this.uvTimer);
1037
+ this.uvTimer = null;
1038
+ }
1039
+ this.uvTimer = setInterval(function() {
1040
+ var now = /* @__PURE__ */ new Date().setHours(0, 0, 0, 0);
1041
+ if (now > currentDate + 24 * interval) {
1042
+ _this.singleTrack("user_view", params);
1043
+ }
1044
+ }, interval);
1045
+ }
1046
+ },
1047
+ {
1048
+ /**
1049
+ * @method 页面销毁事件
1050
+ */ key: "frPageDestroy",
1051
+ value: function frPageDestroy(event_name, custom_data) {
1052
+ var is_aggregation = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
1053
+ var params = {
1054
+ event_name: event_name,
1055
+ custom_data: custom_data,
1056
+ is_aggregation: is_aggregation
1057
+ };
1058
+ this.singleTrack("page_destroy", params);
1059
+ }
1060
+ },
1061
+ {
1062
+ /**
1063
+ * @method 手动立即上报缓存的数据
1064
+ */ key: "frReport",
1065
+ value: function frReport() {
1066
+ reportCacheData(this.option.report_url);
1067
+ }
1068
+ }
1069
+ ]);
1070
+ return FrTrack;
1071
+ }();
1072
+ var index_default = FrTrack;
1073
+ export { index_default as default };
1074
+ //# sourceMappingURL=index.js.map