Skip to content

Commit

Permalink
MWPW-157445 Additional Lana LCP data (#2805)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischrischris authored Sep 3, 2024
1 parent 47dea19 commit 7216452
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
25 changes: 23 additions & 2 deletions libs/utils/logWebVitals.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,35 @@ function getElementInfo(el) {
return `${el.outerHTML.substring(0, 100)}...`;
}

function observeLCP(lanaData, delay) {
function isFragmentFromMep(fragPath, mep) {
return mep.experiments?.some(({ selectedVariant }) => {
const { commands = [], fragments = [] } = selectedVariant || {};

return commands.some((cmd) => {
try {
return new URL(cmd.target).pathname === fragPath;
} catch {
return false;
}
}) || fragments.some((cmd) => cmd?.val === fragPath);
});
}

function observeLCP(lanaData, delay, mep) {
new PerformanceObserver((list) => {
const entries = list.getEntries();
const lastEntry = entries[entries.length - 1]; // Use the latest LCP candidate
lanaData.lcp = parseInt(lastEntry.startTime, 10);
const lcpEl = lastEntry.element;
lanaData.lcpElType = lcpEl.nodeName.toLowerCase();
lanaData.lcpEl = getElementInfo(lcpEl);
const closestFrag = lcpEl.closest('.fragment');
lanaData.isFrag = closestFrag ? 'true' : 'false';
if (closestFrag) {
lanaData.isMep = isFragmentFromMep(closestFrag.dataset.path, mep) ? 'true' : 'false';
} else {
lanaData.isMep = 'false';
}

setTimeout(() => {
sendToLana(lanaData);
Expand Down Expand Up @@ -99,7 +120,7 @@ export default function webVitals(mep, { delay = 1000, sampleRate = 50 } = {}) {
const lanaData = {};
logMepExperiments(lanaData, mep);
observeCLS(lanaData);
observeLCP(lanaData, delay);
observeLCP(lanaData, delay, mep);
}
if (getConsent()) {
handleEvent();
Expand Down
11 changes: 3 additions & 8 deletions test/utils/logWebVitals.test.js

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

0 comments on commit 7216452

Please sign in to comment.