Skip to content

Commit

Permalink
Merge branch 'main' of github.com:adobe/aem-boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
herzog31 committed Mar 15, 2024
2 parents 2dfc035 + a5fef7f commit 88cedae
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions scripts/aem.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Adobe. All rights reserved.
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
Expand All @@ -22,6 +22,9 @@
* for instance the href of a link, or a search term
*/
function sampleRUM(checkpoint, data = {}) {
const SESSION_STORAGE_KEY = 'aem-rum';
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 }));
Expand Down Expand Up @@ -53,12 +56,21 @@ function sampleRUM(checkpoint, data = {}) {
.join('');
const random = Math.random();
const isSelected = random * weight < 1;
const firstReadTime = Date.now();
const firstReadTime = window.performance ? window.performance.timeOrigin : Date.now();
const urlSanitizers = {
full: () => window.location.href,
origin: () => window.location.origin,
path: () => window.location.href.replace(/\?.*$/, ''),
};
// eslint-disable-next-line max-len
const rumSessionStorage = sessionStorage.getItem(SESSION_STORAGE_KEY)
? JSON.parse(sessionStorage.getItem(SESSION_STORAGE_KEY))
: {};
// eslint-disable-next-line max-len
rumSessionStorage.pages = (rumSessionStorage.pages ? rumSessionStorage.pages : 0)
+ 1
/* noise */ + (Math.floor(Math.random() * 20) - 10);
sessionStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(rumSessionStorage));
// eslint-disable-next-line object-curly-newline, max-len
window.hlx.rum = {
weight,
Expand All @@ -68,8 +80,10 @@ function sampleRUM(checkpoint, data = {}) {
firstReadTime,
sampleRUM,
sanitizeURL: urlSanitizers[window.hlx.RUM_MASK_URL || 'path'],
rumSessionStorage,
};
}

const { weight, id, firstReadTime } = window.hlx.rum;
if (window.hlx && window.hlx.rum && window.hlx.rum.isSelected) {
const knownProperties = [
Expand All @@ -85,32 +99,35 @@ function sampleRUM(checkpoint, data = {}) {
'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: Date.now() - firstReadTime,
...data,
weight, id, referer: window.hlx.rum.sanitizeURL(), checkpoint, t, ...data,
},
knownProperties,
);
const url = `https://rum.hlx.page/.rum/${weight}`;
// eslint-disable-next-line no-unused-expressions
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 || {
load: () => sampleRUM('pagesviewed', { source: window.hlx.rum.rumSessionStorage.pages }) || true,
cwv: () => sampleRUM.cwv(data) || true,
lazy: () => {
// use classic script to avoid CORS issues
const script = document.createElement('script');
script.src = 'https://rum.hlx.page/.rum/@adobe/helix-rum-enhancer@^1/src/index.js';
script.src = new URL(
'.rum/@adobe/helix-rum-enhancer@^1/src/index.js',
sampleRUM.baseURL,
).href;
document.head.appendChild(script);
return true;
},
Expand Down

0 comments on commit 88cedae

Please sign in to comment.