Skip to content

Instantly share code, notes, and snippets.

@willmendesneto
Created March 29, 2021 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willmendesneto/f9b7fc1a1800badf7e24136ccca3af75 to your computer and use it in GitHub Desktop.
Save willmendesneto/f9b7fc1a1800badf7e24136ccca3af75 to your computer and use it in GitHub Desktop.
Logging Long Tasks in your browser and Sending the data to be logged in your Analytics Platform
const observer = new PerformanceObserver((list) => {
const freezeThreshold = 300;
const perfEntries = list.getEntries();
// Get a list of all the `longtask` entries to be added as payload for your Analytics API
const analyticsData = perfEntries.filter(entry => entry.duration > freezeThreshold);
// In case of any longtask found, it sends the data to be logged in your Analytics platform
if (analyticsData.length > 0) {
navigator.sendBeacon('/my-analytics-log-platform', analyticsData);
}
});
// register observer for long task notifications
observer.observe({entryTypes: ['longtask']});
// Long script execution after this will result in queueing
// and receiving 'longtask' entries in the observer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment