Created
June 17, 2017 08:24
-
-
Save JMPerez/b95d0f96780ba60424e1f9b1026130f9 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