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

feat(minirum): different base URL for collection and extra scripts #144

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@ export function sampleRUM(checkpoint, data) {
const rumStorage = sessionStorage.getItem(SESSION_STORAGE_KEY) ? JSON.parse(sessionStorage.getItem(SESSION_STORAGE_KEY)) : {};
rumStorage.pages = (rumStorage.pages ?? 0) + (Math.floor(Math.random() * 20) - 10) + 1;
sessionStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(rumStorage));
sampleRUM.baseURL = sampleRUM.baseURL || new URL(window.RUM_BASE == null ? 'https://rum.hlx.page' : window.RUM_BASE, window.location);
const weight = new URLSearchParams(window.location.search).get('rum') === 'on' ? 1 : 100;
const id = Array.from({ length: 75 }, (_, i) => String.fromCharCode(48 + i)).filter((a) => /\d|[A-Z]/i.test(a)).filter(() => Math.random() * 75 > 70).join('');
const isSelected = (Math.random() * weight < 1);
// eslint-disable-next-line object-curly-newline, max-len
window.hlx.rum = { weight, id, isSelected, firstReadTime: window.performance ? window.performance.timeOrigin : Date.now(), sampleRUM, queue: [], collector: (...args) => window.hlx.rum.queue.push(args) };
if (isSelected) {
sampleRUM.collectorBaseURL = sampleRUM.collectorBaseURL || new URL(window.RUM_BASE || '/', window.origin);
sampleRUM.baseURL = sampleRUM.baseURL || new URL(window.RUM_BASE || '/', new URL('https://rum.hlx.page'));
const pathBase = window.hlx.codeBasePath ? `${window.hlx.codeBasePath}/` : '';
// eslint-disable-next-line object-curly-newline, max-len
const body = JSON.stringify({ weight, id, referer: window.location.href, checkpoint: 'top', t: timeShift(), target: document.visibilityState });
const url = new URL(`.rum/${weight}`, sampleRUM.baseURL).href;
const url = new URL(`${pathBase}.rum/${weight}`, sampleRUM.collectorBaseURL).href;
navigator.sendBeacon(url, body);
// eslint-disable-next-line max-statements-per-line, brace-style
window.addEventListener('load', () => { sampleRUM('load'); import(new URL('.rum/@adobe/helix-rum-enhancer@^2/src/index.js', sampleRUM.baseURL));
window.addEventListener('load', () => {
sampleRUM('load');
// use classic script to avoid CORS issues
const script = document.createElement('script');
script.src = new URL('.rum/@adobe/helix-rum-enhancer@^2/src/index.js', sampleRUM.baseURL).href;
document.head.appendChild(script);
});
}
}
Expand Down
Loading