-
-
Save caraya/6f5dd6cb6a5080792c1923399e646758 to your computer and use it in GitHub Desktop.
An example of Performance Observer reporting data to Google Analytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const observer = new PerformanceObserver((list) => { | |
for (const entry of list.getEntries()) { | |
// `name` will be either 'first-paint' or 'first-contentful-paint'. | |
const metricName = entry.name; | |
const time = Math.round(entry.startTime + entry.duration); | |
ga('send', 'event', { | |
eventCategory: 'Performance Metrics', | |
eventAction: metricName, | |
eventValue: time, | |
nonInteraction: true, | |
}); | |
} | |
}); | |
// Start observing paint entries. | |
observer.observe({entryTypes: ['paint']}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment