Skip to main content

PostHog SDK events and recordings to your own storage

PostHog SDKs send the events they capture to their host, api_host in the browser. Set it to Send2Bucket and the SDK keeps working as it does, while every event is stored as sent in your stream's destination. PostHog's product is not involved: there are no insights, person profiles, feature flags or surveys, only the raw events. Use it to keep product analytics at storage prices, or to query events next to your other data. Stored events are event lines, so aggregation rules see them like any other event.

How it works​

posthog-js, mobile SDKs ──capture──────────┐
posthog-python, -node ──capture or v1────┼─▶ /v1/events/posthog ─▶ your stream's destination
──flags, config────┘ (answered with nothing enabled)
  • Each event becomes one event line, as the SDK sent it, without its project token. Delivery format has the shape.
  • Both of PostHog's capture formats work: the one posthog-js and older SDKs send, and capture v1, which current server SDKs send.
  • Flags and remote config answer with nothing enabled, so the SDK captures events and nothing else unless you turn on recordings.
  • Replies follow PostHog's. A 200 means the events are stored durably and will be delivered at least once.

1. Create a stream and a key​

Create an ingestion stream in the dashboard, add a destination, and copy the stream's API key and ID. The key is the SDK's project token, which ships inside web and mobile apps, so use a key that writes only to this stream. A key for several streams names the stream in the host: https://api.send2bucket.com/v1/events/posthog/<stream_id>.

2. Set the host​

In the browser:

posthog.init('<key>', {
api_host: 'https://api.send2bucket.com/v1/events/posthog',
});

In Python and Node.js:

from posthog import Posthog

posthog = Posthog("<key>", host="https://api.send2bucket.com/v1/events/posthog")
const client = new PostHog('<key>', { host: 'https://api.send2bucket.com/v1/events/posthog' });

Other SDKs take the same host in their own setting. PostHog's Ruby library drops the host's path, so it cannot reach this endpoint.

Session recordings​

Recordings are off unless the host ends in /recordings, which makes remote config turn them on:

posthog.init('<key>', {
api_host: 'https://api.send2bucket.com/v1/events/posthog/recordings',
asset_host: 'https://<your_site>',
});

posthog-js loads its recorder script from asset_host, at <asset_host>/static/lazy-recorder.js, and Send2Bucket does not serve it. Publish the files of the posthog-js package's dist folder under /static/ on a site of yours and set asset_host to it. Recordings arrive as $snapshot events: they are much larger than other events, and their size is billed.

3. Check the endpoint​

Capture one event. The answer is {"status":1}:

curl -i https://api.send2bucket.com/v1/events/posthog/i/v0/e/ \
-H "Content-Type: application/json" \
--data "{\"api_key\":\"$S2B_API_KEY\",\"event\":\"hello from curl\",\"distinct_id\":\"check\"}"

A 401 means the key is wrong or revoked. The API reference lists the other errors.

4. Query the events​

DuckDB reads the objects in place:

INSTALL httpfs; LOAD httpfs;
CREATE SECRET s2b (TYPE s3, PROVIDER credential_chain, REGION '<region>');

CREATE VIEW posthog AS
SELECT * FROM read_json(
's3://<bucket>/<prefix>/s2b/stream=*/date=*/hour=*/*.ndjson.gz',
format = 'newline_delimited',
hive_partitioning = true,
columns = {v: 'INTEGER', delivery_batch_id: 'VARCHAR', batch_id: 'VARCHAR',
received_at: 'TIMESTAMPTZ', signal: 'VARCHAR', data: 'JSON'})
WHERE stream = '<stream_id>';

Events by name:

SELECT data->>'$.event' AS event, count(*) AS events
FROM posthog
GROUP BY ALL
ORDER BY events DESC;

Daily users. Server SDKs send distinct_id at the top of the event and posthog-js inside properties, so this reads both:

SELECT CAST(TRY_CAST(data->>'$.timestamp' AS TIMESTAMPTZ) AS DATE) AS day,
count(DISTINCT coalesce(data->>'$.distinct_id', data->>'$.properties.distinct_id')) AS users
FROM posthog
WHERE data->>'$.event' <> '$snapshot'
GROUP BY ALL
ORDER BY day;

Page views by path:

SELECT data->>'$.properties.$pathname' AS path, count(*) AS views
FROM posthog
WHERE data->>'$.event' = '$pageview'
GROUP BY ALL
ORDER BY views DESC;

Recorded sessions, with the number of snapshots each holds:

SELECT data->>'$.properties.$session_id' AS session,
count(*) AS batches,
sum(json_array_length(data->'$.properties.$snapshot_data')) AS snapshots
FROM posthog
WHERE data->>'$.event' = '$snapshot'
GROUP BY ALL;

In an Iceberg or DuckLake table, the same line is in the payload column; see Table columns.

Notes​

  • Not supported: feature flags, experiments, surveys and heatmaps, which need PostHog's server. The flags endpoint answers with no flags, so code behind a flag takes its default path.
  • Compression: gzip and base64 bodies work. The lz64 compression of old posthog-js versions gets a 415; current versions use gzip.
  • Quota: when the stream's allowance is spent, capture answers 200 with quota_limited, as PostHog does, and posthog-js stops sending for a minute at a time. Capture v1 answers 402, which v1 SDKs do not retry.
  • Billing: each event's JSON as stored is the number billed.
  • Retries: each event carries a uuid; to drop repeats, key on it in your query. See Retries.
  • Segment and RudderStack: to keep events a CDP already collects, see Segment and RudderStack.