Skip to content

Instantly share code, notes, and snippets.

@gilbertococchi
Last active August 22, 2023 16:58
Show Gist options
  • Save gilbertococchi/13a28287d5514c0ca65088ade78d1bb3 to your computer and use it in GitHub Desktop.
Save gilbertococchi/13a28287d5514c0ca65088ade78d1bb3 to your computer and use it in GitHub Desktop.
INP to LoAF Attribution
(function() {
if (PerformanceObserver.supportedEntryTypes.includes('long-animation-frame')) {
window.longestINPtoLoAFScript = null;
function process(eventEntry) {
let matchingLoafs = [];
for (const event_entry of eventEntry.entries) {
if (event_entry.entryType == "first-input")
continue;
let entryLoafs = [];
performance.getEntriesByType('long-animation-frame');
const longAnimationFrames = performance.getEntriesByType('long-animation-frame');
longAnimationFrames.filter(loaf => {
return event_entry.startTime < (loaf.startTime + loaf.duration) && loaf.startTime < (event_entry.startTime + event_entry.duration);
}).forEach(loaf => {
performance.measure("LoAF", {
start: loaf.startTime,
end: loaf.startTime + loaf.duration,
});
matchingLoafs.push(loaf);
});
}
matchingLoafs.forEach((loaf) => {
for (const script of loaf.scripts) {
if (script.duration > (window.longestINPtoLoAFScript?.duration ?? 0))
window.longestINPtoLoAFScript = script;
}
});
if (window.longestINPtoLoAFScript) {
sendToGA(longestINPtoLoAFScript);
window.longestINPtoLoAFScript = null;
}
}
webVitals.onINP(process, {
durationThreshold: 150
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment