Skip to main content
Craft → PostHogIntegration guide

Connecting Craft CMS to PostHog

Craft is a developer-oriented CMS, usually built bespoke by an agency, which means the implementation is whatever that agency decided and nothing is standard. 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 Craft Commerce order 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

Craft CMS to PostHog: identify on login, purchase on the server

Identify Craft users from an uncached request or the login and checkout responses, using the Craft user UID as distinct_id, never a Twig variable on cached pages. Store PostHog's anonymous distinct_id on the cart, then capture purchases server-side from the order-complete listener with an event UUID derived from the order reference.

PostHog on a Craft site usually starts with the JavaScript snippet in the layout template, which captures pageviews fine under static caching. The gap is identity: a statically cached page cannot print the logged-in user's ID into the snippet, so a template that calls identify with a Twig variable will either be skipped by the cache or, worse, cache one user's ID and identify every visitor as that person.

Identify from a small uncached request instead, or on the login and checkout responses, using the Craft user UID as the distinct_id. Store PostHog's anonymous distinct_id on the cart in a custom field during checkout, then capture the purchase server-side from the order-complete listener with that ID, the total, and an event UUID derived from the order reference so a retried job does not record the order twice. Guest orders stay tied to the anonymous visitor who browsed.

Self-hosting PostHog next to a Craft site is common on privacy-sensitive builds, which puts ingestion on your own infrastructure. A busy sale or launch spikes Craft and PostHog at the same moment. Check ingestion lag and disk after a launch before concluding that a checkout step is losing users.

Craft → PostHog · what breaks

Why this join usually fails — Craft → PostHog

  1. 01

    The distinct_id never reaches Craft

    Craft's template caching and static caching plugins both serve stored output, and because every build is custom, where the capture code lives differs per site. There is no standard place to look.

  2. 02

    Every install is bespoke

    There is no standard Craft setup, so an approach that worked on another Craft site may have nothing to attach to on this one. Read the build before quoting.

  3. 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.

  4. 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.

Craft → PostHog · the fix

What working looks like — Craft → PostHog

  • The distinct_id on every order

  • Conversions arriving in PostHog

    The event lands on the person profile and becomes available in insights, funnels and session replay.

  • Reconciled against the source

    Craft CMS's own records are the reference, not either dashboard.

Craft → PostHog · architecture

How Craft and PostHog actually connect

  1. 1

    Craft CMS holds the truth

    Craft installs are bespoke by nature. Establish how this particular build models a completed order before assuming anything transfers from another Craft site.

  2. 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. 3

    The identifier is the bridge

    The distinct_id, captured at the visit and carried into the Craft Commerce order.

  4. 4

    The event source decides reliability

    Fire from the event listener in PHP rather than a Twig template, which may be cached.

  5. 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.

Craft → PostHog · normal or broken

Working vs. broken, side by side — Craft → PostHog

Normal, not a fault

  • The distinct_id present on the order

    Visible on the record itself in Craft CMS, not merely captured somewhere on the site.

  • Sent from the Craft Commerce order-complete event

  • 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

    PostHog's count and Craft CMS's records agree within a few percent over a closed window.

Actually broken

  • The distinct_id missing on most records

    Craft's template caching and static caching plugins both serve stored output, and because every build is custom, where the capture code lives differs per site. There is no standard place to look.

  • Sent from the browser

    There is no standard Craft setup, so an approach that worked on another Craft site may have nothing to attach to on this one. Read the build before quoting.

  • 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

    The dashboard shows a number and no one has compared it to Craft CMS.

Craft → PostHog · diagnose

Diagnostic order — Craft → PostHog

  1. 01

    Count orders carrying the distinct_id

  2. 02

    Follow one real conversion end to end

  3. 03

    Read how this specific build handles order completion

    Assumptions carried from another Craft project are the main source of wasted time here.

  4. 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.

  5. 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.

Craft → PostHog · prove

How it gets proven — Craft → PostHog

Against Craft CMS's own records, not against either platform's dashboard.

Source of truth

Craft Commerce's order records in your own database, filtered to completed.

  • The distinct_id confirmed present on a live order.
  • Purchase events reconciled per transaction against your billing system, with ingestion lag checked over the same window.
  • Order custom field confirmed populated on an order placed as an anonymous visitor through cache.

Craft → PostHog · free audit

What we check in the free audit

  • Count orders carrying the distinct_id
  • Follow one real conversion end to end
  • Read how this specific build handles order completion
  • Check ingestion health and disk before debugging any missing event
  • Read the match rate, not the success message

Checked against

Craft Commerce's order records in your own database, filtered to completed.

Get my free conversion tracking audit

Free30 minutesNo obligation

Craft → PostHog · when not to buy

When you don't need this — Craft → PostHog

  • If PostHog already reports conversions that reconcile against Craft CMS's records within a few percent, this join is working and there is nothing to buy.
  • Measure identifier coverage yourself first — pull your last hundred orders and count how many carry the distinct_id.
  • If the original agency is still engaged, the fastest path is usually specifying the field and having them add the hook.
  • 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.

Craft → PostHog · what we deliver

What gets built — Craft → PostHog

  • Identifier capture into Craft

    Capture the identifier and store it as a custom field on the order through a Craft event listener, server-side.

  • Event source: the Craft Commerce order-complete event

    Fire from the event listener in PHP rather than a Twig template, which may be cached.

  • 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

    Refunds and cancelled orders are recorded against the order.

Written guarantee

95%+ tracking accuracy

Measured against Craft Commerce's order records in your own database, filtered to completed.. Miss it and we keep working at no cost.

Get my free conversion tracking audit

From $400 · See packages

FAQ

Craft → PostHog questions, answered

Do not print the user ID in a cached template. Call identify from an uncached request or the login response, with the Craft user UID as the distinct_id.

Free · 30 min

Get Craft and PostHog joined properly

Free30 minutesNo obligation