@fle-sdk/event-tracking-web 1.2.2 → 1.2.3
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/README.md +246 -2
- package/lib/index.esm.js +792 -288
- package/lib/index.esm.min.js +1 -1
- package/lib/index.js +2331 -1827
- package/lib/index.min.js +1 -1
- package/lib/types/index.d.ts +61 -10
- package/lib/types/tools.d.ts +2 -0
- package/lib/types/type.d.ts +47 -0
- package/package.json +2 -3
package/lib/index.esm.js
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
function _typeof(obj) {
|
|
2
|
+
"@babel/helpers - typeof";
|
|
3
|
+
|
|
4
|
+
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
|
|
5
|
+
_typeof = function (obj) {
|
|
6
|
+
return typeof obj;
|
|
7
|
+
};
|
|
8
|
+
} else {
|
|
9
|
+
_typeof = function (obj) {
|
|
10
|
+
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return _typeof(obj);
|
|
15
|
+
}
|
|
16
|
+
|
|
1
17
|
/*! *****************************************************************************
|
|
2
18
|
Copyright (c) Microsoft Corporation.
|
|
3
19
|
|
|
@@ -46,22 +62,6 @@ function __spreadArray(to, from) {
|
|
|
46
62
|
return to;
|
|
47
63
|
}
|
|
48
64
|
|
|
49
|
-
function _typeof(obj) {
|
|
50
|
-
"@babel/helpers - typeof";
|
|
51
|
-
|
|
52
|
-
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
|
|
53
|
-
_typeof = function (obj) {
|
|
54
|
-
return typeof obj;
|
|
55
|
-
};
|
|
56
|
-
} else {
|
|
57
|
-
_typeof = function (obj) {
|
|
58
|
-
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return _typeof(obj);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
65
|
var WebTrackingTools =
|
|
66
66
|
/** @class */
|
|
67
67
|
function () {
|
|
@@ -444,7 +444,7 @@ function () {
|
|
|
444
444
|
|
|
445
445
|
this.filterSensitiveData = function (obj, sensitiveKeys) {
|
|
446
446
|
if (sensitiveKeys === void 0) {
|
|
447
|
-
sensitiveKeys = [
|
|
447
|
+
sensitiveKeys = ["password", "token", "secret", "key"];
|
|
448
448
|
}
|
|
449
449
|
|
|
450
450
|
if (!_this.isObject(obj)) return obj;
|
|
@@ -453,11 +453,11 @@ function () {
|
|
|
453
453
|
_this.each(obj, function (value, key) {
|
|
454
454
|
// 检查是否是敏感字段
|
|
455
455
|
var isSensitive = sensitiveKeys.some(function (sensitiveKey) {
|
|
456
|
-
return typeof key ===
|
|
456
|
+
return typeof key === "string" && key.toLowerCase().includes(sensitiveKey.toLowerCase());
|
|
457
457
|
});
|
|
458
458
|
|
|
459
459
|
if (isSensitive) {
|
|
460
|
-
filteredObj[key] =
|
|
460
|
+
filteredObj[key] = "***";
|
|
461
461
|
} else if (_this.isObject(value)) {
|
|
462
462
|
// 递归过滤嵌套对象
|
|
463
463
|
filteredObj[key] = _this.filterSensitiveData(value, sensitiveKeys);
|
|
@@ -476,9 +476,9 @@ function () {
|
|
|
476
476
|
|
|
477
477
|
|
|
478
478
|
this.xssFilter = function (str) {
|
|
479
|
-
if (!str) return
|
|
480
|
-
if (typeof str !==
|
|
481
|
-
return str.replace(/</g,
|
|
479
|
+
if (!str) return "";
|
|
480
|
+
if (typeof str !== "string") return str.toString();
|
|
481
|
+
return str.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'").replace(/\//g, "/");
|
|
482
482
|
};
|
|
483
483
|
/**
|
|
484
484
|
* 获取url中的参数
|
|
@@ -518,7 +518,7 @@ function () {
|
|
|
518
518
|
return {};
|
|
519
519
|
}
|
|
520
520
|
|
|
521
|
-
if (typeof data ===
|
|
521
|
+
if (typeof data === "string") {
|
|
522
522
|
try {
|
|
523
523
|
return JSON.parse(data);
|
|
524
524
|
} catch (e) {
|
|
@@ -526,7 +526,7 @@ function () {
|
|
|
526
526
|
}
|
|
527
527
|
}
|
|
528
528
|
|
|
529
|
-
if (_typeof(data) ===
|
|
529
|
+
if (_typeof(data) === "object") {
|
|
530
530
|
return data;
|
|
531
531
|
}
|
|
532
532
|
|
|
@@ -817,7 +817,7 @@ function () {
|
|
|
817
817
|
|
|
818
818
|
this.getDeviceId = function () {
|
|
819
819
|
// 获取已存储的设备ID
|
|
820
|
-
var storedDeviceId = _this.getCookie(
|
|
820
|
+
var storedDeviceId = _this.getCookie("device_id") || _this.getLocalStorage("device_id");
|
|
821
821
|
|
|
822
822
|
if (storedDeviceId) {
|
|
823
823
|
return storedDeviceId;
|
|
@@ -830,10 +830,10 @@ function () {
|
|
|
830
830
|
var deviceId = _this.hashFingerprint(fingerprint); // 存储设备ID
|
|
831
831
|
|
|
832
832
|
|
|
833
|
-
_this.setCookie(
|
|
833
|
+
_this.setCookie("device_id", deviceId, 365 * 2); // 存储2年
|
|
834
834
|
|
|
835
835
|
|
|
836
|
-
_this.setLocalStorage(
|
|
836
|
+
_this.setLocalStorage("device_id", deviceId);
|
|
837
837
|
|
|
838
838
|
return deviceId;
|
|
839
839
|
};
|
|
@@ -890,44 +890,50 @@ function () {
|
|
|
890
890
|
|
|
891
891
|
this.getWebGLFingerprint = function () {
|
|
892
892
|
try {
|
|
893
|
-
var canvas = document.createElement(
|
|
894
|
-
var gl = canvas.getContext(
|
|
895
|
-
if (!gl) return
|
|
896
|
-
var debugInfo = gl.getExtension(
|
|
897
|
-
var vendor = debugInfo ? gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL) :
|
|
898
|
-
var renderer = debugInfo ? gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL) :
|
|
893
|
+
var canvas = document.createElement("canvas");
|
|
894
|
+
var gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
|
|
895
|
+
if (!gl) return "not-supported";
|
|
896
|
+
var debugInfo = gl.getExtension("WEBGL_debug_renderer_info");
|
|
897
|
+
var vendor = debugInfo ? gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL) : "unknown";
|
|
898
|
+
var renderer = debugInfo ? gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL) : "unknown";
|
|
899
899
|
return vendor + "|" + renderer;
|
|
900
900
|
} catch (e) {
|
|
901
|
-
return
|
|
901
|
+
return "error";
|
|
902
902
|
}
|
|
903
903
|
};
|
|
904
904
|
/**
|
|
905
905
|
* 获取Canvas指纹
|
|
906
|
+
* 注意:使用固定的尺寸和绘制参数,确保在不同时间生成一致的指纹
|
|
906
907
|
* @returns Canvas指纹字符串
|
|
907
908
|
*/
|
|
908
909
|
|
|
909
910
|
|
|
910
911
|
this.getCanvasFingerprint = function () {
|
|
911
912
|
try {
|
|
912
|
-
|
|
913
|
-
var
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
ctx
|
|
917
|
-
ctx
|
|
918
|
-
|
|
913
|
+
// 使用固定的 canvas 尺寸,确保一致性
|
|
914
|
+
var canvas = document.createElement("canvas");
|
|
915
|
+
canvas.width = 200;
|
|
916
|
+
canvas.height = 50;
|
|
917
|
+
var ctx = canvas.getContext("2d");
|
|
918
|
+
if (!ctx) return "not-supported"; // 使用固定的绘制参数,确保每次生成一致
|
|
919
|
+
|
|
920
|
+
ctx.textBaseline = "top";
|
|
921
|
+
ctx.font = "14px Arial";
|
|
922
|
+
ctx.fillStyle = "#f60";
|
|
919
923
|
ctx.fillRect(125, 1, 62, 20);
|
|
920
|
-
ctx.fillStyle =
|
|
921
|
-
ctx.fillText(
|
|
922
|
-
ctx.fillStyle =
|
|
923
|
-
ctx.fillText(
|
|
924
|
-
|
|
924
|
+
ctx.fillStyle = "#069";
|
|
925
|
+
ctx.fillText("Canvas fingerprint", 2, 15);
|
|
926
|
+
ctx.fillStyle = "rgba(102, 204, 0, 0.7)";
|
|
927
|
+
ctx.fillText("Canvas fingerprint", 4, 17); // 取后50个字符作为指纹
|
|
928
|
+
|
|
929
|
+
return canvas.toDataURL().slice(-50);
|
|
925
930
|
} catch (e) {
|
|
926
|
-
return
|
|
931
|
+
return "error";
|
|
927
932
|
}
|
|
928
933
|
};
|
|
929
934
|
/**
|
|
930
935
|
* 获取音频上下文指纹
|
|
936
|
+
* 注意:只使用稳定的 sampleRate,不使用 currentTime(会随时间变化)
|
|
931
937
|
* @returns 音频指纹字符串
|
|
932
938
|
*/
|
|
933
939
|
|
|
@@ -935,26 +941,14 @@ function () {
|
|
|
935
941
|
this.getAudioFingerprint = function () {
|
|
936
942
|
try {
|
|
937
943
|
var AudioContextClass = window.AudioContext || window.webkitAudioContext;
|
|
938
|
-
if (!AudioContextClass) return
|
|
939
|
-
var context = new AudioContextClass();
|
|
940
|
-
|
|
941
|
-
var
|
|
942
|
-
var gain = context.createGain();
|
|
943
|
-
var scriptProcessor = context.createScriptProcessor(4096, 1, 1);
|
|
944
|
-
oscillator.type = 'triangle';
|
|
945
|
-
oscillator.frequency.value = 10000;
|
|
946
|
-
gain.gain.value = 0;
|
|
947
|
-
oscillator.connect(analyser);
|
|
948
|
-
analyser.connect(scriptProcessor);
|
|
949
|
-
scriptProcessor.connect(gain);
|
|
950
|
-
gain.connect(context.destination);
|
|
951
|
-
oscillator.start(0);
|
|
952
|
-
var fingerprint = context.sampleRate + '|' + context.currentTime;
|
|
953
|
-
oscillator.stop();
|
|
944
|
+
if (!AudioContextClass) return "not-supported";
|
|
945
|
+
var context = new AudioContextClass(); // 只使用稳定的 sampleRate,不使用 currentTime(会随时间变化导致指纹不一致)
|
|
946
|
+
|
|
947
|
+
var fingerprint = String(context.sampleRate || 0);
|
|
954
948
|
context.close();
|
|
955
949
|
return fingerprint;
|
|
956
950
|
} catch (e) {
|
|
957
|
-
return
|
|
951
|
+
return "error";
|
|
958
952
|
}
|
|
959
953
|
};
|
|
960
954
|
/**
|
|
@@ -965,15 +959,15 @@ function () {
|
|
|
965
959
|
|
|
966
960
|
this.getFontFingerprint = function () {
|
|
967
961
|
try {
|
|
968
|
-
var baseFonts_1 = [
|
|
969
|
-
var testString_1 =
|
|
970
|
-
var testSize_1 =
|
|
971
|
-
var canvas = document.createElement(
|
|
972
|
-
var ctx_1 = canvas.getContext(
|
|
973
|
-
if (!ctx_1) return
|
|
962
|
+
var baseFonts_1 = ["monospace", "sans-serif", "serif"];
|
|
963
|
+
var testString_1 = "mmmmmmmmmmlli";
|
|
964
|
+
var testSize_1 = "72px";
|
|
965
|
+
var canvas = document.createElement("canvas");
|
|
966
|
+
var ctx_1 = canvas.getContext("2d");
|
|
967
|
+
if (!ctx_1) return "not-supported";
|
|
974
968
|
var detectedFonts_1 = []; // 测试字体列表
|
|
975
969
|
|
|
976
|
-
var fonts = [
|
|
970
|
+
var fonts = ["Arial", "Arial Black", "Comic Sans MS", "Courier New", "Georgia", "Helvetica", "Impact", "Times New Roman", "Trebuchet MS", "Verdana"]; // 获取基准宽度
|
|
977
971
|
|
|
978
972
|
var baseWidths_1 = {};
|
|
979
973
|
baseFonts_1.forEach(function (font) {
|
|
@@ -996,9 +990,9 @@ function () {
|
|
|
996
990
|
detectedFonts_1.push(font);
|
|
997
991
|
}
|
|
998
992
|
});
|
|
999
|
-
return detectedFonts_1.join(
|
|
993
|
+
return detectedFonts_1.join(",");
|
|
1000
994
|
} catch (e) {
|
|
1001
|
-
return
|
|
995
|
+
return "error";
|
|
1002
996
|
}
|
|
1003
997
|
};
|
|
1004
998
|
/**
|
|
@@ -1021,9 +1015,9 @@ function () {
|
|
|
1021
1015
|
}
|
|
1022
1016
|
}
|
|
1023
1017
|
|
|
1024
|
-
return plugins.join(
|
|
1018
|
+
return plugins.join(";");
|
|
1025
1019
|
} catch (e) {
|
|
1026
|
-
return
|
|
1020
|
+
return "error";
|
|
1027
1021
|
}
|
|
1028
1022
|
};
|
|
1029
1023
|
/**
|
|
@@ -1034,7 +1028,7 @@ function () {
|
|
|
1034
1028
|
|
|
1035
1029
|
this.hasLocalStorage = function () {
|
|
1036
1030
|
try {
|
|
1037
|
-
var test =
|
|
1031
|
+
var test = "__test__";
|
|
1038
1032
|
localStorage.setItem(test, test);
|
|
1039
1033
|
localStorage.removeItem(test);
|
|
1040
1034
|
return true;
|
|
@@ -1050,7 +1044,7 @@ function () {
|
|
|
1050
1044
|
|
|
1051
1045
|
this.hasSessionStorage = function () {
|
|
1052
1046
|
try {
|
|
1053
|
-
var test =
|
|
1047
|
+
var test = "__test__";
|
|
1054
1048
|
sessionStorage.setItem(test, test);
|
|
1055
1049
|
sessionStorage.removeItem(test);
|
|
1056
1050
|
return true;
|
|
@@ -1065,7 +1059,7 @@ function () {
|
|
|
1065
1059
|
|
|
1066
1060
|
|
|
1067
1061
|
this.hasIndexedDB = function () {
|
|
1068
|
-
return
|
|
1062
|
+
return "indexedDB" in window && indexedDB !== null;
|
|
1069
1063
|
};
|
|
1070
1064
|
/**
|
|
1071
1065
|
* 获取网络连接指纹
|
|
@@ -1076,10 +1070,11 @@ function () {
|
|
|
1076
1070
|
this.getConnectionFingerprint = function () {
|
|
1077
1071
|
try {
|
|
1078
1072
|
var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
|
|
1079
|
-
if (!connection) return
|
|
1080
|
-
|
|
1073
|
+
if (!connection) return "not-supported"; // 只使用稳定的 effectiveType,不使用 downlink 和 rtt(会随网络状态变化导致指纹不一致)
|
|
1074
|
+
|
|
1075
|
+
return connection.effectiveType || "unknown";
|
|
1081
1076
|
} catch (e) {
|
|
1082
|
-
return
|
|
1077
|
+
return "error";
|
|
1083
1078
|
}
|
|
1084
1079
|
};
|
|
1085
1080
|
/**
|
|
@@ -1102,12 +1097,14 @@ function () {
|
|
|
1102
1097
|
hash1 = (hash1 << 5) + hash1 + char; // hash1 * 33 + char
|
|
1103
1098
|
|
|
1104
1099
|
hash2 = (hash2 << 5) + hash2 + char; // hash2 * 33 + char
|
|
1105
|
-
} //
|
|
1100
|
+
} // 将两个32位哈希值转换为十六进制字符串并拼接,保持完整长度
|
|
1106
1101
|
|
|
1107
1102
|
|
|
1108
|
-
var
|
|
1103
|
+
var hash1Hex = (hash1 >>> 0).toString(16).padStart(8, '0'); // 32位 = 8个十六进制字符
|
|
1109
1104
|
|
|
1110
|
-
var
|
|
1105
|
+
var hash2Hex = (hash2 >>> 0).toString(16).padStart(8, '0'); // 32位 = 8个十六进制字符
|
|
1106
|
+
|
|
1107
|
+
var deviceId = "fp_" + hash1Hex + hash2Hex;
|
|
1111
1108
|
return deviceId;
|
|
1112
1109
|
};
|
|
1113
1110
|
}
|
|
@@ -1239,20 +1236,18 @@ function (_super) {
|
|
|
1239
1236
|
__extends(WebTracking, _super);
|
|
1240
1237
|
|
|
1241
1238
|
function WebTracking() {
|
|
1242
|
-
var _this = _super.call(this) || this; //
|
|
1243
|
-
|
|
1239
|
+
var _this = _super.call(this) || this; // 批量发送定时器
|
|
1244
1240
|
|
|
1245
|
-
_this.batchQueue = []; // 批量发送定时器
|
|
1246
1241
|
|
|
1247
1242
|
_this.batchTimer = null; // LocalStorage 存储 key
|
|
1248
1243
|
|
|
1249
1244
|
_this.BATCH_QUEUE_STORAGE_KEY = "web_tracking_batch_queue"; // 是否使用自定义 pageKey(如果为 true,路由变化时不会自动更新 pageKey)
|
|
1250
1245
|
|
|
1251
|
-
_this.useCustomPageKey = false; //
|
|
1246
|
+
_this.useCustomPageKey = false; // 页面卸载监听器是否已设置
|
|
1252
1247
|
|
|
1253
|
-
_this.
|
|
1248
|
+
_this.isUnloadListenerSetup = false; // 定时上报页面停留时长的定时器
|
|
1254
1249
|
|
|
1255
|
-
_this.
|
|
1250
|
+
_this.pageDurationTimer = null; // LocalStorage 存储 key(待发送请求)
|
|
1256
1251
|
|
|
1257
1252
|
_this.PENDING_REQUESTS_STORAGE_KEY = "web_tracking_pending_requests"; // 待发送请求队列最大大小(默认值,可通过配置覆盖)
|
|
1258
1253
|
|
|
@@ -1312,10 +1307,14 @@ function (_super) {
|
|
|
1312
1307
|
_this.restorePendingRequestsFromStorage(); // 无论是否启用批量发送,都需要监听页面卸载事件,确保数据发送
|
|
1313
1308
|
|
|
1314
1309
|
|
|
1315
|
-
_this.setupBeforeUnloadListener();
|
|
1310
|
+
_this.setupBeforeUnloadListener(); // 如果启用了定时上报,启动定时器
|
|
1311
|
+
|
|
1312
|
+
|
|
1313
|
+
if (_this.initConfig.autoTrackPageDurationInterval) {
|
|
1314
|
+
_this.startPageDurationTimer();
|
|
1315
|
+
}
|
|
1316
1316
|
};
|
|
1317
1317
|
/**
|
|
1318
|
-
* TODO: 需要判断有哪些不能被预制的参数
|
|
1319
1318
|
* @description 预置参数
|
|
1320
1319
|
* @param {object} PresetParams [预置参数]
|
|
1321
1320
|
*/
|
|
@@ -1341,8 +1340,14 @@ function (_super) {
|
|
|
1341
1340
|
if (key === 'pageKey') return;
|
|
1342
1341
|
|
|
1343
1342
|
if (_this.initConfig.hasOwnProperty(key)) {
|
|
1344
|
-
//
|
|
1345
|
-
_this.
|
|
1343
|
+
// 参数验证
|
|
1344
|
+
var validationResult = _this.validateConfigParam(String(key), val);
|
|
1345
|
+
|
|
1346
|
+
if (validationResult.valid) {
|
|
1347
|
+
_this.initConfig[key] = val;
|
|
1348
|
+
} else {
|
|
1349
|
+
_this.printLog("\u914D\u7F6E\u53C2\u6570\u9A8C\u8BC1\u5931\u8D25: " + String(key) + " = " + val + ", \u539F\u56E0: " + validationResult.message);
|
|
1350
|
+
}
|
|
1346
1351
|
}
|
|
1347
1352
|
});
|
|
1348
1353
|
}
|
|
@@ -1360,7 +1365,145 @@ function (_super) {
|
|
|
1360
1365
|
} else {
|
|
1361
1366
|
// 取消监听
|
|
1362
1367
|
_this.unlistener();
|
|
1368
|
+
} // 处理定时上报配置
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
if (_this.initConfig.autoTrackPageDurationInterval) {
|
|
1372
|
+
_this.startPageDurationTimer();
|
|
1373
|
+
} else {
|
|
1374
|
+
_this.stopPageDurationTimer();
|
|
1375
|
+
}
|
|
1376
|
+
};
|
|
1377
|
+
/**
|
|
1378
|
+
* @description 验证配置参数
|
|
1379
|
+
* @param key 参数名
|
|
1380
|
+
* @param value 参数值
|
|
1381
|
+
* @returns 验证结果
|
|
1382
|
+
*/
|
|
1383
|
+
|
|
1384
|
+
|
|
1385
|
+
_this.validateConfigParam = function (key, value) {
|
|
1386
|
+
switch (key) {
|
|
1387
|
+
case 'sampleRate':
|
|
1388
|
+
if (typeof value !== 'number' || value < 0 || value > 1) {
|
|
1389
|
+
return {
|
|
1390
|
+
valid: false,
|
|
1391
|
+
message: 'sampleRate 必须是 0-1 之间的数字'
|
|
1392
|
+
};
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
break;
|
|
1396
|
+
|
|
1397
|
+
case 'sendTimeout':
|
|
1398
|
+
if (typeof value !== 'number' || value <= 0) {
|
|
1399
|
+
return {
|
|
1400
|
+
valid: false,
|
|
1401
|
+
message: 'sendTimeout 必须是大于 0 的数字'
|
|
1402
|
+
};
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
break;
|
|
1406
|
+
|
|
1407
|
+
case 'batchInterval':
|
|
1408
|
+
if (typeof value !== 'number' || value <= 0) {
|
|
1409
|
+
return {
|
|
1410
|
+
valid: false,
|
|
1411
|
+
message: 'batchInterval 必须是大于 0 的数字'
|
|
1412
|
+
};
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
break;
|
|
1416
|
+
|
|
1417
|
+
case 'batchMaxSize':
|
|
1418
|
+
if (typeof value !== 'number' || value <= 0 || !Number.isInteger(value)) {
|
|
1419
|
+
return {
|
|
1420
|
+
valid: false,
|
|
1421
|
+
message: 'batchMaxSize 必须是大于 0 的整数'
|
|
1422
|
+
};
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
break;
|
|
1426
|
+
|
|
1427
|
+
case 'pendingRequestsMaxSize':
|
|
1428
|
+
if (typeof value !== 'number' || value <= 0 || !Number.isInteger(value)) {
|
|
1429
|
+
return {
|
|
1430
|
+
valid: false,
|
|
1431
|
+
message: 'pendingRequestsMaxSize 必须是大于 0 的整数'
|
|
1432
|
+
};
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
break;
|
|
1436
|
+
|
|
1437
|
+
case 'pageDurationInterval':
|
|
1438
|
+
if (typeof value !== 'number' || value <= 0) {
|
|
1439
|
+
return {
|
|
1440
|
+
valid: false,
|
|
1441
|
+
message: 'pageDurationInterval 必须是大于 0 的数字'
|
|
1442
|
+
};
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
break;
|
|
1446
|
+
|
|
1447
|
+
case 'sendMethod':
|
|
1448
|
+
if (typeof value !== 'string' || !['auto', 'xhr', 'beacon'].includes(value)) {
|
|
1449
|
+
return {
|
|
1450
|
+
valid: false,
|
|
1451
|
+
message: 'sendMethod 必须是 auto、xhr 或 beacon'
|
|
1452
|
+
};
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
break;
|
|
1456
|
+
|
|
1457
|
+
case 'showLog':
|
|
1458
|
+
case 'autoTrack':
|
|
1459
|
+
case 'isTrackSinglePage':
|
|
1460
|
+
case 'batchSend':
|
|
1461
|
+
case 'trackPartKeyClick':
|
|
1462
|
+
case 'autoTrackPageDurationInterval':
|
|
1463
|
+
if (typeof value !== 'boolean') {
|
|
1464
|
+
return {
|
|
1465
|
+
valid: false,
|
|
1466
|
+
message: key + " \u5FC5\u987B\u662F\u5E03\u5C14\u503C"
|
|
1467
|
+
};
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
break;
|
|
1471
|
+
|
|
1472
|
+
case 'business':
|
|
1473
|
+
case 'header':
|
|
1474
|
+
if (value !== null && _typeof(value) !== 'object') {
|
|
1475
|
+
return {
|
|
1476
|
+
valid: false,
|
|
1477
|
+
message: key + " \u5FC5\u987B\u662F\u5BF9\u8C61\u6216 null"
|
|
1478
|
+
};
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
break;
|
|
1482
|
+
|
|
1483
|
+
case 'contentType':
|
|
1484
|
+
if (value !== 'application/json' && value !== 'application/x-www-form-urlencoded') {
|
|
1485
|
+
return {
|
|
1486
|
+
valid: false,
|
|
1487
|
+
message: 'contentType 必须是 application/json 或 application/x-www-form-urlencoded'
|
|
1488
|
+
};
|
|
1489
|
+
}
|
|
1490
|
+
|
|
1491
|
+
break;
|
|
1492
|
+
|
|
1493
|
+
case 'platform':
|
|
1494
|
+
if (typeof value !== 'string') {
|
|
1495
|
+
return {
|
|
1496
|
+
valid: false,
|
|
1497
|
+
message: 'platform 必须是字符串'
|
|
1498
|
+
};
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
break;
|
|
1363
1502
|
}
|
|
1503
|
+
|
|
1504
|
+
return {
|
|
1505
|
+
valid: true
|
|
1506
|
+
};
|
|
1364
1507
|
};
|
|
1365
1508
|
/**
|
|
1366
1509
|
* 用户登录
|
|
@@ -1454,7 +1597,10 @@ function (_super) {
|
|
|
1454
1597
|
|
|
1455
1598
|
|
|
1456
1599
|
_this.listener = function () {
|
|
1457
|
-
//
|
|
1600
|
+
// 先移除旧的监听器,避免重复绑定
|
|
1601
|
+
_this.unlistener(); // 如果启用了全埋点,监听页面浏览事件
|
|
1602
|
+
|
|
1603
|
+
|
|
1458
1604
|
if (!!_this.initConfig.autoTrack) {
|
|
1459
1605
|
if (!!_this.initConfig.isTrackSinglePage) {
|
|
1460
1606
|
_this.rewriteHistory();
|
|
@@ -1494,7 +1640,10 @@ function (_super) {
|
|
|
1494
1640
|
_this.removeEventListener(window, "click", _this.onClickCallback); // 清理批量发送定时器
|
|
1495
1641
|
|
|
1496
1642
|
|
|
1497
|
-
_this.clearBatchTimer();
|
|
1643
|
+
_this.clearBatchTimer(); // 清理定时上报定时器
|
|
1644
|
+
|
|
1645
|
+
|
|
1646
|
+
_this.stopPageDurationTimer();
|
|
1498
1647
|
};
|
|
1499
1648
|
/**
|
|
1500
1649
|
* @description 清理批量发送定时器
|
|
@@ -1513,14 +1662,104 @@ function (_super) {
|
|
|
1513
1662
|
|
|
1514
1663
|
|
|
1515
1664
|
_this.clearBatchQueue = function () {
|
|
1516
|
-
_this.batchQueue = [];
|
|
1517
|
-
|
|
1518
1665
|
_this.setLocalStorage(_this.BATCH_QUEUE_STORAGE_KEY, "[]");
|
|
1519
1666
|
|
|
1520
1667
|
if (_this.initConfig.showLog) {
|
|
1521
1668
|
_this.printLog("批量队列已清空");
|
|
1522
1669
|
}
|
|
1523
1670
|
};
|
|
1671
|
+
/**
|
|
1672
|
+
* @description 从 LocalStorage 获取批量队列
|
|
1673
|
+
* @returns 批量队列数组
|
|
1674
|
+
*/
|
|
1675
|
+
|
|
1676
|
+
|
|
1677
|
+
_this.getBatchQueueFromStorage = function () {
|
|
1678
|
+
try {
|
|
1679
|
+
var storedQueue = _this.getLocalStorage(_this.BATCH_QUEUE_STORAGE_KEY);
|
|
1680
|
+
|
|
1681
|
+
if (storedQueue) {
|
|
1682
|
+
var parsedQueue = JSON.parse(storedQueue);
|
|
1683
|
+
|
|
1684
|
+
if (Array.isArray(parsedQueue)) {
|
|
1685
|
+
return parsedQueue;
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
} catch (e) {
|
|
1689
|
+
_this.printLog("\u8BFB\u53D6\u6279\u91CF\u961F\u5217\u5931\u8D25: " + e); // 如果解析失败,清除损坏的数据
|
|
1690
|
+
|
|
1691
|
+
|
|
1692
|
+
_this.setLocalStorage(_this.BATCH_QUEUE_STORAGE_KEY, "[]");
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
return [];
|
|
1696
|
+
};
|
|
1697
|
+
/**
|
|
1698
|
+
* @description 保存批量队列到 LocalStorage
|
|
1699
|
+
* @param queue 批量队列数组
|
|
1700
|
+
*/
|
|
1701
|
+
|
|
1702
|
+
|
|
1703
|
+
_this.saveBatchQueueToStorage = function (queue) {
|
|
1704
|
+
try {
|
|
1705
|
+
var queueString = JSON.stringify(queue); // 检查存储大小,避免超出 LocalStorage 限制
|
|
1706
|
+
|
|
1707
|
+
if (queueString.length > _this.MAX_STORAGE_SIZE) {
|
|
1708
|
+
var maxItems = Math.floor(queue.length * 0.8); // 保留 80%
|
|
1709
|
+
|
|
1710
|
+
var trimmedQueue = queue.slice(-maxItems);
|
|
1711
|
+
|
|
1712
|
+
_this.printLog("\u961F\u5217\u8FC7\u5927\uFF0C\u5DF2\u622A\u65AD\u4FDD\u7559\u6700\u65B0 " + maxItems + " \u6761\u6570\u636E\uFF08\u9650\u5236: " + _this.MAX_STORAGE_SIZE / 1024 / 1024 + "MB\uFF09");
|
|
1713
|
+
|
|
1714
|
+
_this.setLocalStorage(_this.BATCH_QUEUE_STORAGE_KEY, JSON.stringify(trimmedQueue));
|
|
1715
|
+
} else {
|
|
1716
|
+
_this.setLocalStorage(_this.BATCH_QUEUE_STORAGE_KEY, queueString);
|
|
1717
|
+
}
|
|
1718
|
+
} catch (e) {
|
|
1719
|
+
// LocalStorage 可能已满或不可用
|
|
1720
|
+
_this.printLog("\u4FDD\u5B58\u6279\u91CF\u961F\u5217\u5230 LocalStorage \u5931\u8D25: " + e);
|
|
1721
|
+
}
|
|
1722
|
+
};
|
|
1723
|
+
/**
|
|
1724
|
+
* @description 从 LocalStorage 获取待发送请求队列
|
|
1725
|
+
* @returns 待发送请求队列数组
|
|
1726
|
+
*/
|
|
1727
|
+
|
|
1728
|
+
|
|
1729
|
+
_this.getPendingRequestsFromStorage = function () {
|
|
1730
|
+
try {
|
|
1731
|
+
var storedRequests = _this.getLocalStorage(_this.PENDING_REQUESTS_STORAGE_KEY);
|
|
1732
|
+
|
|
1733
|
+
if (storedRequests) {
|
|
1734
|
+
var parsedRequests = JSON.parse(storedRequests);
|
|
1735
|
+
|
|
1736
|
+
if (Array.isArray(parsedRequests)) {
|
|
1737
|
+
return parsedRequests;
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
} catch (e) {
|
|
1741
|
+
_this.printLog("\u8BFB\u53D6\u5F85\u53D1\u9001\u8BF7\u6C42\u5931\u8D25: " + e); // 如果解析失败,清除损坏的数据
|
|
1742
|
+
|
|
1743
|
+
|
|
1744
|
+
_this.setLocalStorage(_this.PENDING_REQUESTS_STORAGE_KEY, "[]");
|
|
1745
|
+
}
|
|
1746
|
+
|
|
1747
|
+
return [];
|
|
1748
|
+
};
|
|
1749
|
+
/**
|
|
1750
|
+
* @description 保存待发送请求队列到 LocalStorage
|
|
1751
|
+
* @param requests 待发送请求队列数组
|
|
1752
|
+
*/
|
|
1753
|
+
|
|
1754
|
+
|
|
1755
|
+
_this.savePendingRequestsToStorage = function (requests) {
|
|
1756
|
+
try {
|
|
1757
|
+
_this.setLocalStorage(_this.PENDING_REQUESTS_STORAGE_KEY, JSON.stringify(requests));
|
|
1758
|
+
} catch (e) {
|
|
1759
|
+
// LocalStorage 可能已满或不可用
|
|
1760
|
+
_this.printLog("\u4FDD\u5B58\u5F85\u53D1\u9001\u8BF7\u6C42\u5230 LocalStorage \u5931\u8D25: " + e);
|
|
1761
|
+
}
|
|
1762
|
+
};
|
|
1524
1763
|
/**
|
|
1525
1764
|
* @description 设置自定义页面唯一标识
|
|
1526
1765
|
* @param pageKey 页面唯一标识,如果传入 null 或空字符串,则恢复自动生成
|
|
@@ -1589,7 +1828,7 @@ function (_super) {
|
|
|
1589
1828
|
}
|
|
1590
1829
|
});
|
|
1591
1830
|
|
|
1592
|
-
|
|
1831
|
+
_this.sendData(params);
|
|
1593
1832
|
};
|
|
1594
1833
|
/**
|
|
1595
1834
|
* @description 路由触发事件
|
|
@@ -1600,7 +1839,11 @@ function (_super) {
|
|
|
1600
1839
|
var _a, _b; // 在路由变化前,先发送待发送的数据(避免被取消)
|
|
1601
1840
|
|
|
1602
1841
|
|
|
1603
|
-
|
|
1842
|
+
var pendingRequests = _this.getPendingRequestsFromStorage();
|
|
1843
|
+
|
|
1844
|
+
var batchQueue = _this.initConfig.batchSend ? _this.getBatchQueueFromStorage() : [];
|
|
1845
|
+
|
|
1846
|
+
if (pendingRequests.length > 0 || batchQueue.length > 0) {
|
|
1604
1847
|
_this.flushPendingData();
|
|
1605
1848
|
}
|
|
1606
1849
|
|
|
@@ -1619,6 +1862,13 @@ function (_super) {
|
|
|
1619
1862
|
|
|
1620
1863
|
if (!_this.useCustomPageKey) {
|
|
1621
1864
|
_this.pageKey = window.location.pathname.replace(/\//g, "_").substring(1);
|
|
1865
|
+
} // 路由变化时,如果启用了定时上报,需要重启定时器
|
|
1866
|
+
|
|
1867
|
+
|
|
1868
|
+
if (_this.initConfig.autoTrackPageDurationInterval) {
|
|
1869
|
+
_this.stopPageDurationTimer();
|
|
1870
|
+
|
|
1871
|
+
_this.startPageDurationTimer();
|
|
1622
1872
|
}
|
|
1623
1873
|
|
|
1624
1874
|
_this.sendRetained(e.type);
|
|
@@ -1708,16 +1958,42 @@ function (_super) {
|
|
|
1708
1958
|
|
|
1709
1959
|
|
|
1710
1960
|
_this.flushBatchQueue = function () {
|
|
1711
|
-
|
|
1961
|
+
var batchQueue = _this.getBatchQueueFromStorage();
|
|
1962
|
+
|
|
1963
|
+
if (batchQueue.length === 0) return;
|
|
1964
|
+
|
|
1965
|
+
var currentTime = _this.getTimeStamp(); // 过滤出可以发送的数据(未到重试时间的不发送)
|
|
1966
|
+
|
|
1712
1967
|
|
|
1713
|
-
var
|
|
1968
|
+
var readyToSend = batchQueue.filter(function (item) {
|
|
1969
|
+
if (!item._nextRetryTime) {
|
|
1970
|
+
return true;
|
|
1971
|
+
}
|
|
1714
1972
|
|
|
1715
|
-
|
|
1973
|
+
return item._nextRetryTime <= currentTime;
|
|
1974
|
+
});
|
|
1716
1975
|
|
|
1717
|
-
|
|
1976
|
+
if (readyToSend.length === 0) {
|
|
1977
|
+
if (_this.initConfig.showLog) {
|
|
1978
|
+
_this.printLog("\u6279\u91CF\u961F\u5217\u4E2D\u6709 " + batchQueue.length + " \u6761\u6570\u636E\u7B49\u5F85\u91CD\u8BD5");
|
|
1979
|
+
}
|
|
1980
|
+
|
|
1981
|
+
return;
|
|
1982
|
+
} // 从队列中移除已准备发送的数据
|
|
1718
1983
|
|
|
1719
1984
|
|
|
1720
|
-
|
|
1985
|
+
var remainingQueue = batchQueue.filter(function (item) {
|
|
1986
|
+
if (!item._nextRetryTime) {
|
|
1987
|
+
return false;
|
|
1988
|
+
}
|
|
1989
|
+
|
|
1990
|
+
return item._nextRetryTime > currentTime;
|
|
1991
|
+
}); // 保存剩余的队列
|
|
1992
|
+
|
|
1993
|
+
_this.saveBatchQueueToStorage(remainingQueue); // 发送批量数据
|
|
1994
|
+
|
|
1995
|
+
|
|
1996
|
+
_this.sendBatchData(readyToSend);
|
|
1721
1997
|
};
|
|
1722
1998
|
/**
|
|
1723
1999
|
* 发送批量数据
|
|
@@ -1729,7 +2005,9 @@ function (_super) {
|
|
|
1729
2005
|
var _a = _this.initConfig,
|
|
1730
2006
|
serverUrl = _a.serverUrl,
|
|
1731
2007
|
contentType = _a.contentType,
|
|
1732
|
-
showLog = _a.showLog
|
|
2008
|
+
showLog = _a.showLog,
|
|
2009
|
+
sendMethod = _a.sendMethod,
|
|
2010
|
+
initHeader = _a.header;
|
|
1733
2011
|
|
|
1734
2012
|
if (showLog) {
|
|
1735
2013
|
_this.printLog("\u6279\u91CF\u53D1\u9001 " + data.length + " \u6761\u6570\u636E");
|
|
@@ -1737,44 +2015,126 @@ function (_super) {
|
|
|
1737
2015
|
data.forEach(function (item) {
|
|
1738
2016
|
return _this.printLog(item);
|
|
1739
2017
|
});
|
|
1740
|
-
} //
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
2018
|
+
} // 判断是否使用 sendBeacon
|
|
2019
|
+
|
|
2020
|
+
|
|
2021
|
+
var shouldUseBeacon = _this.shouldUseBeacon(sendMethod, undefined, initHeader); // 如果使用 sendBeacon
|
|
2022
|
+
|
|
2023
|
+
|
|
2024
|
+
if (shouldUseBeacon) {
|
|
2025
|
+
try {
|
|
2026
|
+
var blob = new Blob([JSON.stringify(data)], {
|
|
2027
|
+
type: contentType || "application/json"
|
|
2028
|
+
});
|
|
2029
|
+
var sent = navigator.sendBeacon(serverUrl, blob);
|
|
2030
|
+
|
|
2031
|
+
if (sent) {
|
|
2032
|
+
// 发送成功,确保 LocalStorage 已清空
|
|
2033
|
+
_this.saveBatchQueueToStorage([]);
|
|
2034
|
+
|
|
2035
|
+
if (showLog) {
|
|
2036
|
+
_this.printLog("\u6279\u91CF\u53D1\u9001\u6210\u529F: " + data.length + " \u6761\u6570\u636E");
|
|
2037
|
+
}
|
|
2038
|
+
} else {
|
|
2039
|
+
// sendBeacon 返回 false,重新加入队列以便重试
|
|
2040
|
+
_this.printLog("\u6279\u91CF\u53D1\u9001\u5931\u8D25: sendBeacon \u8FD4\u56DE false\uFF0C\u6570\u636E\u5DF2\u91CD\u65B0\u52A0\u5165\u961F\u5217");
|
|
2041
|
+
|
|
2042
|
+
_this.retryBatchData(data);
|
|
2043
|
+
}
|
|
2044
|
+
} catch (e) {
|
|
2045
|
+
// sendBeacon 失败,重新加入队列以便重试
|
|
2046
|
+
_this.printLog("\u6279\u91CF\u53D1\u9001\u5931\u8D25: " + e + "\uFF0C\u6570\u636E\u5DF2\u91CD\u65B0\u52A0\u5165\u961F\u5217");
|
|
2047
|
+
|
|
2048
|
+
_this.retryBatchData(data);
|
|
2049
|
+
}
|
|
2050
|
+
} else {
|
|
2051
|
+
// 使用 XMLHttpRequest 发送
|
|
2052
|
+
_this.ajax({
|
|
2053
|
+
url: serverUrl,
|
|
2054
|
+
type: "POST",
|
|
2055
|
+
data: JSON.stringify({
|
|
2056
|
+
events: data
|
|
2057
|
+
}),
|
|
2058
|
+
contentType: contentType,
|
|
2059
|
+
credentials: false,
|
|
2060
|
+
timeout: _this.initConfig.sendTimeout,
|
|
2061
|
+
cors: true,
|
|
2062
|
+
success: function success() {
|
|
2063
|
+
// 批量发送成功,确保 LocalStorage 已清空
|
|
2064
|
+
// flushBatchQueue 在发送前已清空 LocalStorage,这里再次确认
|
|
2065
|
+
_this.saveBatchQueueToStorage([]);
|
|
2066
|
+
|
|
2067
|
+
if (_this.initConfig.showLog) {
|
|
2068
|
+
_this.printLog("\u6279\u91CF\u53D1\u9001\u6210\u529F: " + data.length + " \u6761\u6570\u636E");
|
|
2069
|
+
}
|
|
2070
|
+
},
|
|
2071
|
+
error: function error(err) {
|
|
2072
|
+
// 批量发送失败,重新加入队列以便重试
|
|
2073
|
+
_this.printLog("\u6279\u91CF\u53D1\u9001\u5931\u8D25: " + err + "\uFF0C\u6570\u636E\u5DF2\u91CD\u65B0\u52A0\u5165\u961F\u5217");
|
|
2074
|
+
|
|
2075
|
+
_this.retryBatchData(data);
|
|
2076
|
+
}
|
|
2077
|
+
});
|
|
2078
|
+
}
|
|
2079
|
+
};
|
|
2080
|
+
/**
|
|
2081
|
+
* @description 批量数据重试逻辑
|
|
2082
|
+
* @param data 批量数据
|
|
2083
|
+
*/
|
|
2084
|
+
|
|
2085
|
+
|
|
2086
|
+
_this.retryBatchData = function (data) {
|
|
2087
|
+
// 获取当前队列
|
|
2088
|
+
var currentQueue = _this.getBatchQueueFromStorage(); // 去重:基于事件类型、itemKey、requestTime 生成唯一键
|
|
2089
|
+
|
|
2090
|
+
|
|
2091
|
+
var getEventKey = function getEventKey(item) {
|
|
2092
|
+
return item.event + "_" + item.itemKey + "_" + item.requestTime;
|
|
2093
|
+
};
|
|
2094
|
+
|
|
2095
|
+
var existingKeys = new Set(currentQueue.map(getEventKey));
|
|
2096
|
+
var maxRetryCount = 3; // 最大重试次数
|
|
2097
|
+
|
|
2098
|
+
var currentTime = _this.getTimeStamp(); // 过滤并更新重试信息
|
|
2099
|
+
|
|
2100
|
+
|
|
2101
|
+
var retryData = data.filter(function (item) {
|
|
2102
|
+
var key = getEventKey(item); // 检查是否已存在
|
|
2103
|
+
|
|
2104
|
+
if (existingKeys.has(key)) {
|
|
2105
|
+
return false;
|
|
2106
|
+
} // 检查重试次数
|
|
2107
|
+
|
|
2108
|
+
|
|
2109
|
+
var retryCount = (item._retryCount || 0) + 1;
|
|
2110
|
+
|
|
2111
|
+
if (retryCount > maxRetryCount) {
|
|
1756
2112
|
if (_this.initConfig.showLog) {
|
|
1757
|
-
_this.printLog("\
|
|
2113
|
+
_this.printLog("\u6570\u636E\u5DF2\u8FBE\u5230\u6700\u5927\u91CD\u8BD5\u6B21\u6570\uFF0C\u653E\u5F03\u91CD\u8BD5: " + key);
|
|
1758
2114
|
}
|
|
1759
|
-
},
|
|
1760
|
-
error: function error(err) {
|
|
1761
|
-
var _a; // 批量发送失败,重新加入队列以便重试
|
|
1762
2115
|
|
|
2116
|
+
return false;
|
|
2117
|
+
} // 更新重试信息
|
|
1763
2118
|
|
|
1764
|
-
_this.printLog("\u6279\u91CF\u53D1\u9001\u5931\u8D25: " + err + "\uFF0C\u6570\u636E\u5DF2\u91CD\u65B0\u52A0\u5165\u961F\u5217"); // 将失败的数据重新加入队列,避免数据丢失
|
|
1765
2119
|
|
|
2120
|
+
item._retryCount = retryCount; // 指数退避:2^retryCount * 1000ms
|
|
1766
2121
|
|
|
1767
|
-
|
|
2122
|
+
item._nextRetryTime = currentTime + Math.pow(2, retryCount) * 1000;
|
|
2123
|
+
existingKeys.add(key);
|
|
2124
|
+
return true;
|
|
2125
|
+
}); // 将失败的数据重新加入队列(添加到队列前面,优先重试)
|
|
1768
2126
|
|
|
2127
|
+
var retryQueue = __spreadArray(__spreadArray([], retryData), currentQueue); // 限制重试队列大小,避免内存溢出
|
|
1769
2128
|
|
|
1770
|
-
if (_this.batchQueue.length > _this.initConfig.batchMaxSize * 2) {
|
|
1771
|
-
_this.batchQueue = _this.batchQueue.slice(0, _this.initConfig.batchMaxSize);
|
|
1772
|
-
} // 保存失败的数据到 LocalStorage
|
|
1773
2129
|
|
|
2130
|
+
var maxSize = _this.initConfig.batchMaxSize * 2;
|
|
2131
|
+
var trimmedQueue = retryQueue.length > maxSize ? retryQueue.slice(0, maxSize) : retryQueue; // 保存失败的数据到 LocalStorage,确保数据不丢失
|
|
1774
2132
|
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
2133
|
+
_this.saveBatchQueueToStorage(trimmedQueue);
|
|
2134
|
+
|
|
2135
|
+
if (_this.initConfig.showLog) {
|
|
2136
|
+
_this.printLog("\u5DF2\u5C06 " + retryData.length + " \u6761\u6570\u636E\u52A0\u5165\u91CD\u8BD5\u961F\u5217");
|
|
2137
|
+
}
|
|
1778
2138
|
};
|
|
1779
2139
|
/**
|
|
1780
2140
|
* 添加到批量队列
|
|
@@ -1785,15 +2145,26 @@ function (_super) {
|
|
|
1785
2145
|
_this.addToBatchQueue = function (params) {
|
|
1786
2146
|
var _a = _this.initConfig,
|
|
1787
2147
|
batchInterval = _a.batchInterval,
|
|
1788
|
-
batchMaxSize = _a.batchMaxSize;
|
|
2148
|
+
batchMaxSize = _a.batchMaxSize; // 数据采样判断(在添加到队列前判断)
|
|
2149
|
+
|
|
2150
|
+
if (!_this.shouldSample()) {
|
|
2151
|
+
if (_this.initConfig.showLog) {
|
|
2152
|
+
_this.printLog("数据已采样跳过(批量模式)");
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
return;
|
|
2156
|
+
} // 从 LocalStorage 获取当前队列
|
|
1789
2157
|
|
|
1790
|
-
_this.batchQueue.push(params); // 保存到 LocalStorage
|
|
1791
2158
|
|
|
2159
|
+
var currentQueue = _this.getBatchQueueFromStorage(); // 添加新数据
|
|
1792
2160
|
|
|
1793
|
-
_this.saveBatchQueueToStorage(); // 如果队列达到最大数量,立即发送
|
|
1794
2161
|
|
|
2162
|
+
currentQueue.push(params); // 保存到 LocalStorage
|
|
1795
2163
|
|
|
1796
|
-
|
|
2164
|
+
_this.saveBatchQueueToStorage(currentQueue); // 如果队列达到最大数量,立即发送
|
|
2165
|
+
|
|
2166
|
+
|
|
2167
|
+
if (currentQueue.length >= batchMaxSize) {
|
|
1797
2168
|
_this.flushBatchQueue();
|
|
1798
2169
|
|
|
1799
2170
|
return;
|
|
@@ -1814,43 +2185,30 @@ function (_super) {
|
|
|
1814
2185
|
|
|
1815
2186
|
|
|
1816
2187
|
_this.restoreBatchQueueFromStorage = function () {
|
|
1817
|
-
|
|
1818
|
-
var storedQueue = _this.getLocalStorage(_this.BATCH_QUEUE_STORAGE_KEY);
|
|
1819
|
-
|
|
1820
|
-
if (storedQueue) {
|
|
1821
|
-
var parsedQueue = JSON.parse(storedQueue);
|
|
2188
|
+
var batchQueue = _this.getBatchQueueFromStorage();
|
|
1822
2189
|
|
|
1823
|
-
|
|
1824
|
-
|
|
2190
|
+
if (batchQueue.length > 0) {
|
|
2191
|
+
if (_this.initConfig.showLog) {
|
|
2192
|
+
_this.printLog("\u4ECE LocalStorage \u6062\u590D " + batchQueue.length + " \u6761\u5F85\u53D1\u9001\u6570\u636E");
|
|
2193
|
+
} // 恢复后立即尝试发送(如果达到条件)
|
|
1825
2194
|
|
|
1826
|
-
if (_this.initConfig.showLog) {
|
|
1827
|
-
_this.printLog("\u4ECE LocalStorage \u6062\u590D " + parsedQueue.length + " \u6761\u5F85\u53D1\u9001\u6570\u636E");
|
|
1828
|
-
} // 恢复后立即尝试发送(如果达到条件)
|
|
1829
2195
|
|
|
2196
|
+
var batchMaxSize = _this.initConfig.batchMaxSize;
|
|
1830
2197
|
|
|
1831
|
-
|
|
2198
|
+
if (batchQueue.length >= batchMaxSize) {
|
|
2199
|
+
_this.flushBatchQueue();
|
|
2200
|
+
} else {
|
|
2201
|
+
// 设置定时发送
|
|
2202
|
+
var batchInterval = _this.initConfig.batchInterval;
|
|
1832
2203
|
|
|
1833
|
-
|
|
2204
|
+
if (!_this.batchTimer) {
|
|
2205
|
+
_this.batchTimer = window.setTimeout(function () {
|
|
1834
2206
|
_this.flushBatchQueue();
|
|
1835
|
-
} else {
|
|
1836
|
-
// 设置定时发送
|
|
1837
|
-
var batchInterval = _this.initConfig.batchInterval;
|
|
1838
|
-
|
|
1839
|
-
if (!_this.batchTimer) {
|
|
1840
|
-
_this.batchTimer = window.setTimeout(function () {
|
|
1841
|
-
_this.flushBatchQueue();
|
|
1842
2207
|
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
}
|
|
1846
|
-
}
|
|
2208
|
+
_this.batchTimer = null;
|
|
2209
|
+
}, batchInterval);
|
|
1847
2210
|
}
|
|
1848
2211
|
}
|
|
1849
|
-
} catch (e) {
|
|
1850
|
-
_this.printLog("\u6062\u590D\u6279\u91CF\u961F\u5217\u5931\u8D25: " + e); // 如果解析失败,清除损坏的数据
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
_this.setLocalStorage(_this.BATCH_QUEUE_STORAGE_KEY, "[]");
|
|
1854
2212
|
}
|
|
1855
2213
|
};
|
|
1856
2214
|
/**
|
|
@@ -1860,17 +2218,24 @@ function (_super) {
|
|
|
1860
2218
|
|
|
1861
2219
|
|
|
1862
2220
|
_this.addToPendingRequests = function (params) {
|
|
1863
|
-
|
|
2221
|
+
// 从 LocalStorage 获取当前队列
|
|
2222
|
+
var currentRequests = _this.getPendingRequestsFromStorage(); // 添加新数据
|
|
1864
2223
|
|
|
1865
2224
|
|
|
2225
|
+
currentRequests.push(params); // 限制队列大小,防止内存溢出
|
|
2226
|
+
|
|
1866
2227
|
var maxSize = _this.initConfig.pendingRequestsMaxSize || _this.DEFAULT_PENDING_REQUESTS_MAX_SIZE;
|
|
1867
2228
|
|
|
1868
|
-
if (
|
|
1869
|
-
|
|
2229
|
+
if (currentRequests.length > maxSize) {
|
|
2230
|
+
var trimmedRequests = currentRequests.slice(-maxSize);
|
|
1870
2231
|
|
|
1871
2232
|
if (_this.initConfig.showLog) {
|
|
1872
2233
|
_this.printLog("\u5F85\u53D1\u9001\u8BF7\u6C42\u961F\u5217\u5DF2\u6EE1\uFF0C\u5DF2\u79FB\u9664\u6700\u65E7\u7684\u6570\u636E\uFF08\u6700\u5927\u9650\u5236: " + maxSize + "\uFF09");
|
|
1873
2234
|
}
|
|
2235
|
+
|
|
2236
|
+
_this.savePendingRequestsToStorage(trimmedRequests);
|
|
2237
|
+
} else {
|
|
2238
|
+
_this.savePendingRequestsToStorage(currentRequests);
|
|
1874
2239
|
}
|
|
1875
2240
|
};
|
|
1876
2241
|
/**
|
|
@@ -1879,30 +2244,15 @@ function (_super) {
|
|
|
1879
2244
|
|
|
1880
2245
|
|
|
1881
2246
|
_this.restorePendingRequestsFromStorage = function () {
|
|
1882
|
-
|
|
1883
|
-
var storedRequests = _this.getLocalStorage(_this.PENDING_REQUESTS_STORAGE_KEY);
|
|
1884
|
-
|
|
1885
|
-
if (storedRequests) {
|
|
1886
|
-
var parsedRequests = JSON.parse(storedRequests);
|
|
1887
|
-
|
|
1888
|
-
if (Array.isArray(parsedRequests) && parsedRequests.length > 0) {
|
|
1889
|
-
_this.pendingRequests = parsedRequests;
|
|
1890
|
-
|
|
1891
|
-
if (_this.initConfig.showLog) {
|
|
1892
|
-
_this.printLog("\u4ECE LocalStorage \u6062\u590D " + parsedRequests.length + " \u6761\u5F85\u53D1\u9001\u8BF7\u6C42");
|
|
1893
|
-
} // 恢复后立即尝试发送
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
if (_this.pendingRequests.length > 0) {
|
|
1897
|
-
_this.flushPendingRequests();
|
|
1898
|
-
}
|
|
1899
|
-
}
|
|
1900
|
-
}
|
|
1901
|
-
} catch (e) {
|
|
1902
|
-
_this.printLog("\u6062\u590D\u5F85\u53D1\u9001\u8BF7\u6C42\u5931\u8D25: " + e); // 如果解析失败,清除损坏的数据
|
|
2247
|
+
var pendingRequests = _this.getPendingRequestsFromStorage();
|
|
1903
2248
|
|
|
2249
|
+
if (pendingRequests.length > 0) {
|
|
2250
|
+
if (_this.initConfig.showLog) {
|
|
2251
|
+
_this.printLog("\u4ECE LocalStorage \u6062\u590D " + pendingRequests.length + " \u6761\u5F85\u53D1\u9001\u8BF7\u6C42");
|
|
2252
|
+
} // 注意:恢复后不立即发送,避免重复
|
|
2253
|
+
// 数据会留在 LocalStorage 中,等待下次正常发送或页面卸载时发送
|
|
2254
|
+
// 这样可以避免与批量队列冲突
|
|
1904
2255
|
|
|
1905
|
-
_this.setLocalStorage(_this.PENDING_REQUESTS_STORAGE_KEY, "[]");
|
|
1906
2256
|
}
|
|
1907
2257
|
};
|
|
1908
2258
|
/**
|
|
@@ -1939,55 +2289,63 @@ function (_super) {
|
|
|
1939
2289
|
};
|
|
1940
2290
|
/**
|
|
1941
2291
|
* 刷新待发送的单个请求(正常情况下的发送)
|
|
1942
|
-
*
|
|
2292
|
+
* 注意:这个方法会直接使用 ajax 发送,避免通过 sendData 导致重复
|
|
1943
2293
|
*/
|
|
1944
2294
|
|
|
1945
2295
|
|
|
1946
2296
|
_this.flushPendingRequests = function () {
|
|
1947
|
-
|
|
1948
|
-
return;
|
|
1949
|
-
}
|
|
2297
|
+
var pendingRequests = _this.getPendingRequestsFromStorage();
|
|
1950
2298
|
|
|
1951
|
-
|
|
2299
|
+
if (pendingRequests.length === 0) {
|
|
2300
|
+
return;
|
|
2301
|
+
} // 清除 LocalStorage 中的待发送请求
|
|
1952
2302
|
|
|
1953
|
-
_this.pendingRequests = []; // 清除 LocalStorage 中的待发送请求
|
|
1954
2303
|
|
|
1955
|
-
_this.
|
|
2304
|
+
_this.savePendingRequestsToStorage([]); // 直接使用 ajax 发送每个请求,避免通过 sendData 导致重复
|
|
1956
2305
|
|
|
1957
2306
|
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
2307
|
+
var _a = _this.initConfig,
|
|
2308
|
+
serverUrl = _a.serverUrl,
|
|
2309
|
+
sendTimeout = _a.sendTimeout,
|
|
2310
|
+
contentType = _a.contentType,
|
|
2311
|
+
showLog = _a.showLog,
|
|
2312
|
+
initHeader = _a.header;
|
|
2313
|
+
pendingRequests.forEach(function (params) {
|
|
2314
|
+
// 数据采样判断
|
|
2315
|
+
if (!_this.shouldSample()) {
|
|
2316
|
+
if (showLog) {
|
|
2317
|
+
_this.printLog("待发送请求已采样跳过");
|
|
1964
2318
|
}
|
|
1965
|
-
});
|
|
1966
|
-
});
|
|
1967
|
-
};
|
|
1968
|
-
/**
|
|
1969
|
-
* 保存批量队列到 LocalStorage
|
|
1970
|
-
*/
|
|
1971
2319
|
|
|
2320
|
+
return;
|
|
2321
|
+
}
|
|
1972
2322
|
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
// 如果队列过大,只保留最新的数据
|
|
1977
|
-
|
|
1978
|
-
if (queueString.length > _this.MAX_STORAGE_SIZE) {
|
|
1979
|
-
var maxItems = Math.floor(_this.batchQueue.length * 0.8); // 保留 80%
|
|
2323
|
+
if (showLog) {
|
|
2324
|
+
_this.printLog(params);
|
|
2325
|
+
} // 直接使用 ajax 发送
|
|
1980
2326
|
|
|
1981
|
-
_this.batchQueue = _this.batchQueue.slice(-maxItems);
|
|
1982
2327
|
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
2328
|
+
_this.ajax({
|
|
2329
|
+
header: initHeader,
|
|
2330
|
+
url: serverUrl,
|
|
2331
|
+
type: "POST",
|
|
2332
|
+
data: JSON.stringify(params),
|
|
2333
|
+
contentType: contentType,
|
|
2334
|
+
credentials: false,
|
|
2335
|
+
timeout: sendTimeout,
|
|
2336
|
+
cors: true,
|
|
2337
|
+
success: function success() {
|
|
2338
|
+
if (showLog) {
|
|
2339
|
+
_this.printLog("待发送请求发送成功");
|
|
2340
|
+
}
|
|
2341
|
+
},
|
|
2342
|
+
error: function error(err) {
|
|
2343
|
+
if (showLog) {
|
|
2344
|
+
_this.printLog("\u5F85\u53D1\u9001\u8BF7\u6C42\u53D1\u9001\u5931\u8D25\uFF08\u4E0D\u518D\u91CD\u8BD5\uFF09: " + err);
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
});
|
|
2348
|
+
});
|
|
1991
2349
|
};
|
|
1992
2350
|
/**
|
|
1993
2351
|
* 设置页面卸载监听器,确保数据发送
|
|
@@ -2015,100 +2373,100 @@ function (_super) {
|
|
|
2015
2373
|
window.addEventListener("pagehide", function () {
|
|
2016
2374
|
_this.flushPendingData();
|
|
2017
2375
|
});
|
|
2018
|
-
};
|
|
2376
|
+
}; // 标记是否正在刷新待发送数据,避免重复发送
|
|
2377
|
+
|
|
2378
|
+
|
|
2379
|
+
_this.isFlushingPendingData = false;
|
|
2019
2380
|
/**
|
|
2020
2381
|
* 刷新待发送数据(在页面卸载/跳转时调用)
|
|
2021
2382
|
*/
|
|
2022
2383
|
|
|
2023
|
-
|
|
2024
2384
|
_this.flushPendingData = function () {
|
|
2025
|
-
//
|
|
2385
|
+
// 如果正在刷新,避免重复执行
|
|
2386
|
+
if (_this.isFlushingPendingData) {
|
|
2387
|
+
return;
|
|
2388
|
+
} // 页面卸载时停止定时器
|
|
2389
|
+
|
|
2390
|
+
|
|
2391
|
+
_this.stopPageDurationTimer(); // 收集所有待发送的数据
|
|
2392
|
+
|
|
2393
|
+
|
|
2026
2394
|
var allPendingData = []; // 如果有批量队列,添加到待发送列表
|
|
2027
2395
|
|
|
2028
|
-
|
|
2029
|
-
|
|
2396
|
+
var batchQueue = _this.getBatchQueueFromStorage();
|
|
2397
|
+
|
|
2398
|
+
if (batchQueue.length > 0) {
|
|
2399
|
+
allPendingData.push.apply(allPendingData, batchQueue);
|
|
2030
2400
|
} // 如果有待发送的单个请求,也添加到列表
|
|
2031
2401
|
|
|
2032
2402
|
|
|
2033
|
-
|
|
2034
|
-
|
|
2403
|
+
var pendingRequests = _this.getPendingRequestsFromStorage();
|
|
2404
|
+
|
|
2405
|
+
if (pendingRequests.length > 0) {
|
|
2406
|
+
allPendingData.push.apply(allPendingData, pendingRequests);
|
|
2035
2407
|
}
|
|
2036
2408
|
|
|
2037
2409
|
if (allPendingData.length === 0) {
|
|
2038
2410
|
return;
|
|
2411
|
+
} // 标记正在刷新
|
|
2412
|
+
|
|
2413
|
+
|
|
2414
|
+
_this.isFlushingPendingData = true; // 先保存到 LocalStorage,确保数据不丢失(在发送前保存)
|
|
2415
|
+
|
|
2416
|
+
try {
|
|
2417
|
+
if (_this.initConfig.batchSend) {
|
|
2418
|
+
_this.setLocalStorage(_this.BATCH_QUEUE_STORAGE_KEY, JSON.stringify(allPendingData));
|
|
2419
|
+
} else {
|
|
2420
|
+
_this.setLocalStorage(_this.PENDING_REQUESTS_STORAGE_KEY, JSON.stringify(allPendingData));
|
|
2421
|
+
}
|
|
2422
|
+
} catch (e) {
|
|
2423
|
+
if (_this.initConfig.showLog) {
|
|
2424
|
+
_this.printLog("\u4FDD\u5B58\u5F85\u53D1\u9001\u8BF7\u6C42\u5230 LocalStorage \u5931\u8D25: " + e);
|
|
2425
|
+
}
|
|
2039
2426
|
} // 使用 sendBeacon 发送数据(最可靠的方式)
|
|
2040
2427
|
|
|
2041
2428
|
|
|
2042
2429
|
if (navigator.sendBeacon && _this.initConfig.serverUrl) {
|
|
2043
2430
|
try {
|
|
2044
2431
|
// 如果只有一条数据,直接发送;否则批量发送
|
|
2045
|
-
var dataToSend = allPendingData.length === 1 ? allPendingData[0] :
|
|
2046
|
-
events: allPendingData
|
|
2047
|
-
};
|
|
2432
|
+
var dataToSend = allPendingData.length === 1 ? allPendingData[0] : allPendingData;
|
|
2048
2433
|
var blob = new Blob([JSON.stringify(dataToSend)], {
|
|
2049
2434
|
type: _this.initConfig.contentType || "application/json"
|
|
2050
2435
|
});
|
|
2051
2436
|
var sent = navigator.sendBeacon(_this.initConfig.serverUrl, blob);
|
|
2052
2437
|
|
|
2053
2438
|
if (sent) {
|
|
2054
|
-
//
|
|
2055
|
-
_this.batchQueue = [];
|
|
2056
|
-
_this.pendingRequests = [];
|
|
2057
|
-
|
|
2439
|
+
// 发送成功,清除所有 LocalStorage
|
|
2058
2440
|
_this.setLocalStorage(_this.BATCH_QUEUE_STORAGE_KEY, "[]");
|
|
2059
2441
|
|
|
2442
|
+
_this.setLocalStorage(_this.PENDING_REQUESTS_STORAGE_KEY, "[]");
|
|
2443
|
+
|
|
2060
2444
|
if (_this.initConfig.showLog) {
|
|
2061
2445
|
_this.printLog("\u9875\u9762\u5378\u8F7D\u65F6\u6210\u529F\u53D1\u9001 " + allPendingData.length + " \u6761\u6570\u636E");
|
|
2062
2446
|
}
|
|
2063
2447
|
} else {
|
|
2064
|
-
// sendBeacon 返回 false
|
|
2065
|
-
if (_this.initConfig.
|
|
2066
|
-
_this.
|
|
2067
|
-
} // 保存 pendingRequests 到 LocalStorage(如果支持)
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
if (_this.pendingRequests.length > 0) {
|
|
2071
|
-
try {
|
|
2072
|
-
_this.setLocalStorage(_this.PENDING_REQUESTS_STORAGE_KEY, JSON.stringify(_this.pendingRequests));
|
|
2073
|
-
} catch (e) {
|
|
2074
|
-
// LocalStorage 可能已满或不可用
|
|
2075
|
-
if (_this.initConfig.showLog) {
|
|
2076
|
-
_this.printLog("\u4FDD\u5B58\u5F85\u53D1\u9001\u8BF7\u6C42\u5230 LocalStorage \u5931\u8D25: " + e);
|
|
2077
|
-
}
|
|
2078
|
-
}
|
|
2448
|
+
// sendBeacon 返回 false,数据已在 LocalStorage 中,等待下次恢复
|
|
2449
|
+
if (_this.initConfig.showLog) {
|
|
2450
|
+
_this.printLog("sendBeacon \u8FD4\u56DE false\uFF0C\u6570\u636E\u5DF2\u4FDD\u5B58\u5230 LocalStorage \u7B49\u5F85\u4E0B\u6B21\u6062\u590D");
|
|
2079
2451
|
}
|
|
2080
2452
|
}
|
|
2081
2453
|
} catch (e) {
|
|
2082
|
-
// sendBeacon
|
|
2083
|
-
if (_this.initConfig.batchSend && _this.batchQueue.length > 0) {
|
|
2084
|
-
_this.saveBatchQueueToStorage();
|
|
2085
|
-
} // 保存 pendingRequests 到 LocalStorage(如果支持)
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
if (_this.pendingRequests.length > 0) {
|
|
2089
|
-
try {
|
|
2090
|
-
_this.setLocalStorage(_this.PENDING_REQUESTS_STORAGE_KEY, JSON.stringify(_this.pendingRequests));
|
|
2091
|
-
} catch (e) {// LocalStorage 可能已满或不可用
|
|
2092
|
-
}
|
|
2093
|
-
}
|
|
2094
|
-
|
|
2454
|
+
// sendBeacon 失败,数据已在 LocalStorage 中,等待下次恢复
|
|
2095
2455
|
if (_this.initConfig.showLog) {
|
|
2096
|
-
_this.printLog("\u9875\u9762\u5378\u8F7D\u65F6\u53D1\u9001\u6570\u636E\u5931\u8D25: " + e);
|
|
2456
|
+
_this.printLog("\u9875\u9762\u5378\u8F7D\u65F6\u53D1\u9001\u6570\u636E\u5931\u8D25: " + e + "\uFF0C\u6570\u636E\u5DF2\u4FDD\u5B58\u5230 LocalStorage");
|
|
2097
2457
|
}
|
|
2458
|
+
} finally {
|
|
2459
|
+
// 重置标记
|
|
2460
|
+
_this.isFlushingPendingData = false;
|
|
2098
2461
|
}
|
|
2099
2462
|
} else {
|
|
2100
|
-
// 不支持 sendBeacon
|
|
2101
|
-
if (_this.initConfig.
|
|
2102
|
-
_this.
|
|
2103
|
-
} //
|
|
2463
|
+
// 不支持 sendBeacon,数据已在 LocalStorage 中,等待下次恢复
|
|
2464
|
+
if (_this.initConfig.showLog) {
|
|
2465
|
+
_this.printLog("\u4E0D\u652F\u6301 sendBeacon\uFF0C\u6570\u636E\u5DF2\u4FDD\u5B58\u5230 LocalStorage \u7B49\u5F85\u4E0B\u6B21\u6062\u590D");
|
|
2466
|
+
} // 重置标记
|
|
2104
2467
|
|
|
2105
2468
|
|
|
2106
|
-
|
|
2107
|
-
try {
|
|
2108
|
-
_this.setLocalStorage(_this.PENDING_REQUESTS_STORAGE_KEY, JSON.stringify(_this.pendingRequests));
|
|
2109
|
-
} catch (e) {// LocalStorage 可能已满或不可用
|
|
2110
|
-
}
|
|
2111
|
-
}
|
|
2469
|
+
_this.isFlushingPendingData = false;
|
|
2112
2470
|
}
|
|
2113
2471
|
};
|
|
2114
2472
|
/**
|
|
@@ -2131,7 +2489,8 @@ function (_super) {
|
|
|
2131
2489
|
contentType = _a.contentType,
|
|
2132
2490
|
showLog = _a.showLog,
|
|
2133
2491
|
initHeader = _a.header,
|
|
2134
|
-
batchSend = _a.batchSend
|
|
2492
|
+
batchSend = _a.batchSend,
|
|
2493
|
+
sendMethod = _a.sendMethod;
|
|
2135
2494
|
if (!!showLog) _this.printLog(params); // 如果启用批量发送
|
|
2136
2495
|
|
|
2137
2496
|
if (batchSend) {
|
|
@@ -2141,10 +2500,13 @@ function (_super) {
|
|
|
2141
2500
|
success: true,
|
|
2142
2501
|
message: "已添加到批量队列"
|
|
2143
2502
|
});
|
|
2144
|
-
} //
|
|
2503
|
+
} // 判断是否使用 sendBeacon
|
|
2504
|
+
|
|
2505
|
+
|
|
2506
|
+
var shouldUseBeacon = _this.shouldUseBeacon(sendMethod, header, initHeader); // 如果使用 sendBeacon
|
|
2145
2507
|
|
|
2146
2508
|
|
|
2147
|
-
if (
|
|
2509
|
+
if (shouldUseBeacon) {
|
|
2148
2510
|
// 检查页面是否即将卸载,如果是,直接使用 sendBeacon 发送,避免被取消
|
|
2149
2511
|
if (_this.isPageUnloading()) {
|
|
2150
2512
|
var sent = _this.sendWithBeacon(params, serverUrl, contentType);
|
|
@@ -2182,8 +2544,8 @@ function (_super) {
|
|
|
2182
2544
|
} else {
|
|
2183
2545
|
// 使用 XMLHttpRequest 发送
|
|
2184
2546
|
return new Promise(function (resolve, reject) {
|
|
2185
|
-
//
|
|
2186
|
-
if (_this.isPageUnloading() && _this.isSupportBeaconSend() && !header && !initHeader) {
|
|
2547
|
+
// 如果页面即将卸载且配置为 auto,尝试使用 sendBeacon 作为备用
|
|
2548
|
+
if (_this.isPageUnloading() && sendMethod === 'auto' && _this.isSupportBeaconSend() && !header && !initHeader) {
|
|
2187
2549
|
var sent = _this.sendWithBeacon(params, serverUrl, contentType);
|
|
2188
2550
|
|
|
2189
2551
|
if (sent) {
|
|
@@ -2212,8 +2574,8 @@ function (_super) {
|
|
|
2212
2574
|
});
|
|
2213
2575
|
},
|
|
2214
2576
|
error: function error(err, status) {
|
|
2215
|
-
//
|
|
2216
|
-
if (_this.isPageUnloading() && _this.isSupportBeaconSend() && !header && !initHeader) {
|
|
2577
|
+
// 如果请求失败且页面即将卸载且配置为 auto,尝试使用 sendBeacon
|
|
2578
|
+
if (_this.isPageUnloading() && sendMethod === 'auto' && _this.isSupportBeaconSend() && !header && !initHeader) {
|
|
2217
2579
|
var sent = _this.sendWithBeacon(params, serverUrl, contentType);
|
|
2218
2580
|
|
|
2219
2581
|
if (sent) {
|
|
@@ -2235,6 +2597,30 @@ function (_super) {
|
|
|
2235
2597
|
});
|
|
2236
2598
|
}
|
|
2237
2599
|
};
|
|
2600
|
+
/**
|
|
2601
|
+
* @description 判断是否应该使用 sendBeacon
|
|
2602
|
+
* @param sendMethod 配置的发送方式
|
|
2603
|
+
* @param header 自定义 header
|
|
2604
|
+
* @param initHeader 初始化配置的 header
|
|
2605
|
+
* @returns 是否使用 sendBeacon
|
|
2606
|
+
*/
|
|
2607
|
+
|
|
2608
|
+
|
|
2609
|
+
_this.shouldUseBeacon = function (sendMethod, header, initHeader) {
|
|
2610
|
+
// 如果配置为 xhr,不使用 beacon
|
|
2611
|
+
if (sendMethod === 'xhr') {
|
|
2612
|
+
return false;
|
|
2613
|
+
} // 如果配置为 beacon,检查是否支持
|
|
2614
|
+
|
|
2615
|
+
|
|
2616
|
+
if (sendMethod === 'beacon') {
|
|
2617
|
+
return _this.isSupportBeaconSend() === true;
|
|
2618
|
+
} // 如果配置为 auto(默认),使用原有逻辑
|
|
2619
|
+
// 只有在支持 sendBeacon 且没有自定义 header 时才使用
|
|
2620
|
+
|
|
2621
|
+
|
|
2622
|
+
return _this.isSupportBeaconSend() === true && !header && !initHeader;
|
|
2623
|
+
};
|
|
2238
2624
|
/**
|
|
2239
2625
|
* @description 留存时长上报
|
|
2240
2626
|
* @param type
|
|
@@ -2263,6 +2649,121 @@ function (_super) {
|
|
|
2263
2649
|
_this.setCookie("retainedStartTime", _this.getTimeStamp());
|
|
2264
2650
|
}
|
|
2265
2651
|
};
|
|
2652
|
+
/**
|
|
2653
|
+
* @description 用户主动上报页面停留时长
|
|
2654
|
+
* @param duration 自定义停留时长(毫秒),如果不传则自动计算从页面加载(或上次调用)到当前的时长
|
|
2655
|
+
* @param options 可选参数,包括自定义描述、业务参数等
|
|
2656
|
+
* @param resetStartTime 是否重置起始时间,默认 true(手动上报后重置,定时上报不重置)
|
|
2657
|
+
* @returns Promise<TrackingResponse> 上报结果
|
|
2658
|
+
*/
|
|
2659
|
+
|
|
2660
|
+
|
|
2661
|
+
_this.trackPageDuration = function (duration, options, resetStartTime) {
|
|
2662
|
+
if (resetStartTime === void 0) {
|
|
2663
|
+
resetStartTime = true;
|
|
2664
|
+
} // 计算停留时长
|
|
2665
|
+
|
|
2666
|
+
|
|
2667
|
+
var retainedDuration;
|
|
2668
|
+
|
|
2669
|
+
if (duration !== undefined && duration !== null) {
|
|
2670
|
+
// 使用自定义时长
|
|
2671
|
+
retainedDuration = Math.max(duration, 0);
|
|
2672
|
+
} else {
|
|
2673
|
+
// 自动计算时长
|
|
2674
|
+
var __time = _this.getCookie("retainedStartTime");
|
|
2675
|
+
|
|
2676
|
+
var retainedStartTime = __time ? +__time : _this.getTimeStamp();
|
|
2677
|
+
|
|
2678
|
+
var currentTime = _this.getTimeStamp();
|
|
2679
|
+
|
|
2680
|
+
retainedDuration = Math.max(currentTime - retainedStartTime, 0);
|
|
2681
|
+
} // 构建参数
|
|
2682
|
+
|
|
2683
|
+
|
|
2684
|
+
var desc = (options === null || options === void 0 ? void 0 : options.desc) || _this.eventDescMap["PageRetained"];
|
|
2685
|
+
var pageKey = (options === null || options === void 0 ? void 0 : options.pageKey) || _this.pageKey;
|
|
2686
|
+
var business = (options === null || options === void 0 ? void 0 : options.business) || {};
|
|
2687
|
+
var header = options === null || options === void 0 ? void 0 : options.header;
|
|
2688
|
+
|
|
2689
|
+
var params = _this.getParams({
|
|
2690
|
+
event: "PageRetained",
|
|
2691
|
+
desc: desc,
|
|
2692
|
+
itemKey: _this.getItemKey(undefined, pageKey),
|
|
2693
|
+
privateParamMap: {
|
|
2694
|
+
business: business,
|
|
2695
|
+
retainedDuration: retainedDuration
|
|
2696
|
+
}
|
|
2697
|
+
}); // 上报数据
|
|
2698
|
+
|
|
2699
|
+
|
|
2700
|
+
var result = _this.sendData(params, header); // 根据 resetStartTime 参数决定是否重置起始时间
|
|
2701
|
+
// 手动上报后重置起始时间,定时上报不重置(累积计算)
|
|
2702
|
+
|
|
2703
|
+
|
|
2704
|
+
if (resetStartTime) {
|
|
2705
|
+
_this.setCookie("retainedStartTime", _this.getTimeStamp());
|
|
2706
|
+
}
|
|
2707
|
+
|
|
2708
|
+
return result;
|
|
2709
|
+
};
|
|
2710
|
+
/**
|
|
2711
|
+
* @description 启动定时上报页面停留时长的定时器
|
|
2712
|
+
*/
|
|
2713
|
+
|
|
2714
|
+
|
|
2715
|
+
_this.startPageDurationTimer = function () {
|
|
2716
|
+
// 先停止现有的定时器(避免重复启动)
|
|
2717
|
+
_this.stopPageDurationTimer();
|
|
2718
|
+
|
|
2719
|
+
var interval = _this.initConfig.pageDurationInterval || 30000; // 检查间隔时间是否有效
|
|
2720
|
+
|
|
2721
|
+
if (interval <= 0) {
|
|
2722
|
+
if (_this.initConfig.showLog) {
|
|
2723
|
+
_this.printLog("定时上报间隔时间无效,已禁用定时上报");
|
|
2724
|
+
}
|
|
2725
|
+
|
|
2726
|
+
return;
|
|
2727
|
+
} // 启动定时器
|
|
2728
|
+
|
|
2729
|
+
|
|
2730
|
+
_this.pageDurationTimer = window.setInterval(function () {
|
|
2731
|
+
// 只在页面可见时上报(避免后台上报)
|
|
2732
|
+
if (document.visibilityState === "visible") {
|
|
2733
|
+
// 定时上报:retainedDuration 直接使用上报间隔时间,数据工程师会清洗数据
|
|
2734
|
+
_this.trackPageDuration(interval, {
|
|
2735
|
+
desc: "定时上报页面停留时长",
|
|
2736
|
+
business: {
|
|
2737
|
+
reportType: "interval",
|
|
2738
|
+
interval: interval
|
|
2739
|
+
}
|
|
2740
|
+
}, true).catch(function (err) {
|
|
2741
|
+
if (_this.initConfig.showLog) {
|
|
2742
|
+
_this.printLog("\u5B9A\u65F6\u4E0A\u62A5\u9875\u9762\u505C\u7559\u65F6\u957F\u5931\u8D25: " + err);
|
|
2743
|
+
}
|
|
2744
|
+
});
|
|
2745
|
+
}
|
|
2746
|
+
}, interval);
|
|
2747
|
+
|
|
2748
|
+
if (_this.initConfig.showLog) {
|
|
2749
|
+
_this.printLog("\u5B9A\u65F6\u4E0A\u62A5\u9875\u9762\u505C\u7559\u65F6\u957F\u5DF2\u542F\u52A8\uFF0C\u95F4\u9694: " + interval + "ms");
|
|
2750
|
+
}
|
|
2751
|
+
};
|
|
2752
|
+
/**
|
|
2753
|
+
* @description 停止定时上报页面停留时长的定时器
|
|
2754
|
+
*/
|
|
2755
|
+
|
|
2756
|
+
|
|
2757
|
+
_this.stopPageDurationTimer = function () {
|
|
2758
|
+
if (_this.pageDurationTimer !== null) {
|
|
2759
|
+
clearInterval(_this.pageDurationTimer);
|
|
2760
|
+
_this.pageDurationTimer = null;
|
|
2761
|
+
|
|
2762
|
+
if (_this.initConfig.showLog) {
|
|
2763
|
+
_this.printLog("定时上报页面停留时长已停止");
|
|
2764
|
+
}
|
|
2765
|
+
}
|
|
2766
|
+
};
|
|
2266
2767
|
/**
|
|
2267
2768
|
* @description 获取 itemKey
|
|
2268
2769
|
* @param {[string]} partkey [控件/自定义事件的唯一标识]
|
|
@@ -2280,7 +2781,7 @@ function (_super) {
|
|
|
2280
2781
|
}, "");
|
|
2281
2782
|
};
|
|
2282
2783
|
|
|
2283
|
-
_this.sdkVersion = "1.2.
|
|
2784
|
+
_this.sdkVersion = "1.2.3"; // sdk版本
|
|
2284
2785
|
|
|
2285
2786
|
_this.initConfig = {
|
|
2286
2787
|
appKey: "",
|
|
@@ -2298,7 +2799,10 @@ function (_super) {
|
|
|
2298
2799
|
batchInterval: 5000,
|
|
2299
2800
|
batchMaxSize: 10,
|
|
2300
2801
|
trackPartKeyClick: false,
|
|
2301
|
-
pendingRequestsMaxSize: 50
|
|
2802
|
+
pendingRequestsMaxSize: 50,
|
|
2803
|
+
autoTrackPageDurationInterval: false,
|
|
2804
|
+
pageDurationInterval: 30000,
|
|
2805
|
+
sendMethod: "auto" // 数据发送方式:auto(自动选择)、xhr(XMLHttpRequest)、beacon(sendBeacon)
|
|
2302
2806
|
|
|
2303
2807
|
}; // 系统信息
|
|
2304
2808
|
|