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

[MWPW-154969] Performance consent #2714

Merged
merged 10 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
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
25 changes: 20 additions & 5 deletions libs/utils/logWebVitals.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,24 @@
});
}

export default function webVitals(mep, { delay = 1000 } = {}) {
const lanaData = {};
logMepExperiments(lanaData, mep);
observeCLS(lanaData);
observeLCP(lanaData, delay);
export default function webVitals(mep, { delay = 1000, sampleRate = 50 } = {}) {
const isChrome = () => {
const nav = window.navigator;
return nav.userAgent.includes('Chrome') && nav.vendor.includes('Google');
};
if (!isChrome() || Math.random() * 100 > sampleRate) return;
const getConsent = () => window.adobePrivacy?.activeCookieGroups().indexOf('C0002') !== -1;
function handleEvent() {
if (!getConsent()) return;
const lanaData = {};
logMepExperiments(lanaData, mep);
observeCLS(lanaData);
observeLCP(lanaData, delay);
}
if (getConsent()) {
handleEvent();
return;
}
window.addEventListener('adobePrivacy:PrivacyConsent', handleEvent, { once: true });
window.addEventListener('adobePrivacy:PrivacyCustom', handleEvent, { once: true });

Check warning on line 109 in libs/utils/logWebVitals.js

View check run for this annotation

Codecov / codecov/patch

libs/utils/logWebVitals.js#L108-L109

Added lines #L108 - L109 were not covered by tests
}
20 changes: 7 additions & 13 deletions libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -999,18 +999,6 @@ export function scrollToHashedElement(hash) {
});
}

function logPagePerf() {
if (getMetadata('pageperf') !== 'on') return;
mokimo marked this conversation as resolved.
Show resolved Hide resolved
const isChrome = () => {
const nav = window.navigator;
return nav.userAgent.includes('Chrome') && nav.vendor.includes('Google');
};
const sampleRate = parseInt(getMetadata('pageperf-rate'), 10) || 50;
if (!isChrome() || Math.random() * 100 > sampleRate) return;
import('./logWebVitals.js')
.then((mod) => mod.default(getConfig().mep, getMetadata('pageperf-delay') || 1000));
}

export async function loadDeferred(area, blocks, config) {
const event = new Event(MILO_EVENTS.DEFERRED);
area.dispatchEvent(event);
Expand Down Expand Up @@ -1039,7 +1027,13 @@ export async function loadDeferred(area, blocks, config) {
sampleRUM.observe(area.querySelectorAll('picture > img'));
});

logPagePerf();
if (getMetadata('pageperf') === 'on') {
import('./logWebVitals.js')
.then((mod) => mod.default(getConfig().mep, {
delay: getMetadata('pageperf-delay'),
sampleRate: parseInt(getMetadata('pageperf-rate'), 10),
}));
}
}

function initSidekick() {
Expand Down
11 changes: 10 additions & 1 deletion test/utils/logWebVitals.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion test/utils/logWebVitalsUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@ document.head.innerHTML = `

document.body.innerHTML = await readFile({ path: './mocks/body.html' });

describe('Log Web Vitals', () => {
describe('Log Web Vitals Utils', () => {
let intervalId;
before(() => {
window.adobePrivacy = { activeCookieGroups: () => ['C0002'] };
intervalId = setInterval(() => {
window.dispatchEvent(new Event('adobePrivacy:PrivacyCustom'));
}, 100);
});

after(() => {
delete window.adobePrivacy;
clearInterval(intervalId);
});

it('Logs data to lana', (done) => {
window.lana = {
log: (logStr, logOpts) => {
Expand Down