Instrument Core Web Vitals in Production and Block Regressions in CI.

By · Published · Updated · About 8 min read

On this page (10)
  1. Decide what the pipeline must answer
  2. Collect field data with the web-vitals library
  3. Receive the beacons on your server
  4. Add attribution when you need a cause
  5. Treat single-page applications with care
  6. Watch what Google sees with the CrUX API
  7. Enforce budgets in CI with Lighthouse CI
  8. Build the common fixes into the platform
  9. Work one example from beacon to budget
  10. Keep a cadence

A team fixes its Core Web Vitals, and Search Console turns green. Three releases later it turns red again. Nobody saw it coming, because the only measurement was a monthly look at a report with a 28-day lag.

Core Web Vitals are Google's three user experience metrics. Largest Contentful Paint (LCP) measures loading, and Interaction to Next Paint (INP) measures responsiveness. Cumulative Layout Shift (CLS) measures visual stability. Google's ranking systems use them, so performance work is also SEO work.

This guide covers the measurement side: real user data, alerts, and budgets in the build pipeline. To find the cause of a specific failure, read Core Web Vitals debugging. For the fix patterns per metric, read Core Web Vitals engineering.

Decide what the pipeline must answer

Your pipeline answers three questions, each with a different tool:

Decide what the pipeline must answer
QuestionDataTool
What do real users experience, per page and per release?Field data from your own visitorsThe web-vitals library and your analytics endpoint
What does Google see?Field data from Chrome users, 28-day rolling windowThe Chrome UX Report (CrUX) API and Search Console
Will this pull request make it worse?Lab data from a controlled runLighthouse CI in your CI system

Google assesses each metric at the 75th percentile of real page loads. The good thresholds are LCP of 2.5 seconds or less, INP of 200 ms or less, and CLS of 0.1 or less. Lab data diagnoses problems. Field data validates the result. A page can score perfectly in the lab while real users on slow phones and networks fail it.

Collect field data with the web-vitals library

Google's web-vitals library measures the three metrics in the browser the same way Chrome reports them. Install it:

npm install web-vitals

Send each metric to your own endpoint. Include the page path so that you can group results by template:

import {onCLS, onINP, onLCP} from 'web-vitals';

function sendToAnalytics(metric) {
  const body = JSON.stringify({
    name: metric.name,
    value: metric.value,
    rating: metric.rating,
    id: metric.id,
    navigationType: metric.navigationType,
    page: location.pathname
  });
  // sendBeacon survives page unload; fetch with keepalive is the fallback
  if (!navigator.sendBeacon('/analytics', body)) {
    fetch('/analytics', {method: 'POST', body, keepalive: true});
  }
}

onCLS(sendToAnalytics);
onINP(sendToAnalytics);
onLCP(sendToAnalytics);

The library reports INP and CLS when the page is hidden, because both accumulate over the page's life. The rating field already classifies each value as good, needs improvement, or poor.

Receive the beacons on your server

A beacon with a string body arrives as text/plain, so parse it as text:

// Express endpoint
app.post('/analytics', express.text({type: '*/*'}), (req, res) => {
  const {name, value, rating, id, page} = JSON.parse(req.body);
  analyticsDB.insert({
    metric: name, value, rating, sessionId: id, page,
    userAgent: req.headers['user-agent'],
    timestamp: new Date()
  });
  res.sendStatus(204);
});

Store raw values, not averages. Core Web Vitals are judged at the 75th percentile, and an average hides the slow tail. In PostgreSQL, the p75 LCP for a template is one query:

SELECT percentile_cont(0.75) WITHIN GROUP (ORDER BY value)
FROM vitals WHERE metric = 'LCP' AND page LIKE '/products/%';

Add attribution when you need a cause

The attribution build adds the diagnostic detail that tells you why a value is slow. It costs more bytes, so many teams ship it to a sample of sessions or turn it on while they debug:

import {onLCP} from 'web-vitals/attribution';

onLCP(({value, attribution}) => {
  sendToAnalytics({name: 'LCP', value,
    element: attribution.element,
    ttfb: attribution.timeToFirstByte,
    loadDelay: attribution.resourceLoadDelay,
    loadDuration: attribution.resourceLoadDuration,
    renderDelay: attribution.elementRenderDelay});
});

These field names apply to web-vitals version 4 and later, released in 2024. Version 4 renamed several attribution fields, so check the version before you copy older samples.

Treat single-page applications with care

In a single-page application, INP and CLS keep accumulating across client-side route changes. Field data attributes them to the URL of the first full page load. Record the current route with each beacon, as the sample above does. You can then see which view produced a slow interaction.

Single-page applications also struggle with LCP, because the content does not exist until JavaScript runs. Render the first view on the server or at build time with Next.js, Nuxt, or SvelteKit. For content pages, static generation with periodic regeneration is the usual fit. These examples use the Next.js Pages Router; in the App Router, server components fetch data on the server by default.

// pages/blog/[slug].js: static page, regenerated at most once an hour
export async function getStaticProps({params}) {
  return {props: {post: await fetchPost(params.slug)}, revalidate: 3600};
}

export async function getStaticPaths() {
  const posts = await fetchAllPosts();
  return {paths: posts.map(p => ({params: {slug: p.slug}})), fallback: 'blocking'};
}

For pages that must be fresh on every request, getServerSideProps fetches the data on the server so that the HTML arrives complete. For framework-level trade-offs, read JavaScript SEO frameworks.

Watch what Google sees with the CrUX API

Your own data shows every visit. CrUX shows the Chrome data that Google uses. Check it on a schedule and alert when the 75th percentile crosses a threshold:

async function checkCoreWebVitals(url) {
  const response = await fetch(
    `https://chromeuxreport.googleapis.com/v1/records:queryRecord?key=${API_KEY}`,
    {
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({
        url,
        formFactor: 'PHONE',
        metrics: ['largest_contentful_paint', 'interaction_to_next_paint', 'cumulative_layout_shift']
      })
    }
  );
  const {record} = await response.json();
  if (record.metrics.largest_contentful_paint.percentiles.p75 > 2500) {
    sendAlert(`LCP p75 over 2,500 ms on ${url}`);
  }
}

Query the phone form factor first. Google uses mobile-first indexing, and phones are usually the harder case. CrUX returns data only for URLs with enough traffic; for smaller pages, query by origin instead.

Enforce budgets in CI with Lighthouse CI

Budgets stop the slow accumulation of small regressions. Run Lighthouse CI on every pull request against a staging build. Save the GitHub Actions job as .github/workflows/lighthouse.yml:

name: Lighthouse CI
on: [pull_request]
jobs:
  lighthouse:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Run Lighthouse CI
        uses: treosh/lighthouse-ci-action@v9   # check for a newer major version
        with:
          urls: |
            https://staging.example.com/
            https://staging.example.com/product
          configPath: ./lighthouserc.json
          uploadArtifacts: true

Put the budgets in lighthouserc.json as assertions. Sizes are in bytes:

{
  "ci": {
    "assert": {
      "assertions": {
        "largest-contentful-paint": ["error", {"maxNumericValue": 2500}],
        "cumulative-layout-shift": ["error", {"maxNumericValue": 0.1}],
        "total-blocking-time": ["warn", {"maxNumericValue": 200}],
        "resource-summary:script:size": ["error", {"maxNumericValue": 300000}],
        "resource-summary:image:size": ["warn", {"maxNumericValue": 500000}],
        "resource-summary:total:size": ["error", {"maxNumericValue": 1000000}]
      }
    }
  }
}

Lighthouse loads the page without user input, so it cannot measure INP. Total Blocking Time is the lab proxy. Keep INP in your field monitoring. The workflow in more detail is in Lighthouse in GitHub Actions, and the wider test setup in SEO testing in a CI/CD pipeline.

To see what fills a JavaScript budget, add webpack-bundle-analyzer with analyzerMode: 'static'. It writes an HTML report of every module in each bundle.

Build the common fixes into the platform

Some fixes belong in shared infrastructure, so that each page gets them without extra work.

Cache static assets for a year with immutable, and keep HTML revalidating. In Express, express.static('public', {maxAge: '1y', immutable: true}) does this, with a setHeaders hook that sends Cache-Control: no-cache for .html files. Cache slow dynamic data in Redis, for example with client.setEx(key, 300, value) for five minutes. A Cloudflare Worker can apply Cache-Control: public, max-age=31536000, immutable to /static/ paths at the edge.

Generate modern image formats at build time. In webpack, image-minimizer-webpack-plugin with the sharp encoder can emit WebP at quality 80 and AVIF at quality 70. Serve them in a picture element with a JPEG fallback and explicit width and height.

Load third-party scripts late. For analytics that does not need the first second, a small loader creates the script element late. It waits for the load event plus a short delay, for example 2 seconds. Partytown moves a third-party script into a web worker; you mark the script element with type="text/partytown".

Keep the main thread free. Split heavy components with lazy() and dynamic import(). Use splitChunks in webpack to separate vendor code. Move computation into Web Workers; Comlink lets you call a worker like an async function. Register touchstart and wheel listeners with {passive: true}. Batch scroll-driven visual updates in requestAnimationFrame.

Keep layouts stable. Give video iframes a container with aspect-ratio: 16 / 9. Reserve ad slots with min-height. Show skeleton screens where content loads late. Use font-display: optional and preload the main font. Subset Google Fonts with the text= parameter when a font only renders a few words. Animate transform and opacity only, and isolate widgets with contain: layout style paint.

Work one example from beacon to budget

The inputs in this example are hypothetical. A store's product page template gets a week of field beacons. The p75 LCP on phones is 3.4 seconds, in the poor range. The attribution beacons show that resource load delay is the largest part. The hero image is a CSS background, so the browser finds it only after it parses the stylesheet.

The team changes the hero to an img element with fetchpriority="high", explicit dimensions, and AVIF and WebP sources. It adds a preload for the image in the head. It adds the product template URL to the Lighthouse CI job, with the LCP assertion at 2,500 ms. A future change that delays the image then fails the pull request.

Then the team measures. It records the p75 LCP from its own beacons for the week before and the week after the release. It checks CrUX for the template URL over the following 28 days, and Search Console after that. The example stops at the measurement: record your own before and after values, not an expected gain.

Keep a cadence

Monitor continuously through your beacons, CrUX alerts, and Search Console. After a major deployment, confirm within 48 hours that the field values have not regressed. Run a focused optimization sprint each quarter, or when Search Console flags a new issue.

Most optimizations improve experience without removing features, but some trade-offs exist. Aggressive code splitting adds requests. Lazy loading delays a feature until the user needs it. A removed third-party script takes its function with it. When a trade-off is real, favor what the user sees over background analytics and non-essential features.

Keep a record of instrumenting Core Web Vitals

Next steps for instrumenting Core Web Vitals

Topic map for instrumenting Core Web Vitals

Every page in this topic

  1. Decide whether AMP still earns its maintenance cost on your site
  2. Stop your code from creating duplicate URLs with canonicals, redirects, and clean routing
  3. Configure your CDN so crawlers get fresh pages and users get fast ones
  4. Run an SEO audit that fixes ranking barriers first and trivia last
  5. Trace each failing Core Web Vital to its root cause
  6. Instrument Core Web Vitals in production and block regressions in CI
  7. Apply code patterns that keep LCP, INP, and CLS inside Google's thresholds
  8. Build category pages that rank for the searches your shoppers start with
  9. Add Product, Offer, and Breadcrumb markup that makes your store eligible for rich results
  10. Control faceted navigation so filters help shoppers without flooding Google with URLs
  11. Fix your product page template so each product ranks for its buying searches
  12. Ship SEO fixes at the CDN edge when the backend cannot change
  13. Get engineering to ship your SEO fixes by writing them as engineering work
  14. Run seven SEO quick wins in 90 days to test organic search as a channel
  15. Split Google Search Console reports by role and act on each one
  16. Test how Googlebot renders your JavaScript site and close the indexing gaps
  17. Build SEO into a headless CMS so Google indexes every page
  18. Implement hreflang so each searcher lands on the right language version
  19. Choose the right HTTP status code for each URL and keep Google's index clean
  20. Optimize website images to speed up pages and rank in image search
  21. Notify search engines of new URLs with IndexNow and the Google Indexing API
  22. Set up international SEO so each market finds its own version of your site
  23. Plan international SEO as a product roadmap and launch markets in order
  24. Configure React, Vue, and Angular rendering so search engines index every page
  25. Fix JavaScript SEO so crawlers read your content without a render step
  26. Lazy-load images, video, and content without hiding them from Google
  27. Run Lighthouse in GitHub Actions to block SEO regressions before merge
  28. Read server logs to see what Googlebot crawls and stop wasted crawl budget
  29. Diagnose technical SEO problems and get engineering to fix them without writing code
  30. Design mobile-first pages that Google indexes in full and users can tap without errors

More in SEO by role

Neighboring topics

Services for this work