Connecting Sanity to PostHog
Sanity is a headless content platform, which means like every headless CMS it holds no commercial record and the tracking question lives elsewhere. PostHog can be self-hosted, which changes the calculus entirely: the data stays on infrastructure you control, and so does the responsibility for everything that goes wrong with it. The join between them succeeds or fails on one thing: whether the distinct_id reaches the front-end conversion, not a Sanity record before the conversion happens, because PostHog can only match a conversion it can tie back to a click it recognizes.
Free30 minutesNo obligation
Specific to this pair
PostHog experiments on Sanity content: decide variants at request time
PostHog feature flags can pick which Sanity content variant the front end queries; put the served document _id on the page view. Evaluate flags per request in middleware or client-side, never at build time on static pages, or both groups see identical content. Merge anonymous and signed-up identities to measure activation, and exclude Studio preview sessions.
PostHog feature flags can choose between Sanity content variants: two hero documents, two pricing-page layouts, two headlines stored as separate fields. The flag decides which document the front end queries, and the served document _id should go onto the page view as an event property so results tie to the content people actually saw.
Statically generated pages are the trap. If the variant is chosen at build time, every visitor gets the same page while PostHog records a split based on the flag, and the experiment compares two groups who saw identical content. Evaluate the flag in middleware, server-side per request, or client-side with a guard against flicker, and check a sample of recordings to confirm each group saw its variant.
Identity follows the usual pattern: anonymous distinct_id on the marketing site, identified at sign-up in the product, merged so the experiment's effect on activation can be measured. Exclude Sanity Studio preview sessions, which load the real site and enroll editors into experiments. On a self-hosted instance, check that the capture endpoint is on a domain your content security policy and ad blockers allow.
Sanity → PostHog · what breaks
Why this join usually fails — Sanity → PostHog
- 01
The distinct_id never reaches Sanity
The front-end application does the routing, the caching and the identifier capture. Single-page navigation is the usual failure and it belongs to the framework rather than to Sanity.
- 02
A headless stack diagnosed at the wrong layer
Route changes not tracked, identifiers lost between pages — these are front-end framework problems that get filed against the CMS because that is the name everyone knows.
- 03
Self-hosting the analytics and the outage together
The advantage of owning the pipeline is that nothing blocks it. The cost is that when the ingestion instance fills its disk on a Saturday, the events are gone and nobody is paged. Hosted tools fail differently and more visibly.
- 04
Nothing reconciles afterwards
PostHog shows events in the activity view within moments. On a self-hosted instance, check the ingestion lag as well — events that are queued are not events that are lost, and the two need different responses.
Sanity → PostHog · the fix
What working looks like — Sanity → PostHog
The distinct_id on every conversion
Conversions arriving in PostHog
The event lands on the person profile and becomes available in insights, funnels and session replay.
Reconciled against the source
Sanity → PostHog · architecture
How Sanity and PostHog actually connect
- 1
Sanity holds the truth
Sanity's real-time content APIs make it excellent at what it does and irrelevant to conversion measurement. Establishing that early saves the client money.
- 2
PostHog needs a click to match
PostHog needs a distinct_id, an event name and its properties. Self-hosted instances additionally need the ingestion endpoint reachable and the project API key correct, which is a class of failure hosted tools do not have.
- 3
The identifier is the bridge
The distinct_id, captured at the visit and carried into the front-end conversion, not a Sanity record.
- 4
The event source decides reliability
Send from wherever the money is recorded, not from the content layer.
- 5
Deduplication at the destination
PostHog deduplicates on the event UUID where supplied. Without one, a retried batch duplicates, and on a self-hosted instance there is no vendor support desk to notice it for you.
Sanity → PostHog · normal or broken
Working vs. broken, side by side — Sanity → PostHog
Normal, not a fault
The distinct_id present on the conversion
Sent from your commerce or CRM system
Deduplicated correctly
PostHog deduplicates on the event UUID where supplied. Without one, a retried batch duplicates, and on a self-hosted instance there is no vendor support desk to notice it for you.
Reconciles with the source
Actually broken
The distinct_id missing on most records
The front-end application does the routing, the caching and the identifier capture. Single-page navigation is the usual failure and it belongs to the framework rather than to Sanity.
Sent from the browser
Route changes not tracked, identifiers lost between pages — these are front-end framework problems that get filed against the CMS because that is the name everyone knows.
Counted twice, or not at all
The advantage of owning the pipeline is that nothing blocks it. The cost is that when the ingestion instance fills its disk on a Saturday, the events are gone and nobody is paged. Hosted tools fail differently and more visibly.
Nobody has reconciled it
Sanity → PostHog · diagnose
Diagnostic order — Sanity → PostHog
- 01
Count conversions carrying the distinct_id
- 02
Follow one real conversion end to end
- 03
Identify the front-end framework, not the CMS
The routing behaviour that breaks tracking belongs to Next.js, Nuxt or whatever renders the site.
- 04
Check ingestion health and disk before debugging any missing event
On self-hosted instances the answer is infrastructure far more often than it is instrumentation.
- 05
Read the match rate, not the success message
PostHog shows events in the activity view within moments. On a self-hosted instance, check the ingestion lag as well — events that are queued are not events that are lost, and the two need different responses.
Sanity → PostHog · prove
How it gets proven — Sanity → PostHog
Source of truth
Your commerce, CRM or billing system — Sanity has no orders and no customers.
- The distinct_id confirmed present on a live conversion.
- Purchase events reconciled per transaction against your billing system, with ingestion lag checked over the same window.
- The conversion confirmed sourced from the commerce system, with front-end routing verified separately.
Sanity → PostHog · free audit
What we check in the free audit
- Count conversions carrying the distinct_id
- Follow one real conversion end to end
- Identify the front-end framework, not the CMS
- Check ingestion health and disk before debugging any missing event
- Read the match rate, not the success message
Checked against
Your commerce, CRM or billing system — Sanity has no orders and no customers.
Free30 minutesNo obligation
Sanity → PostHog · when not to buy
When you don't need this — Sanity → PostHog
- Measure identifier coverage yourself first — pull your last hundred conversions and count how many carry the distinct_id.
- There is no Sanity-specific build. Fix the front end and the commerce integration; the CMS is not in the path.
- If you have no reason to keep the data on your own infrastructure and no team to run it, the hosted product removes the only real downside of this one.
Sanity → PostHog · what we deliver
What gets built — Sanity → PostHog
Identifier capture into Sanity
Capture and persist the identifier server-side in the front-end application, then carry it into the system that records the sale.
Event source: your commerce or CRM system
Send from wherever the money is recorded, not from the content layer.
Dispatch to PostHog
The PostHog capture API, posted server-side from your backend to your own instance or to PostHog Cloud.
Reversals where they exist
Handled by the commerce system.
Written guarantee
95%+ tracking accuracy
Measured against Your commerce, CRM or billing system — Sanity has no orders and no customers.. Miss it and we keep working at no cost.
From $400 · See packages
FAQ
Sanity → PostHog questions, answered
Yes. Let the flag pick which document to query and record the served document ID, but evaluate the flag per request, not at build time.
The variant was chosen at build time, so everyone saw the same page.
Almost always because the distinct_id never reached the front-end conversion, not a Sanity record. The front-end application does the routing, the caching and the identifier capture. Single-page navigation is the usual failure and it belongs to the framework rather than to Sanity. Measure coverage before debugging the dispatch.
PostHog identifies by distinct_id with an alias call to merge anonymous and identified users. On a self-hosted instance you also control the ingestion endpoint, which means the identifier survives ad blockers that would kill a third-party pixel — one of the few genuine technical advantages available to a first-party setup.
Your commerce or CRM system. Send from wherever the money is recorded, not from the content layer.
PostHog shows events in the activity view within moments. On a self-hosted instance, check the ingestion lag as well — events that are queued are not events that are lost, and the two need different responses.
More on Sanity → PostHog
- Sanity → Google AdsConnecting Sanity to Google Ads
- Sanity → MetaConnecting Sanity to Meta Conversions API
- Sanity → GA4Connecting Sanity to GA4
RELATED GUIDES
Where Sanity → PostHog fits in the rest of your tracking
- Webflow → PostHogConnecting Webflow to PostHog
- Drupal Commerce → PostHogConnecting Drupal Commerce to PostHog
- Concrete CMS → PostHogConnecting Concrete CMS to PostHog
- Sanity → MixpanelConnecting Sanity to Mixpanel
- Sanity → MatomoConnecting Sanity to Matomo
- Local servicesConversion tracking for local businesses, where most conversions bypass the website
- Sitecore → PostHogConnecting Sitecore to PostHog
- Contentful → PostHogConnecting Contentful to PostHog
Free · 30 min
Get Sanity and PostHog joined properly
Free30 minutesNo obligation
