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

eventwhenproperties
artifact_vieweda page was served on GET /s/:token or GET /v/:id?t=artifact_id, version, channel, $lib: "artifactkit"
artifact_publisheda new artifactartifact_id, version
artifact_updateda version was addedartifact_id, version
artifact_shared / artifact_unsharedthe public link changedartifact_id
artifact_state.changeda state key was writtenartifact_id, bytes
comment.thread.created / comment.thread.resolved / comment.createdthe comment surfaceartifact_id

artifact_viewed in detail:

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:

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

namewheremeaning
POSTHOG_API_KEYsecret (wrangler secret put POSTHOG_API_KEY -c wrangler.lead-magnet.jsonc)PostHog project key (phc_…). Absent = pages served, nothing counted.
POSTHOG_HOSTvarhttps://us.i.posthog.com (default) or https://eu.i.posthog.com. The adapter posts to ${host}/i/v0/e/.
VIEW_SECRETsecret≥16 characters; signs staff view tokens. Required to serve anything.
ADMIN_TOKENsecretbearer token for POST /api/artifacts in the exemplar. Absent disables publishing.
ARTIFACTS_HOSTvarhostname 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

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.