lee_web_track_sdk 0.0.12 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -4,6 +4,9 @@ function _array_like_to_array(arr, len) {
4
4
  for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
5
5
  return arr2;
6
6
  }
7
+ function _array_with_holes(arr) {
8
+ if (Array.isArray(arr)) return arr;
9
+ }
7
10
  function _array_without_holes(arr) {
8
11
  if (Array.isArray(arr)) return _array_like_to_array(arr);
9
12
  }
@@ -120,6 +123,33 @@ function _is_native_function(fn) {
120
123
  function _iterable_to_array(iter) {
121
124
  if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
122
125
  }
126
+ function _iterable_to_array_limit(arr, i) {
127
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
128
+ if (_i == null) return;
129
+ var _arr = [];
130
+ var _n = true;
131
+ var _d = false;
132
+ var _s, _e;
133
+ try {
134
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
135
+ _arr.push(_s.value);
136
+ if (i && _arr.length === i) break;
137
+ }
138
+ } catch (err) {
139
+ _d = true;
140
+ _e = err;
141
+ } finally{
142
+ try {
143
+ if (!_n && _i["return"] != null) _i["return"]();
144
+ } finally{
145
+ if (_d) throw _e;
146
+ }
147
+ }
148
+ return _arr;
149
+ }
150
+ function _non_iterable_rest() {
151
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
152
+ }
123
153
  function _non_iterable_spread() {
124
154
  throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
125
155
  }
@@ -138,6 +168,30 @@ function _object_spread(target) {
138
168
  }
139
169
  return target;
140
170
  }
171
+ function ownKeys(object, enumerableOnly) {
172
+ var keys = Object.keys(object);
173
+ if (Object.getOwnPropertySymbols) {
174
+ var symbols = Object.getOwnPropertySymbols(object);
175
+ if (enumerableOnly) {
176
+ symbols = symbols.filter(function(sym) {
177
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
178
+ });
179
+ }
180
+ keys.push.apply(keys, symbols);
181
+ }
182
+ return keys;
183
+ }
184
+ function _object_spread_props(target, source) {
185
+ source = source != null ? source : {};
186
+ if (Object.getOwnPropertyDescriptors) {
187
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
188
+ } else {
189
+ ownKeys(Object(source)).forEach(function(key) {
190
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
191
+ });
192
+ }
193
+ return target;
194
+ }
141
195
  function _possible_constructor_return(self, call) {
142
196
  if (call && (_type_of(call) === "object" || typeof call === "function")) {
143
197
  return call;
@@ -151,6 +205,9 @@ function _set_prototype_of(o, p) {
151
205
  };
152
206
  return _set_prototype_of(o, p);
153
207
  }
208
+ function _sliced_to_array(arr, i) {
209
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
210
+ }
154
211
  function _to_consumable_array(arr) {
155
212
  return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
156
213
  }
@@ -358,14 +415,18 @@ __export(index_exports, {
358
415
  });
359
416
  module.exports = __toCommonJS(index_exports);
360
417
  // src/config/index.ts
418
+ var MAX_CACHE_SIZE = 15;
419
+ var MAX_CACHE_TIME = 3e5;
420
+ var MAX_TIMEOUT = 1e4;
361
421
  var defaultConfig = {
362
422
  project_name: "",
363
423
  report_url: "",
364
424
  app_version: "",
365
425
  adapter: "web",
366
- maxCacheSize: 15,
367
- maxCacheTime: 3e5,
368
- maxTimeout: 1e4,
426
+ maxCacheSize: MAX_CACHE_SIZE,
427
+ maxCacheTime: MAX_CACHE_TIME,
428
+ maxTimeout: MAX_TIMEOUT,
429
+ isDebug: false,
369
430
  plugins: [
370
431
  {
371
432
  type: "track",
@@ -443,6 +504,12 @@ var EVENT_KEYS = [
443
504
  var CACHE_DATA_NAME = "fr-track-cache-data";
444
505
  var CACHE_ERR_DATA_NAME = "fr-track-cache-err-data";
445
506
  // src/utils/utils.ts
507
+ var logInfo = function(message, title) {
508
+ var isDebug = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
509
+ if (!isDebug) return;
510
+ var logTitle = title ? "[Fr-track log ".concat(title, "]") : "[Fr-track log]";
511
+ console.log("".concat(logTitle, ":"), message);
512
+ };
446
513
  var logWarn = function(message, title) {
447
514
  var logTitle = title ? "[Fr-track warning ".concat(title, "]") : "[Fr-track warning]";
448
515
  console.warn("".concat(logTitle, ":"), message);
@@ -493,6 +560,36 @@ var getUserTimeZone = function() {
493
560
  }
494
561
  }
495
562
  };
563
+ var sanitizeCustomData = function(data) {
564
+ var sanitized = {};
565
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
566
+ try {
567
+ for(var _iterator = Object.entries(data)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
568
+ var _step_value = _sliced_to_array(_step.value, 2), key = _step_value[0], value = _step_value[1];
569
+ if (typeof value === "string") {
570
+ sanitized[key] = value.replace(/<[^>]*>/g, "");
571
+ } else if (getVariableType(value) === "object") {
572
+ sanitized[key] = sanitizeCustomData(value);
573
+ } else {
574
+ sanitized[key] = value;
575
+ }
576
+ }
577
+ } catch (err) {
578
+ _didIteratorError = true;
579
+ _iteratorError = err;
580
+ } finally{
581
+ try {
582
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
583
+ _iterator.return();
584
+ }
585
+ } finally{
586
+ if (_didIteratorError) {
587
+ throw _iteratorError;
588
+ }
589
+ }
590
+ }
591
+ return sanitized;
592
+ };
496
593
  // src/utils/validator.ts
497
594
  function validateEventType(eventType) {
498
595
  return typeof eventType === "string" && EVENT_TYPES.includes(eventType);
@@ -574,8 +671,9 @@ var cleanTrackData = function(rawData) {
574
671
  var aggregationData = function(rawData) {
575
672
  try {
576
673
  var _loop = function(i) {
577
- var item = rawData[i];
578
- if (!item) return "continue";
674
+ var rawItem = rawData[i];
675
+ if (!rawItem) return "continue";
676
+ var item = _object_spread({}, rawItem);
579
677
  if (CAN_AGGREGATION_TYPES.includes(item.event_type) || item.is_aggregation) {
580
678
  delete item.is_aggregation;
581
679
  var currentDate = new Date(item.event_time).setHours(0, 0, 0, 0);
@@ -749,50 +847,57 @@ var WebReport = /*#__PURE__*/ function(Report) {
749
847
  {
750
848
  key: "retry",
751
849
  value: function retry(failedData, callback) {
850
+ var retryCount = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 1;
752
851
  return _async_to_generator(function() {
753
- var _this, retryCount, retryFun;
852
+ var _this, err;
754
853
  return _ts_generator(this, function(_state) {
755
- _this = this;
756
- retryCount = 1;
757
- if (!(failedData === null || failedData === void 0 ? void 0 : failedData.length)) return [
758
- 2
759
- ];
760
- if (retryCount >= 3) {
761
- if (!callback) return [
762
- 2
763
- ];
764
- callback();
765
- return [
766
- 2
767
- ];
854
+ switch(_state.label){
855
+ case 0:
856
+ _this = this;
857
+ if (!(failedData === null || failedData === void 0 ? void 0 : failedData.length)) return [
858
+ 2
859
+ ];
860
+ if (retryCount >= 3) {
861
+ if (!callback) return [
862
+ 2
863
+ ];
864
+ callback();
865
+ return [
866
+ 2
867
+ ];
868
+ }
869
+ _state.label = 1;
870
+ case 1:
871
+ _state.trys.push([
872
+ 1,
873
+ 3,
874
+ ,
875
+ 4
876
+ ]);
877
+ return [
878
+ 4,
879
+ this.send(failedData)
880
+ ];
881
+ case 2:
882
+ _state.sent();
883
+ return [
884
+ 3,
885
+ 4
886
+ ];
887
+ case 3:
888
+ err = _state.sent();
889
+ setTimeout(function() {
890
+ _this.retry(failedData, callback, retryCount + 1);
891
+ }, 3e3);
892
+ return [
893
+ 3,
894
+ 4
895
+ ];
896
+ case 4:
897
+ return [
898
+ 2
899
+ ];
768
900
  }
769
- retryFun = function() {
770
- return _async_to_generator(function() {
771
- return _ts_generator(this, function(_state) {
772
- switch(_state.label){
773
- case 0:
774
- return [
775
- 4,
776
- this.send(failedData).catch(function() {
777
- setTimeout(function() {
778
- retryCount++;
779
- retryFun();
780
- }, 3e3);
781
- })
782
- ];
783
- case 1:
784
- _state.sent();
785
- return [
786
- 2
787
- ];
788
- }
789
- });
790
- }).call(_this);
791
- };
792
- retryFun();
793
- return [
794
- 2
795
- ];
796
901
  });
797
902
  }).call(this);
798
903
  }
@@ -807,22 +912,34 @@ var WebReport = /*#__PURE__*/ function(Report) {
807
912
  return [
808
913
  2,
809
914
  new Promise(function(resolve, reject) {
810
- var xhr = new XMLHttpRequest();
811
- xhr.open("POST", url, true);
812
- xhr.timeout = timeout;
813
- xhr.setRequestHeader("Content-Type", "application/json");
814
- xhr.onload = function() {
815
- resolve(xhr.response);
816
- if (xhr.status === 0) {
817
- resolve(xhr.response);
915
+ if ("sendBeacon" in navigator) {
916
+ var jsonData = JSON.stringify(data);
917
+ var formData = new FormData();
918
+ formData.append("data", jsonData);
919
+ var status = navigator.sendBeacon(url, formData);
920
+ if (status) {
921
+ resolve(true);
818
922
  } else {
819
923
  reject();
820
924
  }
821
- };
822
- xhr.onerror = function() {
823
- reject();
824
- };
825
- xhr.send(JSON.stringify(data));
925
+ } else {
926
+ var xhr = new XMLHttpRequest();
927
+ xhr.open("POST", url, true);
928
+ xhr.timeout = timeout;
929
+ xhr.setRequestHeader("Content-Type", "application/json");
930
+ xhr.onload = function() {
931
+ resolve(xhr.response);
932
+ if (xhr.status === 0) {
933
+ resolve(xhr.response);
934
+ } else {
935
+ reject();
936
+ }
937
+ };
938
+ xhr.onerror = function() {
939
+ reject();
940
+ };
941
+ xhr.send(JSON.stringify(data));
942
+ }
826
943
  })
827
944
  ];
828
945
  });
@@ -849,13 +966,17 @@ var getAdapter = function(configEnv) {
849
966
  };
850
967
  // src/utils/uuid.ts
851
968
  var getUserId = function() {
852
- var track_uuid = localStorage.getItem("track_uuid");
853
- if (track_uuid) {
854
- return track_uuid;
969
+ try {
970
+ var track_uuid = localStorage.getItem("track_uuid");
971
+ if (track_uuid) {
972
+ return track_uuid;
973
+ }
974
+ var uuid = generateUserId();
975
+ localStorage.setItem("track_uuid", uuid);
976
+ return uuid;
977
+ } catch (e) {
978
+ return generateUserId();
855
979
  }
856
- var uuid = generateUserId();
857
- localStorage.setItem("track_uuid", uuid);
858
- return uuid;
859
980
  };
860
981
  // src/utils/browser.ts
861
982
  var import_bowser = __toESM(require("bowser"), 1);
@@ -897,11 +1018,11 @@ var FrTrack = /*#__PURE__*/ function() {
897
1018
  this.userId = getUserId();
898
1019
  this.timer = null;
899
1020
  this.uvTimer = null;
1021
+ this.lastUvReportTime = 0;
900
1022
  this.browserInfo = null;
901
1023
  this.taskQueue = [];
902
1024
  this.isProcessing = false;
903
1025
  this.reportPrevData = function() {
904
- _this.reportCacheData();
905
1026
  _this.reportErrCacheData();
906
1027
  };
907
1028
  if (!instance2) {
@@ -940,14 +1061,14 @@ var FrTrack = /*#__PURE__*/ function() {
940
1061
  ];
941
1062
  case 2:
942
1063
  _state.sent();
943
- console.log("✅ 任务执行成功:".concat(currentTask.desc));
1064
+ logInfo("✅ 任务执行成功:".concat(currentTask.desc), "", this.instanceConfig.isDebug);
944
1065
  return [
945
1066
  3,
946
1067
  5
947
1068
  ];
948
1069
  case 3:
949
1070
  err = _state.sent();
950
- logError(err, "❌ 任务执行失败:".concat(currentTask.desc));
1071
+ logInfo(err, "❌ 任务执行失败:".concat(currentTask.desc), this.instanceConfig.isDebug);
951
1072
  return [
952
1073
  3,
953
1074
  5
@@ -988,10 +1109,11 @@ var FrTrack = /*#__PURE__*/ function() {
988
1109
  type: "report_send",
989
1110
  fn: function() {
990
1111
  return _async_to_generator(function() {
991
- var cacheData, cleanInfo, result;
1112
+ var _this, cacheData, cleanInfo, result;
992
1113
  return _ts_generator(this, function(_state) {
993
1114
  switch(_state.label){
994
1115
  case 0:
1116
+ _this = this;
995
1117
  if (!this.cacheFn || !this.reportFn) return [
996
1118
  2
997
1119
  ];
@@ -1009,9 +1131,10 @@ var FrTrack = /*#__PURE__*/ function() {
1009
1131
  logWarn(cleanInfo.errReason);
1010
1132
  } else {
1011
1133
  result = aggregationData(cleanInfo.data);
1012
- if (result && result.length) {
1134
+ if (result === null || result === void 0 ? void 0 : result.length) {
1013
1135
  this.reportFn.send(result).catch(function(err) {
1014
1136
  logError(err, "\u4E0A\u62A5\u5931\u8D25\u6570\u636E\u5F02\u5E38");
1137
+ _this.cacheFn.set(CACHE_ERR_DATA_NAME, result);
1015
1138
  });
1016
1139
  }
1017
1140
  }
@@ -1192,22 +1315,32 @@ var FrTrack = /*#__PURE__*/ function() {
1192
1315
  if (!this.cacheFn) return [
1193
1316
  2
1194
1317
  ];
1318
+ maxCacheSize = this.instanceConfig.maxCacheSize;
1195
1319
  return [
1196
1320
  4,
1197
- this.cacheFn.set(CACHE_DATA_NAME, params)
1321
+ this.cacheFn.get(CACHE_DATA_NAME)
1198
1322
  ];
1199
1323
  case 1:
1200
- _state.sent();
1201
- maxCacheSize = this.instanceConfig.maxCacheSize;
1324
+ res = _state.sent();
1325
+ if (!((res === null || res === void 0 ? void 0 : res.length) >= maxCacheSize)) return [
1326
+ 3,
1327
+ 2
1328
+ ];
1329
+ this.reportCacheData();
1330
+ this.toSetCacheData(params);
1202
1331
  return [
1203
- 4,
1204
- this.cacheFn.get(CACHE_DATA_NAME)
1332
+ 3,
1333
+ 4
1205
1334
  ];
1206
1335
  case 2:
1207
- res = _state.sent();
1208
- if ((res === null || res === void 0 ? void 0 : res.length) >= maxCacheSize) {
1209
- this.reportCacheData();
1210
- }
1336
+ return [
1337
+ 4,
1338
+ this.cacheFn.set(CACHE_DATA_NAME, params)
1339
+ ];
1340
+ case 3:
1341
+ _state.sent();
1342
+ _state.label = 4;
1343
+ case 4:
1211
1344
  return [
1212
1345
  2
1213
1346
  ];
@@ -1231,6 +1364,9 @@ var FrTrack = /*#__PURE__*/ function() {
1231
1364
  return _ts_generator(this, function(_state) {
1232
1365
  _this_instanceConfig = this.instanceConfig, project_name = _this_instanceConfig.project_name, app_version = _this_instanceConfig.app_version;
1233
1366
  _this_browserInfo = this.browserInfo, osName = _this_browserInfo.osName, osVersion = _this_browserInfo.osVersion, browserName = _this_browserInfo.browserName, browserVersion = _this_browserInfo.browserVersion, platformName = _this_browserInfo.platformName;
1367
+ if (params.custom_data) {
1368
+ params.custom_data = sanitizeCustomData(params.custom_data);
1369
+ }
1234
1370
  trackParams = Object.assign({
1235
1371
  project_name: project_name,
1236
1372
  app_version: app_version,
@@ -1265,8 +1401,47 @@ var FrTrack = /*#__PURE__*/ function() {
1265
1401
  * @method 自定义用户id
1266
1402
  */ key: "setUser",
1267
1403
  value: function setUser(id) {
1404
+ var _this = this;
1268
1405
  this.userId = id;
1269
- this.frUserView();
1406
+ this.addTask({
1407
+ type: "cache_update",
1408
+ fn: function() {
1409
+ return _async_to_generator(function() {
1410
+ var cacheData, updatedData;
1411
+ return _ts_generator(this, function(_state) {
1412
+ switch(_state.label){
1413
+ case 0:
1414
+ return [
1415
+ 4,
1416
+ this.cacheFn.get(CACHE_DATA_NAME)
1417
+ ];
1418
+ case 1:
1419
+ cacheData = _state.sent();
1420
+ if (!(cacheData === null || cacheData === void 0 ? void 0 : cacheData.length)) return [
1421
+ 3,
1422
+ 3
1423
+ ];
1424
+ updatedData = cacheData.map(function(item) {
1425
+ return _object_spread_props(_object_spread({}, item), {
1426
+ user_id: id
1427
+ });
1428
+ });
1429
+ return [
1430
+ 4,
1431
+ this.cacheFn.set(CACHE_DATA_NAME, updatedData)
1432
+ ];
1433
+ case 2:
1434
+ _state.sent();
1435
+ _state.label = 3;
1436
+ case 3:
1437
+ return [
1438
+ 2
1439
+ ];
1440
+ }
1441
+ });
1442
+ }).call(_this);
1443
+ }
1444
+ });
1270
1445
  }
1271
1446
  },
1272
1447
  {
@@ -1397,7 +1572,7 @@ var FrTrack = /*#__PURE__*/ function() {
1397
1572
  */ key: "frUserView",
1398
1573
  value: function frUserView() {
1399
1574
  var _this = this;
1400
- var currentDate = /* @__PURE__ */ new Date().setHours(0, 0, 0, 0);
1575
+ this.lastUvReportTime = /* @__PURE__ */ new Date().setHours(0, 0, 0, 0);
1401
1576
  var interval = 60 * 60 * 1e3;
1402
1577
  var params = {
1403
1578
  event_name: "user_view"
@@ -1409,7 +1584,8 @@ var FrTrack = /*#__PURE__*/ function() {
1409
1584
  }
1410
1585
  this.uvTimer = setInterval(function() {
1411
1586
  var now = /* @__PURE__ */ new Date().setHours(0, 0, 0, 0);
1412
- if (now > currentDate + 24 * interval) {
1587
+ if (now > _this.lastUvReportTime + 24 * interval) {
1588
+ _this.lastUvReportTime = now;
1413
1589
  _this.singleTrack("user_view", params);
1414
1590
  }
1415
1591
  }, interval);