Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Track First Contentful Paint with PerformanceObserver and Google Analytics

    Mike SherovMike Sherov
    htmlHTML 5
    javascriptJavaScript

    "If you can't measure it, you can't improve it." The first step when doing performance work is to measure meaningful metrics to establish a baseline for improvement. In this lesson, we'll measure Time to First Contentful Paint, a user-centric performance metric. We'll first create a PerformanceObserver object and explore the information it captures. Next, we'll send that information to Google Analytics to track FCP over time. Lastly, we'll discover why PerformanceObserver is one of the few bits of Javascript that rightly belong in the head of your document.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: Here we have a website pre-installed with Google Analytics, a light blue background color, and an image of a kitten. Because we want to improve the performance of this site, let's instrument it and monitor the First Contentful Paint time of the page.

    In order to do this, we'll use PerformanceObserver to capture the First Contentful Paint metric and the paint timings. To do this, we instantiate a PerformanceObserver. PerformanceObserver takes in one parameter, a call-back function. That call-back function takes in as its parameter a list of performance timings.

    We can iterate through this list with a for of and calling the list docket entries on that. With that, we can console log the name of the entry and the start time of the entry. Once we've done that, we tell the observer that we want to observe types of paint.

    18 milliseconds is when the browser was first able to render the blue background. 66 milliseconds was when the browser was able to first render the first bits of that kitten. Let's log these values now to Google Analytics.

    We do this by calling the built-in GA function. First parameter is send. Second parameter is timing, which tells Google Analytics to send a timing event with a name of our performance entry followed by timing variable, which we'll here use arbitrary user ID and an integer value for how long the entry took.

    If we save this, we can verify that the network is actually sending requests by loading up image tab of the console, reloading our page, and seeing that a request went out with a timing type, the First Contentful Paint timing variable with a value of 121.

    Going back to our script, you'll notice one peculiar thing. We wrote our code in the head of the document rather than in the body of the document. This is because if you put the code in the bottom of the body, you may be listing to the PerformanceObserver too late to catch the First Contentful Paint.

    We've also in-lined this script so there's no additional load-time burden from having to load the instrumentation code. Lastly, if you need IE support, you'll need to check to see if PerformanceObserver exists on the window first and wrapping your instrumentation code within an if block.

    If we run this code again, we see that it still works. If we now visit Google Analytics and choose user timers in the site speed section of behavior, we see that it logs First Contentful Paint and first paint timings.

    Discuss

    Discuss