Skip to content

Commit e5281b2

Browse files
committed
fix: manual patch of sampleRUM
1 parent 72b31eb commit e5281b2

File tree

1 file changed

+11
-58
lines changed

1 file changed

+11
-58
lines changed

scripts/aem.js

+11-58
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@
2222
* for instance the href of a link, or a search term
2323
*/
2424
function sampleRUM(checkpoint, data = {}) {
25-
const SESSION_STORAGE_KEY = 'aem-rum';
26-
sampleRUM.baseURL = sampleRUM.baseURL
27-
|| new URL(window.RUM_BASE == null ? 'https://rum.hlx.page' : window.RUM_BASE, window.location);
25+
sampleRUM.baseURL = sampleRUM.baseURL || new URL(window.RUM_BASE == null ? 'https://rum.hlx.page' : window.RUM_BASE, window.location);
2826
sampleRUM.defer = sampleRUM.defer || [];
2927
const defer = (fnname) => {
30-
sampleRUM[fnname] = sampleRUM[fnname] || ((...args) => sampleRUM.defer.push({ fnname, args }));
28+
sampleRUM[fnname] = sampleRUM[fnname]
29+
|| ((...args) => sampleRUM.defer.push({ fnname, args }));
3130
};
3231
sampleRUM.drain = sampleRUM.drain
3332
|| ((dfnname, fn) => {
@@ -49,85 +48,39 @@ function sampleRUM(checkpoint, data = {}) {
4948
window.hlx = window.hlx || {};
5049
if (!window.hlx.rum) {
5150
const usp = new URLSearchParams(window.location.search);
52-
const weight = usp.get('rum') === 'on' ? 1 : 100; // with parameter, weight is 1. Defaults to 100.
53-
const id = Array.from({ length: 75 }, (_, i) => String.fromCharCode(48 + i))
54-
.filter((a) => /\d|[A-Z]/i.test(a))
55-
.filter(() => Math.random() * 75 > 70)
56-
.join('');
51+
const weight = (usp.get('rum') === 'on') ? 1 : 100; // with parameter, weight is 1. Defaults to 100.
52+
const id = Math.random().toString(36).slice(-4);
5753
const random = Math.random();
58-
const isSelected = random * weight < 1;
54+
const isSelected = (random * weight < 1);
5955
const firstReadTime = window.performance ? window.performance.timeOrigin : Date.now();
6056
const urlSanitizers = {
6157
full: () => window.location.href,
6258
origin: () => window.location.origin,
6359
path: () => window.location.href.replace(/\?.*$/, ''),
6460
};
65-
// eslint-disable-next-line max-len
66-
const rumSessionStorage = sessionStorage.getItem(SESSION_STORAGE_KEY)
67-
? JSON.parse(sessionStorage.getItem(SESSION_STORAGE_KEY))
68-
: {};
69-
// eslint-disable-next-line max-len
70-
rumSessionStorage.pages = (rumSessionStorage.pages ? rumSessionStorage.pages : 0)
71-
+ 1
72-
/* noise */ + (Math.floor(Math.random() * 20) - 10);
73-
sessionStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(rumSessionStorage));
7461
// eslint-disable-next-line object-curly-newline, max-len
75-
window.hlx.rum = {
76-
weight,
77-
id,
78-
random,
79-
isSelected,
80-
firstReadTime,
81-
sampleRUM,
82-
sanitizeURL: urlSanitizers[window.hlx.RUM_MASK_URL || 'path'],
83-
rumSessionStorage,
84-
};
62+
window.hlx.rum = { weight, id, random, isSelected, firstReadTime, sampleRUM, sanitizeURL: urlSanitizers[window.hlx.RUM_MASK_URL || 'path'] };
8563
}
8664

8765
const { weight, id, firstReadTime } = window.hlx.rum;
8866
if (window.hlx && window.hlx.rum && window.hlx.rum.isSelected) {
89-
const knownProperties = [
90-
'weight',
91-
'id',
92-
'referer',
93-
'checkpoint',
94-
't',
95-
'source',
96-
'target',
97-
'cwv',
98-
'CLS',
99-
'FID',
100-
'LCP',
101-
'INP',
102-
'TTFB',
103-
];
67+
const knownProperties = ['weight', 'id', 'referer', 'checkpoint', 't', 'source', 'target', 'cwv', 'CLS', 'FID', 'LCP', 'INP', 'TTFB'];
10468
const sendPing = (pdata = data) => {
10569
// eslint-disable-next-line max-len
106-
const t = Math.round(
107-
window.performance ? window.performance.now() : Date.now() - firstReadTime,
108-
);
70+
const t = Math.round(window.performance ? window.performance.now() : (Date.now() - firstReadTime));
10971
// eslint-disable-next-line object-curly-newline, max-len, no-use-before-define
110-
const body = JSON.stringify(
111-
{
112-
weight, id, referer: window.hlx.rum.sanitizeURL(), checkpoint, t, ...data,
113-
},
114-
knownProperties,
115-
);
72+
const body = JSON.stringify({ weight, id, referer: window.hlx.rum.sanitizeURL(), checkpoint, t, ...data }, knownProperties);
11673
const url = new URL(`.rum/${weight}`, sampleRUM.baseURL).href;
11774
navigator.sendBeacon(url, body);
11875
// eslint-disable-next-line no-console
11976
console.debug(`ping:${checkpoint}`, pdata);
12077
};
12178
sampleRUM.cases = sampleRUM.cases || {
122-
load: () => sampleRUM('pagesviewed', { source: window.hlx.rum.rumSessionStorage.pages }) || true,
12379
cwv: () => sampleRUM.cwv(data) || true,
12480
lazy: () => {
12581
// use classic script to avoid CORS issues
12682
const script = document.createElement('script');
127-
script.src = new URL(
128-
'.rum/@adobe/helix-rum-enhancer@^1/src/index.js',
129-
sampleRUM.baseURL,
130-
).href;
83+
script.src = new URL('.rum/@adobe/helix-rum-enhancer@^1/src/index.js', sampleRUM.baseURL).href;
13184
document.head.appendChild(script);
13285
return true;
13386
},

0 commit comments

Comments
 (0)