Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lib): update scripts/aem.js to [email protected] #420

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions scripts/aem.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ function sampleRUM(checkpoint, data) {
window.hlx = window.hlx || {};
sampleRUM.enhance = () => {};
if (!window.hlx.rum) {
const param = new URLSearchParams(window.location.search).get('rum');
const weight = (window.SAMPLE_PAGEVIEWS_AT_RATE === 'high' && 10)
|| (window.SAMPLE_PAGEVIEWS_AT_RATE === 'low' && 1000)
|| (new URLSearchParams(window.location.search).get('rum') === 'on' && 1)
|| (param === 'on' && 1)
|| 100;
const id = Math.random().toString(36).slice(-4);
const isSelected = Math.random() * weight < 1;
const isSelected = param !== 'off' && Math.random() * weight < 1;
// eslint-disable-next-line object-curly-newline, max-len
window.hlx.rum = {
weight,
Expand Down Expand Up @@ -80,7 +81,13 @@ function sampleRUM(checkpoint, data) {
t: time,
...pingData,
});
const { href: url, origin } = new URL(`.rum/${weight}`, sampleRUM.collectBaseURL);
const urlParams = window.RUM_PARAMS
? `?${new URLSearchParams(window.RUM_PARAMS).toString()}`
: '';
const { href: url, origin } = new URL(
`.rum/${weight}${urlParams}`,
sampleRUM.collectBaseURL,
);
const body = origin === window.location.origin
? new Blob([rumData], { type: 'application/json' })
: rumData;
Expand All @@ -91,6 +98,9 @@ function sampleRUM(checkpoint, data) {
sampleRUM.sendPing('top', timeShift());

sampleRUM.enhance = () => {
// only enhance once
if (document.querySelector('script[src*="rum-enhancer"]')) return;

const script = document.createElement('script');
script.src = new URL(
'.rum/@adobe/helix-rum-enhancer@^2/src/index.js',
Expand All @@ -108,7 +118,7 @@ function sampleRUM(checkpoint, data) {
}
document.dispatchEvent(new CustomEvent('rum', { detail: { checkpoint, data } }));
} catch (error) {
// something went wrong
// something went awry
}
}

Expand Down Expand Up @@ -684,6 +694,9 @@ async function loadSections(element) {
for (let i = 0; i < sections.length; i += 1) {
// eslint-disable-next-line no-await-in-loop
await loadSection(sections[i]);
if (i === 0 && sampleRUM.enhance) {
sampleRUM.enhance();
}
}
}

Expand Down