Progress check-in. Updated tracker and recorder scripts.
This commit is contained in:
1 parent
e9ac09ec5d
commit
8c1ee0b3ce
25 files changed
+350
-74
No files matched your search
+61
-12
@@ -24,18 +24,19 @@
|
||||
const _false = 'false';
|
||||
const _true = 'true';
|
||||
const attr = currentScript.getAttribute.bind(currentScript);
|
||||
const config = value => attr(`${_data}${value}`);
|
||||
|
||||
const website = attr(`${_data}website-id`);
|
||||
const hostUrl = attr(`${_data}host-url`);
|
||||
const beforeSend = attr(`${_data}before-send`);
|
||||
const tag = attr(`${_data}tag`) || undefined;
|
||||
const autoTrack = attr(`${_data}auto-track`) !== _false;
|
||||
const dnt = attr(`${_data}do-not-track`) === _true;
|
||||
const excludeSearch = attr(`${_data}exclude-search`) === _true;
|
||||
const excludeHash = attr(`${_data}exclude-hash`) === _true;
|
||||
const domain = attr(`${_data}domains`) || '';
|
||||
const credentials = attr(`${_data}fetch-credentials`) || 'omit';
|
||||
const perf = attr(`${_data}perf`) === _true;
|
||||
const website = config('website-id');
|
||||
const hostUrl = config('host-url');
|
||||
const beforeSend = config('before-send');
|
||||
const tag = config('tag') || undefined;
|
||||
const autoTrack = config('auto-track') !== _false;
|
||||
const dnt = config('do-not-track') === _true;
|
||||
const excludeSearch = config('exclude-search') === _true;
|
||||
const excludeHash = config('exclude-hash') === _true;
|
||||
const domain = config('domains') || '';
|
||||
const credentials = config('fetch-credentials') || 'omit';
|
||||
const perf = config('performance') === _true;
|
||||
|
||||
const domains = domain.split(',').map(n => n.trim());
|
||||
const host =
|
||||
@@ -82,6 +83,10 @@
|
||||
const handlePush = (_state, _title, url) => {
|
||||
if (!url) return;
|
||||
|
||||
if (typeof flushPerformance === 'function') {
|
||||
flushPerformance();
|
||||
}
|
||||
|
||||
currentRef = currentUrl;
|
||||
currentUrl = normalize(new URL(url, location.href).toString());
|
||||
|
||||
@@ -226,6 +231,7 @@
|
||||
const initPerformance = () => {
|
||||
const metrics = {};
|
||||
let sent = false;
|
||||
let timeoutId;
|
||||
|
||||
const observe = (type, callback) => {
|
||||
try {
|
||||
@@ -280,12 +286,54 @@
|
||||
/* not supported */
|
||||
}
|
||||
|
||||
const getEntriesByType = type => {
|
||||
try {
|
||||
return window.performance?.getEntriesByType?.(type) || [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const applyFallbackMetrics = () => {
|
||||
if (metrics.ttfb === undefined) {
|
||||
const navigation = getEntriesByType('navigation')?.[0];
|
||||
if (navigation) {
|
||||
metrics.ttfb = Math.max(navigation.responseStart - navigation.requestStart, 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (metrics.fcp === undefined) {
|
||||
const fcpEntry = getEntriesByType('paint')?.find(
|
||||
entry => entry.name === 'first-contentful-paint',
|
||||
);
|
||||
if (fcpEntry) {
|
||||
metrics.fcp = fcpEntry.startTime;
|
||||
}
|
||||
}
|
||||
|
||||
if (metrics.lcp === undefined) {
|
||||
const lcpEntries = getEntriesByType('largest-contentful-paint');
|
||||
const lcpEntry = lcpEntries?.[lcpEntries.length - 1];
|
||||
if (lcpEntry) {
|
||||
metrics.lcp = lcpEntry.startTime;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const sendPerformance = () => {
|
||||
if (sent || !Object.keys(metrics).length) return;
|
||||
if (sent) return;
|
||||
|
||||
applyFallbackMetrics();
|
||||
if (!Object.keys(metrics).length) return;
|
||||
|
||||
sent = true;
|
||||
if (timeoutId) clearTimeout(timeoutId);
|
||||
send({ ...getPayload(), ...metrics }, 'performance');
|
||||
};
|
||||
|
||||
flushPerformance = sendPerformance;
|
||||
timeoutId = setTimeout(sendPerformance, 10000);
|
||||
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (document.visibilityState === 'hidden') sendPerformance();
|
||||
});
|
||||
@@ -309,6 +357,7 @@
|
||||
let disabled = false;
|
||||
let cache;
|
||||
let identity;
|
||||
let flushPerformance;
|
||||
|
||||
if (autoTrack && !trackingDisabled()) {
|
||||
if (document.readyState === 'complete') {
|
||||
|
||||
Reference in new issue
Block a user