This is a simple Python script for extracting Web Vitals event data out of Google Analytics using the Reporting API. It outputs distribution and percentile values that you can pipe/paste into charting tools.
Using GA segments you can visualize things like anonymous vs logged-in users or comparing converting vs non-converting sessions.
-
Set up Web Vitals tracking in Google Analytics as described in the
web-vitals
library usage docs -
Follow steps in the Analytics Reporting API v4 quick start to get the python client and credentials set up
-
Edit
vitals.py
to include the path to your JSON credentials and GA profile ID -
Edit the
fetch_data()
function with the filters or segments you want to use
Returns 3 buckets using the Good, Needs Improvement, and Poor thresholds defined by the Web Vitals project. Supports LCP, FID, CLS, FCP, and TTFB.
Returns an overall value for a given percentile.
Returns a timeseries for a given percentile. Adjust the time delta for daily/weekly/monthly intervals.