Skip to content

Commit e15a69b

Browse files
1 parent 4cc1faa commit e15a69b

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

scripts/aem.js

+49-13
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
* for instance the href of a link, or a search term
2323
*/
2424
function sampleRUM(checkpoint, data = {}) {
25-
sampleRUM.baseURL = sampleRUM.baseURL || new URL(window.RUM_BASE == null ? 'https://rum.hlx.page' : window.RUM_BASE, window.location);
25+
sampleRUM.baseURL = sampleRUM.baseURL
26+
|| new URL(window.RUM_BASE == null ? 'https://rum.hlx.page' : window.RUM_BASE, window.location);
2627
sampleRUM.defer = sampleRUM.defer || [];
2728
const defer = (fnname) => {
28-
sampleRUM[fnname] = sampleRUM[fnname]
29-
|| ((...args) => sampleRUM.defer.push({ fnname, args }));
29+
sampleRUM[fnname] = sampleRUM[fnname] || ((...args) => sampleRUM.defer.push({ fnname, args }));
3030
};
3131
sampleRUM.drain = sampleRUM.drain
3232
|| ((dfnname, fn) => {
@@ -48,28 +48,57 @@ function sampleRUM(checkpoint, data = {}) {
4848
window.hlx = window.hlx || {};
4949
if (!window.hlx.rum) {
5050
const usp = new URLSearchParams(window.location.search);
51-
const weight = (usp.get('rum') === 'on') ? 1 : 100; // with parameter, weight is 1. Defaults to 100.
51+
const weight = usp.get('rum') === 'on' ? 1 : 100; // with parameter, weight is 1. Defaults to 100.
5252
const id = Math.random().toString(36).slice(-4);
5353
const random = Math.random();
54-
const isSelected = (random * weight < 1);
54+
const isSelected = random * weight < 1;
5555
const firstReadTime = window.performance ? window.performance.timeOrigin : Date.now();
5656
const urlSanitizers = {
5757
full: () => window.location.href,
5858
origin: () => window.location.origin,
5959
path: () => window.location.href.replace(/\?.*$/, ''),
6060
};
6161
// eslint-disable-next-line object-curly-newline, max-len
62-
window.hlx.rum = { weight, id, random, isSelected, firstReadTime, sampleRUM, sanitizeURL: urlSanitizers[window.hlx.RUM_MASK_URL || 'path'] };
62+
window.hlx.rum = {
63+
weight,
64+
id,
65+
random,
66+
isSelected,
67+
firstReadTime,
68+
sampleRUM,
69+
sanitizeURL: urlSanitizers[window.hlx.RUM_MASK_URL || 'path'],
70+
};
6371
}
6472

6573
const { weight, id, firstReadTime } = window.hlx.rum;
6674
if (window.hlx && window.hlx.rum && window.hlx.rum.isSelected) {
67-
const knownProperties = ['weight', 'id', 'referer', 'checkpoint', 't', 'source', 'target', 'cwv', 'CLS', 'FID', 'LCP', 'INP', 'TTFB'];
75+
const knownProperties = [
76+
'weight',
77+
'id',
78+
'referer',
79+
'checkpoint',
80+
't',
81+
'source',
82+
'target',
83+
'cwv',
84+
'CLS',
85+
'FID',
86+
'LCP',
87+
'INP',
88+
'TTFB',
89+
];
6890
const sendPing = (pdata = data) => {
6991
// eslint-disable-next-line max-len
70-
const t = Math.round(window.performance ? window.performance.now() : (Date.now() - firstReadTime));
92+
const t = Math.round(
93+
window.performance ? window.performance.now() : Date.now() - firstReadTime,
94+
);
7195
// eslint-disable-next-line object-curly-newline, max-len, no-use-before-define
72-
const body = JSON.stringify({ weight, id, referer: window.hlx.rum.sanitizeURL(), checkpoint, t, ...data }, knownProperties);
96+
const body = JSON.stringify(
97+
{
98+
weight, id, referer: window.hlx.rum.sanitizeURL(), checkpoint, t, ...data,
99+
},
100+
knownProperties,
101+
);
73102
const url = new URL(`.rum/${weight}`, sampleRUM.baseURL).href;
74103
navigator.sendBeacon(url, body);
75104
// eslint-disable-next-line no-console
@@ -80,7 +109,10 @@ function sampleRUM(checkpoint, data = {}) {
80109
lazy: () => {
81110
// use classic script to avoid CORS issues
82111
const script = document.createElement('script');
83-
script.src = new URL('.rum/@adobe/helix-rum-enhancer@^1/src/index.js', sampleRUM.baseURL).href;
112+
script.src = new URL(
113+
'.rum/@adobe/helix-rum-enhancer@^1/src/index.js',
114+
sampleRUM.baseURL,
115+
).href;
84116
document.head.appendChild(script);
85117
return true;
86118
},
@@ -133,11 +165,15 @@ function init() {
133165
const errData = { source: 'undefined error' };
134166
try {
135167
errData.target = (reason || error).toString();
136-
errData.source = (reason || error).stack.split('\n')
137-
.filter((line) => line.match(/https?:\/\//)).shift()
168+
errData.source = (reason || error).stack
169+
.split('\n')
170+
.filter((line) => line.match(/https?:\/\//))
171+
.shift()
138172
.replace(/at ([^ ]+) \((.+)\)/, '$1@$2')
139173
.trim();
140-
} catch (err) { /* error structure was not as expected */ }
174+
} catch (err) {
175+
/* error structure was not as expected */
176+
}
141177
sampleRUM('error', errData);
142178
});
143179
});

0 commit comments

Comments
 (0)