Skip to content

Instantly share code, notes, and snippets.

@JMPerez
Created June 17, 2017 08:24
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JMPerez/b95d0f96780ba60424e1f9b1026130f9 to your computer and use it in GitHub Desktop.
Save JMPerez/b95d0f96780ba60424e1f9b1026130f9 to your computer and use it in GitHub Desktop.
An example of Performance Observer reporting data to Google Analytics
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