Lesson 10 of 11
Web Vitals To DataLayer
① Connect your GTM container
Paste your container ID to load it into this page. It only ever runs here.
Advanced: use a specific environment
Speed is measurable, and worth measuring. Google's Core Web Vitals (largest contentful paint, cumulative layout shift, and interaction to next paint) are how it scores the experience. The browser exposes them through PerformanceObserver, so you can read them on a real page and send them to GA4 as a custom event.
The pattern is a small Custom HTML listener that observes the metrics and pushes a web_vitals event carrying the metric name and value, then a Custom Event trigger and a GA4 Event tag that maps those into event parameters. In GA4 you get a CWV report broken down by page.
This page is already reading LCP and CLS live below. Push the event, then build the trigger and tag to send it on for real.
Goal
Read Core Web Vitals with PerformanceObserver and send them to GA4 as a web_vitals event.
Build it in GTM
Listener: Custom HTML - Web Vitals
- Go to Tags → New → Custom HTML.
- Paste the official
web-vitalslibrary snippet (or aPerformanceObserver) that pushes{event:'web_vitals', metric_name, metric_value}for each metric. - Trigger it on All Pages, name it
Custom HTML - Web Vitalsand Save.
Trigger: Custom Event - Web Vitals
Add a Custom Event trigger with event nameweb_vitals, name itCustom Event - Web Vitalsand Save.Tag: GA4 - Web Vitals
- Add a GA4 Event tag, event name
web_vitals, with parametersmetric_nameandmetric_valuemapped from Data Layer Variables. - Fire it on
Custom Event - Web Vitals, name itGA4 - Web Vitalsand Save.
- Add a GA4 Event tag, event name
Debug in Tag Assistant
Copy this lesson's live URL and paste it into GTM Preview, that is the page Tag Assistant connects to. It has the clickable elements, so this page stays clean for reading.
- In your GTM, click Preview.
- Paste the live URL above and click Connect.
- Interact with the live page and watch your tag fire in Tag Assistant.
What you should expect to see
web_vitals event and your GA4 tag fires once per metric, with metric_name and metric_value visible under the tag in Tag Assistant.Verify your container
Built it? Export your container, Admin → Export Container, choose your workspace, then drop the JSON here to check it against this lesson.
Drop your container .json here
or browse · checked in your browser, nothing is uploaded