Analytics
Artifactkit counts one thing about readers: a page was served. That is artifact.viewed, emitted by
the application layer on both serve paths, and it is deliberately thin. This page is the contract
behind it, how to wire PostHog, and how to prove a capture actually arrived.
If you only want the checklist: POSTHOG_API_KEY + POSTHOG_HOST, point runtime.events at
telemetryEventSink(posthogTelemetry(...)), deploy, open a share link, look in PostHog's Live events.
The event contract
| event | when | properties |
|---|---|---|
artifact_viewed | a page was served on GET /s/:token or GET /v/:id?t= | artifact_id, version, channel, $lib: "artifactkit" |
artifact_published | a new artifact | artifact_id, version |
artifact_updated | a version was added | artifact_id, version |
artifact_shared / artifact_unshared | the public link changed | artifact_id |
artifact_state.changed | a state key was written | artifact_id, bytes |
comment.thread.created / comment.thread.resolved / comment.created | the comment surface | artifact_id |
artifact_viewed in detail:
artifact_id— the artifact that was served.version— the version actually rendered, which is the current one unless the staff URL named?v=N. A view of an old version is a view of that version, not of "the artifact".channel—"share"for a public/s/:tokenlink,"staff"for an authenticated/v/:id. This is the dimension that makes share traffic separable from internal traffic; without it a lead-magnet launch looks like your own team refreshing the page.$lib—"artifactkit", added by the PostHog adapter, so captures from this library are distinguishable from anything else in the same project.distinct_id— views have no actor, so they are attributed toviewer:shareorviewer:staff. Those are placeholders, not people: count events, and break down byartifact_idandchannel. Do not read "unique users" on these events as anything but two rows.
One event per served page. Not per asset, not per request: HEAD is answered from the same handler
and is explicitly not counted, and a conditional 304 (whenever the handler grows one) must return
before the record hook. A revoked share link, an expired view token and a missing artifact are all
errors and none of them produce an event — the count only moves when a body was handed to a reader.
What is never sent, and why
Never in the event, and not reachable from the layer that builds it:
- The page. Not the HTML, not its size as a page, not the title, not the description. An artifact can contain a customer's data and a title is free text a person wrote; telemetry is not a place either belongs.
- The reader. No IP address, no user agent, no referrer, no cookie, no email, no query string,
no
cf-connecting-ip. The application layer is handed an artifact id, a version and a channel — that is all it has to give. Counting opens is useful; identifying who opened a document someone sent them privately is surveillance, and it is not a feature of this library. - State values and comment bodies.
artifact_state.changedcarries a key and a byte count;comment.*carries ids. The value is fetched by the consumer, never shipped in the event.
A note on PostHog specifically: the capture request is made by your server or Worker, not by the reader's browser, so the IP PostHog sees is your egress address. PostHog may still attach a location from it — that location is your datacentre, not your reader. If you run your own collector, the same rule applies: the only identity in the pipeline is whatever you add, and the adapter adds none.
Wiring PostHog
The adapter is posthogTelemetry from adapters/telemetry; the bridge is telemetryEventSink. Point
the runtime's event sink at the bridge and every mutation and every view is captured — the domain does
not know PostHog exists.
import { actorFromEvent, posthogTelemetry, telemetryEventSink } from './src/adapters/telemetry/index.ts';
const deps: Deps = {
store, bodies, comments,
runtime: {
now: () => new Date(),
ids,
signer,
events: telemetryEventSink(posthogTelemetry({ apiKey: env.POSTHOG_API_KEY, host: env.POSTHOG_HOST }), {
actor: actorFromEvent,
}),
},
};
If you also keep a durable event log, put the bridge downstream of it instead
(createEventService({ log, clock, downstream: bridge })) so the log is the record and PostHog is a
side channel whose failure cannot fail a publish.
examples/lead-magnet-worker.ts is the full worked example: a Cloudflare Worker with a KV store, an
admin route that publishes a landing page and mints its share link, and captures handed to
ctx.waitUntil so a slow PostHog never delays the page.
Environment variables
| name | where | meaning |
|---|---|---|
POSTHOG_API_KEY | secret (wrangler secret put POSTHOG_API_KEY -c wrangler.lead-magnet.jsonc) | PostHog project key (phc_…). Absent = pages served, nothing counted. |
POSTHOG_HOST | var | https://us.i.posthog.com (default) or https://eu.i.posthog.com. The adapter posts to ${host}/i/v0/e/. |
VIEW_SECRET | secret | ≥16 characters; signs staff view tokens. Required to serve anything. |
ADMIN_TOKEN | secret | bearer token for POST /api/artifacts in the exemplar. Absent disables publishing. |
ARTIFACTS_HOST | var | hostname the worker answers on and builds share URLs from. Empty disables the host check (local dev). |
Verifying a capture end to end
1. The endpoint itself
PostHog's capture endpoint takes a single event as JSON. Send exactly what the adapter sends:
curl -sS -X POST https://us.i.posthog.com/i/v0/e/ \
-H 'content-type: application/json' \
-d '{
"api_key": "phc_YOUR_PROJECT_KEY",
"event": "artifact_viewed",
"distinct_id": "viewer:share",
"properties": {
"artifact_id": "art_0123456789abcdef",
"version": 2,
"channel": "share",
"$lib": "artifactkit"
},
"timestamp": "2026-12-01T12:00:00.000Z"
}'
A working key answers 200 with {"status": 1}. A wrong key answers 401 and the adapter raises
posthog capture failed: 401 — that is the error you will see in a Worker log if the secret is
missing or belongs to another project.
2. The deployment
npx wrangler deploy -c wrangler.lead-magnet.jsonc
curl -sS -X POST https://go.example.com/api/artifacts \
-H "authorization: Bearer $ADMIN_TOKEN" -H 'content-type: application/json' \
-d '{"title":"The checklist","html":"<!doctype html><h1>Hi</h1>"}'
# → { "id": "art_…", "version": 1, "shareUrl": "https://go.example.com/s/<token>" }
curl -sS https://go.example.com/s/<token> > /dev/null # one view
curl -sSI https://go.example.com/s/<token> > /dev/null # HEAD: no view
GET /health reports {"telemetry":"posthog"} when a key is configured, "off" when it is not —
that catches the "deployed without the secret" case before you wonder why nothing arrives.
3. What to look for in PostHog
- Activity → Live events, filtered to
artifact_viewed: one row per page open, one row per refresh, none for theHEADrequest. Open the row and checkproperties:artifact_id,version,channel,$lib: "artifactkit", and nothing else beyond PostHog's own$-prefixed metadata. - Trends, event
artifact_viewed, broken down bychannel— that is share traffic versus staff traffic. Break down byartifact_idto see which landing page is being read. - If nothing arrives: check
/health, check the Worker log fortelemetry capture failed, and check the host (eu.keys do not work againstus.i.posthog.com).
PostHog ingests with a small delay and deduplicates nothing, so a test view may take a few seconds to appear in Live events and a refresh will show two rows. That is the correct behaviour, not a bug.