Skip to content

Commit a5fef7f

Browse files
authored
Merge pull request #325 from adobe/update-lib-aem-patch-1-5-1
fix(lib): update scripts/aem.js to [email protected]
2 parents 8f80c98 + 154e0a0 commit a5fef7f

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

scripts/aem.js

+28-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 Adobe. All rights reserved.
2+
* Copyright 2024 Adobe. All rights reserved.
33
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License. You may obtain a copy
55
* of the License at http://www.apache.org/licenses/LICENSE-2.0
@@ -22,6 +22,9 @@
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);
2528
sampleRUM.defer = sampleRUM.defer || [];
2629
const defer = (fnname) => {
2730
sampleRUM[fnname] = sampleRUM[fnname] || ((...args) => sampleRUM.defer.push({ fnname, args }));
@@ -53,12 +56,21 @@ function sampleRUM(checkpoint, data = {}) {
5356
.join('');
5457
const random = Math.random();
5558
const isSelected = random * weight < 1;
56-
const firstReadTime = Date.now();
59+
const firstReadTime = window.performance ? window.performance.timeOrigin : Date.now();
5760
const urlSanitizers = {
5861
full: () => window.location.href,
5962
origin: () => window.location.origin,
6063
path: () => window.location.href.replace(/\?.*$/, ''),
6164
};
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));
6274
// eslint-disable-next-line object-curly-newline, max-len
6375
window.hlx.rum = {
6476
weight,
@@ -68,8 +80,10 @@ function sampleRUM(checkpoint, data = {}) {
6880
firstReadTime,
6981
sampleRUM,
7082
sanitizeURL: urlSanitizers[window.hlx.RUM_MASK_URL || 'path'],
83+
rumSessionStorage,
7184
};
7285
}
86+
7387
const { weight, id, firstReadTime } = window.hlx.rum;
7488
if (window.hlx && window.hlx.rum && window.hlx.rum.isSelected) {
7589
const knownProperties = [
@@ -85,32 +99,35 @@ function sampleRUM(checkpoint, data = {}) {
8599
'FID',
86100
'LCP',
87101
'INP',
102+
'TTFB',
88103
];
89104
const sendPing = (pdata = data) => {
105+
// eslint-disable-next-line max-len
106+
const t = Math.round(
107+
window.performance ? window.performance.now() : Date.now() - firstReadTime,
108+
);
90109
// eslint-disable-next-line object-curly-newline, max-len, no-use-before-define
91110
const body = JSON.stringify(
92111
{
93-
weight,
94-
id,
95-
referer: window.hlx.rum.sanitizeURL(),
96-
checkpoint,
97-
t: Date.now() - firstReadTime,
98-
...data,
112+
weight, id, referer: window.hlx.rum.sanitizeURL(), checkpoint, t, ...data,
99113
},
100114
knownProperties,
101115
);
102-
const url = `https://rum.hlx.page/.rum/${weight}`;
103-
// eslint-disable-next-line no-unused-expressions
116+
const url = new URL(`.rum/${weight}`, sampleRUM.baseURL).href;
104117
navigator.sendBeacon(url, body);
105118
// eslint-disable-next-line no-console
106119
console.debug(`ping:${checkpoint}`, pdata);
107120
};
108121
sampleRUM.cases = sampleRUM.cases || {
122+
load: () => sampleRUM('pagesviewed', { source: window.hlx.rum.rumSessionStorage.pages }) || true,
109123
cwv: () => sampleRUM.cwv(data) || true,
110124
lazy: () => {
111125
// use classic script to avoid CORS issues
112126
const script = document.createElement('script');
113-
script.src = 'https://rum.hlx.page/.rum/@adobe/helix-rum-enhancer@^1/src/index.js';
127+
script.src = new URL(
128+
'.rum/@adobe/helix-rum-enhancer@^1/src/index.js',
129+
sampleRUM.baseURL,
130+
).href;
114131
document.head.appendChild(script);
115132
return true;
116133
},

0 commit comments

Comments
 (0)