Tie Polar.sh orders and refunds straight back to the content that converted.
Attach window.trimstats.attribution() to @polar-sh/sdk checkout metadata and verify order.paid and order.refunded Standard Webhooks server-side in ClickHouse.


How does TrimStats attribute Polar.sh orders, renewals, and refunds?
TrimStats connects natively to Polar.sh via Standard Webhooks for order.paid and order.refunded events. Pass window.trimstats.attribution() into polar.checkouts.create() metadata.trimstats, and the Fastify collector verifies webhook-id, webhook-timestamp, and webhook-signature headers with timing-safe comparison before joining revenue across 90-day ClickHouse journeys.
Step-by-step setup for Polar.sh
Copy-paste implementation verified against the live TrimStats tracker and collector APIs.
- 1
Capture window.trimstats.attribution() in your checkout trigger
Before posting to your Polar checkout API route, read the flat attribution snapshot from window.trimstats?.attribution?.() so referrer, landing page, UTM parameters, and visitor ID reach your server.
components/PolarBuyButton.tsxts1const attribution = window.trimstats?.attribution?.() ?? {};23const res = await fetch('/api/polar-checkout', {4 method: 'POST',5 headers: { 'content-type': 'application/json' },6 body: JSON.stringify({ productId: 'prod_growth_monthly', attribution }),7});8const { url } = await res.json();9window.location.assign(url); - 2
Attach metadata.trimstats in @polar-sh/sdk checkouts.create()
Forward the serialized attribution object (plus your authenticated database userId as uid) into metadata.trimstats when creating the Polar checkout session.
app/api/polar-checkout/route.tsts1import { Polar } from '@polar-sh/sdk';23const polar = new Polar({ accessToken: process.env.POLAR_ACCESS_TOKEN! });45export async function POST(req: Request) {6 const { productId, userId, attribution } = await req.json();78 const checkout = await polar.checkouts.create({9 products: [productId],10 metadata: {11 trimstats: JSON.stringify({12 ...(attribution ?? {}),13 uid: userId ?? attribution?.uid ?? '',14 }),15 },16 successUrl: 'https://acme.dev/dashboard?upgraded=1',17 });1819 return Response.json({ url: checkout.url });20} - 3
Register your Polar webhook endpoint for order.paid and order.refunded
In Polar organization settings under Webhooks -> Add Endpoint, enter https://collect.trimstats.com/v1/webhooks/polar/{siteId} and subscribe to order.paid and order.refunded.
- 4
Save your polar_whs_ signing secret in TrimStats Site settings
Paste your Polar signing secret (polar_whs_... or whsec_...) under Site settings -> Revenue Attribution Webhooks (provider: Polar). TrimStats validates v1 Standard Webhooks signatures with a 5-minute replay tolerance.
High-signal custom events to track for Polar.sh
Fire any event with window.trimstats?.('event', name, props) and attach it to conversion goals or multi-step ClickHouse funnels.
| Event name | When to fire | Sample properties |
|---|---|---|
| pricing_viewed | Developer or founder lands on your pricing comparison page. | {"plan_highlighted":"growth"} |
| polar_checkout_started | User clicks Subscribe and redirects to a Polar.sh hosted checkout. | {"product":"growth_monthly"} |
| github_repo_clicked | Visitor clicks through to your GitHub repository or open-source README. | {"location":"navbar"} |
| docs_quickstart_copied | Developer copies an SDK or CLI installation block from docs. | {"sdk":"polar"} |
| customer_portal_opened | Paying subscriber opens the Polar Customer Portal to change plans. | {"source":"billing_page"} |


TrimStats ClickHouse journey windows supporting Polar.sh SaaS billing cycles
Verified lookback duration in days across TrimStats reporting engines
Joining Polar order webhooks across a 90-day ClickHouse lookback captures long developer evaluation cycles from first blog read to paid subscription.
Methodology: Exact production lookback parameters read from packages/shared/src/schemas.ts and apps/web/lib/content/facts.ts.
View chart data table
| Label | Lookback window |
|---|---|
| Cookie-less daily salt | 1 day |
| Multi-step funnel max window | 30 days |
| Polar webhook ClickHouse join | 90 days |
| Visitor timeline max history | 730 days |
Polar.sh analytics FAQ
Implementation details, privacy trade-offs, and event-quota answers.
How does TrimStats verify Polar.sh webhook signatures?
Which Polar webhook events should I subscribe to?
Can I pass flat Polar metadata keys instead of a JSON trimstats string?
Does TrimStats itself run on Polar.sh billing?
Are Polar webhook retries deduplicated automatically?
Does Polar.sh attribution work in cookie-less mode or with multi-day SaaS journeys?
Co-founder of DotCamp LLC in Brooklyn, NY and lead engineer of TrimStats. Ships privacy-first web analytics, ClickHouse pipelines, and WordPress plugins used on 115,000+ active sites.
- • Architect of the TrimStats 1.96 KB gzipped browser tracker, Fastify collector, and ClickHouse MergeTree pipeline
- • Co-founder of DotCamp LLC (est. 2019), creator of Ultimate Blocks (50,000+ installs), WP Table Builder (50,000+ installs), Tableberg, and OptimizeCamp
- • WordPress.org plugin & theme developer since 2016 with 1,200+ community support replies and 5M+ cumulative plugin downloads
Verification standard: Code snippets, custom event payloads, and webhook contracts for Polar.sh are verified against the live TrimStats tracker (1.96 KB gzipped) and collector schema as of 2026-11-30.
Related documentation and guides
Instrument Polar.sh in under 60 seconds
Start on Starter ($17/month for 100,000 monthly events), with cookie-less privacy defaults and full access to revenue webhooks, funnels, cohorts, and UI friction.