lee_web_track_sdk 0.0.14 → 0.0.16

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
@@ -431,9 +431,7 @@ var defaultConfig = {
431
431
  {
432
432
  type: "track",
433
433
  options: {
434
- autoPv: true,
435
- frameType: "js",
436
- router: null
434
+ autoPv: true
437
435
  }
438
436
  }
439
437
  ]
@@ -560,6 +558,29 @@ var getUserTimeZone = function() {
560
558
  }
561
559
  }
562
560
  };
561
+ function debounce(fn) {
562
+ var delay = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 800, immediate = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
563
+ var timer = null;
564
+ var flag = false;
565
+ return function() {
566
+ var _this = this;
567
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
568
+ args[_key] = arguments[_key];
569
+ }
570
+ if (immediate && !flag) {
571
+ fn.apply(this, args);
572
+ }
573
+ if (timer !== null) {
574
+ clearTimeout(timer);
575
+ timer = null;
576
+ }
577
+ flag = true;
578
+ timer = setTimeout(function() {
579
+ fn.apply(_this, args);
580
+ flag = false;
581
+ }, delay);
582
+ };
583
+ }
563
584
  var sanitizeCustomData = function(data) {
564
585
  var sanitized = {};
565
586
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
@@ -950,6 +971,197 @@ var WebReport = /*#__PURE__*/ function(Report) {
950
971
  return WebReport;
951
972
  }(Report);
952
973
  var report_default = WebReport;
974
+ // src/adapter/wx/cache.ts
975
+ var WxCache = /*#__PURE__*/ function(Cache) {
976
+ _inherits(WxCache, Cache);
977
+ function WxCache(options) {
978
+ _class_call_check(this, WxCache);
979
+ return _call_super(this, WxCache, [
980
+ options
981
+ ]);
982
+ }
983
+ _create_class(WxCache, [
984
+ {
985
+ key: "get",
986
+ value: function get(name) {
987
+ return _async_to_generator(function() {
988
+ var res;
989
+ return _ts_generator(this, function(_state) {
990
+ res = wx.getStorageSync(name);
991
+ return [
992
+ 2,
993
+ res || []
994
+ ];
995
+ });
996
+ })();
997
+ }
998
+ },
999
+ {
1000
+ key: "set",
1001
+ value: function set(name, data) {
1002
+ return _async_to_generator(function() {
1003
+ var currentData, cacheData, _currentData;
1004
+ return _ts_generator(this, function(_state) {
1005
+ switch(_state.label){
1006
+ case 0:
1007
+ return [
1008
+ 4,
1009
+ this.get(name)
1010
+ ];
1011
+ case 1:
1012
+ currentData = _state.sent();
1013
+ cacheData = Array.isArray(data) ? data : [
1014
+ data
1015
+ ];
1016
+ if (currentData.length) {
1017
+ ;
1018
+ (_currentData = currentData).push.apply(_currentData, _to_consumable_array(cacheData));
1019
+ wx.setStorageSync(name, currentData);
1020
+ } else {
1021
+ wx.setStorageSync(name, cacheData);
1022
+ }
1023
+ return [
1024
+ 2
1025
+ ];
1026
+ }
1027
+ });
1028
+ }).call(this);
1029
+ }
1030
+ },
1031
+ {
1032
+ key: "remove",
1033
+ value: function remove(name) {
1034
+ return _async_to_generator(function() {
1035
+ return _ts_generator(this, function(_state) {
1036
+ wx.removeStorageSync(name);
1037
+ return [
1038
+ 2
1039
+ ];
1040
+ });
1041
+ })();
1042
+ }
1043
+ },
1044
+ {
1045
+ key: "clear",
1046
+ value: function clear() {
1047
+ return _async_to_generator(function() {
1048
+ return _ts_generator(this, function(_state) {
1049
+ wx.clearStorageSync();
1050
+ return [
1051
+ 2
1052
+ ];
1053
+ });
1054
+ })();
1055
+ }
1056
+ }
1057
+ ]);
1058
+ return WxCache;
1059
+ }(_wrap_native_super(Cache));
1060
+ var cache_default2 = WxCache;
1061
+ // src/adapter/wx/report.ts
1062
+ var WxReport = /*#__PURE__*/ function(Report) {
1063
+ _inherits(WxReport, Report);
1064
+ function WxReport(options) {
1065
+ _class_call_check(this, WxReport);
1066
+ return _call_super(this, WxReport, [
1067
+ options
1068
+ ]);
1069
+ }
1070
+ _create_class(WxReport, [
1071
+ {
1072
+ key: "retry",
1073
+ value: function retry(failedData, callback) {
1074
+ var retryCount = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 1;
1075
+ return _async_to_generator(function() {
1076
+ var _this, err;
1077
+ return _ts_generator(this, function(_state) {
1078
+ switch(_state.label){
1079
+ case 0:
1080
+ _this = this;
1081
+ if (!(failedData === null || failedData === void 0 ? void 0 : failedData.length)) return [
1082
+ 2
1083
+ ];
1084
+ if (retryCount >= 3) {
1085
+ if (!callback) return [
1086
+ 2
1087
+ ];
1088
+ callback();
1089
+ return [
1090
+ 2
1091
+ ];
1092
+ }
1093
+ _state.label = 1;
1094
+ case 1:
1095
+ _state.trys.push([
1096
+ 1,
1097
+ 3,
1098
+ ,
1099
+ 4
1100
+ ]);
1101
+ return [
1102
+ 4,
1103
+ this.send(failedData)
1104
+ ];
1105
+ case 2:
1106
+ _state.sent();
1107
+ return [
1108
+ 3,
1109
+ 4
1110
+ ];
1111
+ case 3:
1112
+ err = _state.sent();
1113
+ setTimeout(function() {
1114
+ _this.retry(failedData, callback, retryCount + 1);
1115
+ }, 3e3);
1116
+ return [
1117
+ 3,
1118
+ 4
1119
+ ];
1120
+ case 4:
1121
+ return [
1122
+ 2
1123
+ ];
1124
+ }
1125
+ });
1126
+ }).call(this);
1127
+ }
1128
+ },
1129
+ {
1130
+ key: "send",
1131
+ value: function send(data) {
1132
+ return _async_to_generator(function() {
1133
+ var _this_options, url, timeout;
1134
+ return _ts_generator(this, function(_state) {
1135
+ _this_options = this.options, url = _this_options.url, timeout = _this_options.timeout;
1136
+ return [
1137
+ 2,
1138
+ new Promise(function(resolve, reject) {
1139
+ if (wx && wx.request) {
1140
+ wx.request({
1141
+ url: url,
1142
+ method: "POST",
1143
+ data: data,
1144
+ timeout: timeout,
1145
+ success: function success(res) {
1146
+ resolve(res);
1147
+ },
1148
+ fail: function fail(err) {
1149
+ reject(err);
1150
+ }
1151
+ });
1152
+ } else {
1153
+ reject();
1154
+ }
1155
+ })
1156
+ ];
1157
+ });
1158
+ }).call(this);
1159
+ }
1160
+ }
1161
+ ]);
1162
+ return WxReport;
1163
+ }(Report);
1164
+ var report_default2 = WxReport;
953
1165
  // src/adapter/index.ts
954
1166
  var getEnv = function() {
955
1167
  var isMiniProgram = wx && typeof wx.getSystemInfoSync === "function";
@@ -957,7 +1169,13 @@ var getEnv = function() {
957
1169
  };
958
1170
  var getAdapter = function(configEnv) {
959
1171
  var env = configEnv || getEnv();
960
- if (env === "miniProgram") {}
1172
+ if (env === "miniProgram") {
1173
+ return {
1174
+ env: "miniProgram",
1175
+ cache: cache_default2,
1176
+ report: report_default2
1177
+ };
1178
+ }
961
1179
  return {
962
1180
  env: "web",
963
1181
  cache: cache_default,
@@ -1032,6 +1250,19 @@ var FrTrack = /*#__PURE__*/ function() {
1032
1250
  this.reportFn = instance2.$report;
1033
1251
  this.instanceConfig = instance2.config;
1034
1252
  this.pluginOptions = pluginOptions;
1253
+ this.debouncePv = debounce(function(type) {
1254
+ var href = window.location.href || "";
1255
+ logInfo(type, "\u4E8B\u4EF6\u89E6\u53D1", _this.instanceConfig.isDebug);
1256
+ var params = {
1257
+ event_name: href,
1258
+ custom_data: {
1259
+ page_url: href,
1260
+ page_name: "unknown",
1261
+ count: 1
1262
+ }
1263
+ };
1264
+ _this.singleTrack("page_view", params);
1265
+ }, 300);
1035
1266
  }
1036
1267
  _create_class(FrTrack, [
1037
1268
  {
@@ -1258,50 +1489,38 @@ var FrTrack = /*#__PURE__*/ function() {
1258
1489
  var _this = this;
1259
1490
  var methodsList = [
1260
1491
  "popstate",
1492
+ "hashchange",
1261
1493
  "pushState",
1262
1494
  "replaceState"
1263
1495
  ];
1496
+ this.debouncePv("\u521D\u6B21\u4E0A\u62A5");
1264
1497
  methodsList.forEach(function(method) {
1265
1498
  window.addEventListener(method, function() {
1266
- var href = window.location.href || "";
1267
- logInfo(method, "\u4E8B\u4EF6\u89E6\u53D1", _this.instanceConfig.isDebug);
1268
- var params = {
1269
- event_name: href,
1270
- custom_data: {
1271
- page_url: href,
1272
- page_name: "unknown",
1273
- count: 1
1274
- }
1275
- };
1276
- _this.singleTrack("page_view", params);
1499
+ _this.debouncePv(method);
1277
1500
  });
1278
1501
  });
1279
1502
  }
1280
1503
  },
1281
- {
1282
- key: "vuePv",
1283
- value: function vuePv() {
1284
- var _this = this;
1285
- if (!this.pluginOptions.router) {
1286
- throw new Error("\u8BF7\u4F20\u5165\u8DEF\u7531\u5B9E\u4F8B");
1287
- }
1288
- var router = this.pluginOptions.router;
1289
- router.afterEach(function(to) {
1290
- var params = {
1291
- event_name: to.path,
1292
- custom_data: {
1293
- page_url: to.fullPath || "--",
1294
- page_name: to.name || "--",
1295
- count: 1
1296
- }
1297
- };
1298
- _this.singleTrack("page_view", params);
1299
- });
1300
- }
1301
- },
1302
1504
  {
1303
1505
  key: "toSetCacheData",
1304
- value: function toSetCacheData(params) {
1506
+ value: // private vuePv() {
1507
+ // if (!this.pluginOptions.router) {
1508
+ // throw new Error('请传入路由实例');
1509
+ // }
1510
+ // const router = this.pluginOptions.router as any;
1511
+ // router.afterEach((to: any) => {
1512
+ // const params = {
1513
+ // event_name: to.path,
1514
+ // custom_data: {
1515
+ // page_url: to.fullPath || '--',
1516
+ // page_name: to.name || '--',
1517
+ // count: 1
1518
+ // }
1519
+ // };
1520
+ // this.singleTrack('page_view', params);
1521
+ // });
1522
+ // }
1523
+ function toSetCacheData(params) {
1305
1524
  return _async_to_generator(function() {
1306
1525
  var _this;
1307
1526
  return _ts_generator(this, function(_state) {
@@ -1548,13 +1767,9 @@ var FrTrack = /*#__PURE__*/ function() {
1548
1767
  * @method 自动收集pv事件
1549
1768
  */ key: "frAutoPageView",
1550
1769
  value: function frAutoPageView() {
1551
- if (this.pluginOptions.frameType === "js") {
1552
- window.history.pushState = overrideHistoryMethods("pushState");
1553
- window.history.replaceState = overrideHistoryMethods("replaceState");
1554
- this.nativePv();
1555
- } else if (this.pluginOptions.frameType === "vue") {
1556
- this.vuePv();
1557
- }
1770
+ window.history.pushState = overrideHistoryMethods("pushState");
1771
+ window.history.replaceState = overrideHistoryMethods("replaceState");
1772
+ this.nativePv();
1558
1773
  }
1559
1774
  },
1560
1775
  {
@@ -1619,14 +1834,16 @@ var FrTrack = /*#__PURE__*/ function() {
1619
1834
  {
1620
1835
  key: "init",
1621
1836
  value: function init() {
1622
- this.browserInfo = getBrowserAndDeviceInfo();
1837
+ if (this.instanceConfig.adapter === "web" && window) {
1838
+ this.browserInfo = getBrowserAndDeviceInfo();
1839
+ this.onBrowserClose();
1840
+ this.frUserView();
1841
+ if (this.pluginOptions.autoPv) {
1842
+ this.frAutoPageView();
1843
+ }
1844
+ }
1623
1845
  this.reportPrevData();
1624
1846
  this.loopReport();
1625
- this.onBrowserClose();
1626
- this.frUserView();
1627
- if (this.pluginOptions.autoPv) {
1628
- this.frAutoPageView();
1629
- }
1630
1847
  }
1631
1848
  }
1632
1849
  ]);