Skip to content

Commit

Permalink
feat: use rum js v2
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe committed Jun 19, 2024
1 parent 232d7d7 commit 6d99a2c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 94 deletions.
2 changes: 1 addition & 1 deletion 404.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
btnContainer.append(backBtn);
}
}
sampleRUM('404', { source: document.referrer });
sampleRUM('404', { source: document.referrer, target: window.location.href });
});
</script>
<link rel="stylesheet" href="/styles/styles.css">
Expand Down
142 changes: 61 additions & 81 deletions scripts/aem.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,88 +11,75 @@
*/

/* eslint-env browser */

/**
* log RUM if part of the sample.
* @param {string} checkpoint identifies the checkpoint in funnel
* @param {Object} data additional data for RUM sample
* @param {string} data.source DOM node that is the source of a checkpoint event,
* identified by #id or .classname
* @param {string} data.target subject of the checkpoint event,
* for instance the href of a link, or a search term
*/
function sampleRUM(checkpoint, data = {}) {
sampleRUM.baseURL = sampleRUM.baseURL || new URL(window.RUM_BASE == null ? 'https://rum.hlx.page' : window.RUM_BASE, window.location);
sampleRUM.defer = sampleRUM.defer || [];
const defer = (fnname) => {
sampleRUM[fnname] = sampleRUM[fnname]
|| ((...args) => sampleRUM.defer.push({ fnname, args }));
};
sampleRUM.drain = sampleRUM.drain
|| ((dfnname, fn) => {
sampleRUM[dfnname] = fn;
sampleRUM.defer
.filter(({ fnname }) => dfnname === fnname)
.forEach(({ fnname, args }) => sampleRUM[fnname](...args));
});
sampleRUM.always = sampleRUM.always || [];
sampleRUM.always.on = (chkpnt, fn) => {
sampleRUM.always[chkpnt] = fn;
};
sampleRUM.on = (chkpnt, fn) => {
sampleRUM.cases[chkpnt] = fn;
};
defer('observe');
defer('cwv');
function sampleRUM(checkpoint, data) {
// eslint-disable-next-line max-len
const timeShift = () => (window.performance ? window.performance.now() : Date.now() - window.hlx.rum.firstReadTime);
try {
window.hlx = window.hlx || {};
sampleRUM.enhance = () => {};
if (!window.hlx.rum) {
const usp = new URLSearchParams(window.location.search);
const weight = (usp.get('rum') === 'on') ? 1 : 100; // with parameter, weight is 1. Defaults to 100.
const weight = new URLSearchParams(window.location.search).get('rum') === 'on' ? 1 : 100;
const id = Math.random().toString(36).slice(-4);
const random = Math.random();
const isSelected = (random * weight < 1);
const firstReadTime = window.performance ? window.performance.timeOrigin : Date.now();
const urlSanitizers = {
full: () => window.location.href,
origin: () => window.location.origin,
path: () => window.location.href.replace(/\?.*$/, ''),
};
const isSelected = Math.random() * weight < 1;
// eslint-disable-next-line object-curly-newline, max-len
window.hlx.rum = { weight, id, random, isSelected, firstReadTime, sampleRUM, sanitizeURL: urlSanitizers[window.hlx.RUM_MASK_URL || 'path'] };
}

const { weight, id, firstReadTime } = window.hlx.rum;
if (window.hlx && window.hlx.rum && window.hlx.rum.isSelected) {
const knownProperties = ['weight', 'id', 'referer', 'checkpoint', 't', 'source', 'target', 'cwv', 'CLS', 'FID', 'LCP', 'INP', 'TTFB'];
const sendPing = (pdata = data) => {
// eslint-disable-next-line max-len
const t = Math.round(window.performance ? window.performance.now() : (Date.now() - firstReadTime));
// eslint-disable-next-line object-curly-newline, max-len, no-use-before-define
const body = JSON.stringify({ weight, id, referer: window.hlx.rum.sanitizeURL(), checkpoint, t, ...data }, knownProperties);
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) {
['error', 'unhandledrejection'].forEach((event) => {
window.addEventListener(event, ({ reason, error }) => {
const errData = { source: 'undefined error' };
try {
errData.target = (reason || error).toString();
errData.source = (reason || error).stack
.split('\n')
.filter((line) => line.match(/https?:\/\//))
.shift()
.replace(/at ([^ ]+) \((.+)\)/, '$1@$2')
.trim();
} catch (err) {
/* error structure was not as expected */
}
sampleRUM('error', errData);
});
});
sampleRUM.baseURL = sampleRUM.baseURL || new URL(window.RUM_BASE || '/', new URL('https://rum.hlx.page'));
sampleRUM.collectBaseURL = sampleRUM.collectBaseURL || sampleRUM.baseURL;
// 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;
navigator.sendBeacon(url, body);
// eslint-disable-next-line no-console
console.debug(`ping:${checkpoint}`, pdata);
};
sampleRUM.cases = sampleRUM.cases || {
cwv: () => sampleRUM.cwv(data) || true,
lazy: () => {
// use classic script to avoid CORS issues

sampleRUM.enhance = () => {
const script = document.createElement('script');
script.src = new URL('.rum/@adobe/helix-rum-enhancer@^1/src/index.js', sampleRUM.baseURL).href;
script.src = new URL(
'.rum/@adobe/helix-rum-enhancer@^2/src/index.js',
sampleRUM.baseURL,
).href;
document.head.appendChild(script);
return true;
},
};
sendPing(data);
if (sampleRUM.cases[checkpoint]) {
sampleRUM.cases[checkpoint]();
};
if (!window.hlx.RUM_MANUAL_ENHANCE) {
sampleRUM.enhance();
}
}
}
if (sampleRUM.always[checkpoint]) {
sampleRUM.always[checkpoint](data);
if (window.hlx.rum && window.hlx.rum.isSelected && checkpoint) {
window.hlx.rum.collector(checkpoint, data, timeShift());
}
document.dispatchEvent(new CustomEvent('rum', { detail: { checkpoint, data } }));
} catch (error) {
// something went wrong
}
Expand All @@ -104,6 +91,7 @@ function sampleRUM(checkpoint, data = {}) {
function setup() {
window.hlx = window.hlx || {};
window.hlx.RUM_MASK_URL = 'full';
window.hlx.RUM_MANUAL_ENHANCE = true;
window.hlx.codeBasePath = '';
window.hlx.lighthouse = new URLSearchParams(window.location.search).get('lighthouse') === 'on';

Expand All @@ -124,17 +112,8 @@ function setup() {

function init() {
setup();
sampleRUM('top');

window.addEventListener('load', () => sampleRUM('load'));

window.addEventListener('unhandledrejection', (event) => {
sampleRUM('error', { source: event.reason.sourceURL, target: event.reason.line });
});

window.addEventListener('error', (event) => {
sampleRUM('error', { source: event.filename, target: event.lineno });
});
sampleRUM();
window.addEventListener('hlx:section:loaded', sampleRUM.enhance, { once: true });
}

/**
Expand Down Expand Up @@ -533,6 +512,7 @@ function updateSectionsStatus(main) {
} else {
section.dataset.sectionStatus = 'loaded';
section.style.display = null;
window.dispatchEvent(new CustomEvent('hlx:section:loaded', { detail: { section } }));
}
}
}
Expand Down
8 changes: 1 addition & 7 deletions scripts/delayed.js
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
// eslint-disable-next-line import/no-cycle
import { sampleRUM } from './aem.js';

// Core Web Vitals RUM collection
sampleRUM('cwv');

// add more delayed functionality here
// add delayed functionality here
5 changes: 0 additions & 5 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
sampleRUM,
buildBlock,
loadHeader,
loadFooter,
Expand Down Expand Up @@ -110,10 +109,6 @@ async function loadLazy(doc) {

loadCSS(`${window.hlx.codeBasePath}/styles/lazy-styles.css`);
loadFonts();

sampleRUM('lazy');
sampleRUM.observe(main.querySelectorAll('div[data-block-name]'));
sampleRUM.observe(main.querySelectorAll('picture > img'));
}

/**
Expand Down

0 comments on commit 6d99a2c

Please sign in to comment.