How to Integrate Google Analytics with Your Headless CMS
Connect Google Analytics to your headless CMS so every published page, author, topic, and campaign can be tracked with clean content metadata instead of manual spreadsheet mapping.
What is Google Analytics?
Google Analytics is Google's web and app analytics platform for tracking traffic, user behavior, acquisition channels, conversions, and audience segments. Marketing, product, growth, and editorial teams use Google Analytics 4 to understand which pages bring in visitors, which paths convert, and where users drop off. It's one of the most widely used analytics tools because it connects site behavior to Google Ads, Search Console, BigQuery exports, and reporting workflows.
Why integrate Google Analytics with a headless CMS?
When your analytics setup only knows a URL, your reports stop at page-level traffic. You can see that /blog/ai-buyers-guide got 18,000 views, but you can't easily group performance by content type, author, industry, funnel stage, product line, or campaign unless someone maintains that mapping by hand. That gets messy fast when you're publishing across a website, docs, landing pages, and localized pages.
Architecture overview
A typical Sanity and Google Analytics integration has two flows. First, content flows from the Content Lake to your site or app through GROQ queries. The frontend renders the page and sends user behavior to GA4 with the Google tag, Google Tag Manager, or a framework package such as @next/third-parties. The page_view or custom events include content metadata, such as content_type, author, topic, locale, and funnel_stage, pulled from the same structured content used to render the page.
Common use cases
Content performance by metadata
Send author, topic, content type, locale, and funnel stage to GA4 so teams can compare performance beyond URL and page title.
Campaign launch tracking
Fire a content_published event when a landing page goes live, then match launch timing against traffic, conversions, and acquisition channels.
A/B test reporting context
Pass experiment IDs, variant names, and page modules from Sanity into Google Analytics events so test results include the content users actually saw.
Localization reporting
Track page views and conversions by language, region, and market without maintaining separate spreadsheet mappings for translated URLs.
Step-by-step integration
- 1
Set up Google Analytics 4
Create or choose a GA4 property, add a Web data stream, and copy the Measurement ID, such as G-ABCD1234. If you'll send server-side events, open Admin, Data streams, choose your stream, then create a Measurement Protocol API secret. For frontend tracking, install the Google tag directly, through Google Tag Manager, or with a framework package.
- 2
Define the tracking plan
Decide which content fields belong in GA4. Common fields include content_type, author, primary_topic, locale, funnel_stage, campaign_id, and publish_date. Keep names short and stable because GA4 event parameter names have rules, and custom dimensions take time to appear in reports.
- 3
Model the fields in Sanity Studio
Add schema fields that match the tracking plan. For example, an article document might include title, slug, author reference, topics array, locale, funnelStage, campaignId, and publishedAt. Because the schema is code, developers can review changes before they affect tracking.
- 4
Create the sync mechanism
Configure a Sanity webhook to trigger on publish events for the document types you want to track. Point it at a Sanity Function, Next.js API route, serverless function, or other HTTPS endpoint. Use a webhook secret so the endpoint can reject requests that didn't come from Sanity.
- 5
Call Google Analytics
In the handler, use @sanity/client and GROQ to fetch the published document with joined references, such as author name and primary topic. Then send a GA4 Measurement Protocol request with an event name like content_published and parameters that match your tracking plan.
- 6
Test the frontend and reports
Use GA4 DebugView for client-side events and the Measurement Protocol validation endpoint for server-side payloads. Publish a test document, confirm the webhook fires, check the network request, and verify that custom dimensions populate in Google Analytics after processing.
How Sanity + Google Analytics works
Build your Google Analytics integration on Sanity
Sanity's AI Content Operating System gives you structured content, real-time events, and flexible APIs for connecting Google Analytics to the content your teams publish every day.
Start building free โCMS approaches to Google Analytics
| Capability | Traditional CMS | Sanity |
|---|---|---|
| Content metadata for GA4 | The Content Lake stores typed JSON, and GROQ can project analytics-ready metadata, including referenced author, topic, locale, and campaign fields. | |
| Publish-time analytics events | Webhooks and Functions can react to publish, update, and delete events, then send GA4 Measurement Protocol events without a separate worker service. | |
| Field-level query control | GROQ filters, joins, and projections let you fetch only the fields Google Analytics needs for each event. | |
| A/B test and campaign context | Editors can work with campaign_id, experiment_id, variant, and funnel_stage fields in Sanity Studio, then those values can flow into GA4 events. | |
| Trade-offs and governance | Schema-as-code, Content Releases, and reviewable changes help keep tracking fields consistent, though teams still need a clear GA4 naming plan. |
Keep building
Explore related integrations to complete your content stack.
Sanity + Segment
Send content metadata from Sanity into Segment so page, product, and campaign events can route to multiple analytics and marketing tools.
Sanity + Amplitude
Connect structured content attributes to product analytics so teams can compare activation, retention, and conversion by content experience.
Sanity + BigQuery
Join Sanity content data with GA4 BigQuery exports for deeper reporting across traffic, conversions, authors, topics, and campaigns.