ADR 0006 — Analytics Is a Separate Sink Port
Status: Accepted (★ new port; no sibling equivalent)
Context
Section titled “Context”Click volume is orders of magnitude above control data, and its query shape (time series, top-N by dimension, filters) is OLAP, not KV. Dub uses Tinybird, Sink uses Workers Analytics Engine, Shlink uses its SQL DB. An embeddable library must work on day one with the host’s existing DB and scale out without a rewrite.
Decision
Section titled “Decision”-
A separate port,
AnalyticsSink:interface AnalyticsSink {ingest(events: AnalyticsEvent[]): Promise<void>; // clicks, leads, salesquery(query: AnalyticsQuery): Promise<AnalyticsResult>;events?(query: EventsQuery): Promise<AnalyticsEvent[]>; // recent raw events (optional)close?(): Promise<void>;}AnalyticsQuery={ tenant, linkIds?, domain?, from, to, interval?: "hour" | "day", groupBy?: Dimension, filters?: Partial<Record<Dimension, string[]>>, limit?, metrics? }→{ totals, timeseries?, breakdown? }. Dimensions: link, domain, country, continent, city, device, os, browser, referrer, utm_source/medium/campaign, qr, variant. -
Default:
createKvAnalytics({ storage })— time-bucket rollups in the same KV storage: hourly buckets (kepthourlyRetention, default 14 days) and daily buckets (forever, ordailyRetention), per tenant and per link, each carrying metric counters and per-dimension counters. Ingest aggregates a batch in memory first, then does one read-modify-write per touched bucket (CAS-retried when supported). A bounded recent-events log feeds the “recent clicks” view. Limitation (documented): breakdown under a filter on a different dimension is not available from rollups (a link filter is); the sink reportscapabilities.crossFilter = falseand the UI hides those controls. -
Adapters for scale, all
fetch-based (no peer deps):analytics/clickhouse(HTTP interface,JSONEachRowinserts, generated SQL with parameter binding),analytics/tinybird(Events API + SQL API),analytics/analytics-engine(Workers bindingwriteDataPoint+ the SQL API).analytics/memoryfor tests. Dual-writing during a migration is a few lines of user code (a sink whoseingestcalls both); see docs/ANALYTICS.md. -
A shared sink conformance suite (
test/analytics-contract.ts) runs against memory, KV (on memory storage), and ClickHouse whenCLICKHOUSE_URLis set (CI service container).
Consequences
Section titled “Consequences”- Day one: zero extra infrastructure. At scale: swap the sink, keep links in Postgres/Redis.
- Unique visitors are only exact in OLAP sinks; KV rollups do not count uniques (documented).