7.6 KiB
7.6 KiB
@suaveplan/delivery-tracking - Channel-agnostic delivery event tracking for campaigns and messages: lifecycle dedupe, in-memory + SurrealDB stores, time-series aggregation, tracking pixel / link tokens, multi-provider webhook parsing, OTel instrumentation
Channel-agnostic delivery event tracking for campaigns and messages: lifecycle dedupe, in-memory + SurrealDB stores, time-series aggregation, tracking pixel and click-link tokens, multi-provider webhook parsing, and OpenTelemetry instrumentation.
Documentation tree
- aggregator —
DeliveryAggregatorreads campaign and contact statistics from anyDeliveryEventStoreand exposes time-series bucket…- aggregator —
DeliveryAggregatorreads campaign and contact statistics from anyDeliveryEventStoreand exposes time-series bucket…
- aggregator —
- tracker —
DeliveryTrackeris the package's primary write-side surface. It validates inboundCreateDeliveryEventInputs against…- tracker —
DeliveryTrackeris the package's primary write-side surface. It validates inboundCreateDeliveryEventInputs against… - tracker.schemas —
tracker.schemasdeclares the Zod boundary schemas applied toDeliveryTracker.trackinput. Centralising the schemas…
- tracker —
- tracking-link —
TrackingLinkWrapperrewrites outbound URLs with click-tracking redirects. Each wrapped URL points at the tracking hos…- tracking-link —
TrackingLinkWrapperrewrites outbound URLs with click-tracking redirects. Each wrapped URL points at the tracking hos…
- tracking-link —
- tracking-pixel —
TrackingPixelGeneratorproduces and parses the open-tracking pixel URLs embedded in outbound HTML emails. The pixel U…- token-codec —
token-codecis the shared XOR + base64url encoder/decoder used by bothTrackingPixelGenerator(open tracking) and `… - tracking-pixel —
TrackingPixelGeneratorproduces and parses the open-tracking pixel URLs embedded in outbound HTML emails. The pixel U…
- token-codec —
- webhook-parser —
WebhookEventParsernormalises provider-specific delivery webhooks intoCreateDeliveryEventInput[]ready for `Delive…- webhook-parser —
WebhookEventParsernormalises provider-specific delivery webhooks intoCreateDeliveryEventInput[]ready for `Delive…
- webhook-parser —
- errors —
DeliveryTrackingErroris the stable typed error class raised by every code path inside@suaveplan/delivery-tracking…- delivery-tracking-error —
DeliveryTrackingErroris the stable typed error class raised by every code path inside@suaveplan/delivery-tracking…
- delivery-tracking-error —
- otel —
getDeliveryTrackingInstrumentslazily constructs and caches the OTelCounterset used by every production code path…- instruments —
getDeliveryTrackingInstrumentslazily constructs and caches the OTelCounterset used by every production code path…
- instruments —
- memory —
memory-storedefines theDeliveryEventStoreinterface that every persistence backend must satisfy, ships the determ…- memory-store —
memory-storedefines theDeliveryEventStoreinterface that every persistence backend must satisfy, ships the determ…
- memory-store —
- surreal —
SurrealDeliveryEventStoreis the SurrealDB-backed implementation of theDeliveryEventStorecontract. It uses positi…- surreal-store —
SurrealDeliveryEventStoreis the SurrealDB-backed implementation of theDeliveryEventStorecontract. It uses positi…
- surreal-store —
Install
bun add @suaveplan/delivery-tracking
Quick start
import {
DeliveryAggregator,
DeliveryTracker,
InMemoryDeliveryEventStore,
TrackingPixelGenerator,
WebhookEventParser,
} from "@suaveplan/delivery-tracking";
const store = new InMemoryDeliveryEventStore();
const tracker = new DeliveryTracker(store);
const aggregator = new DeliveryAggregator(store);
const parser = new WebhookEventParser();
const pixel = new TrackingPixelGenerator({
baseUrl: "https://t.example.com",
secret: "tracking-secret",
});
tracker.bus.on("delivery:opened", (e) => {
console.log("opened", e.messageId);
});
const events = parser.parse("sendgrid", { body: webhookBody });
await tracker.trackBatch(events);
const stats = await aggregator.getCampaignStats("c1");
const pixelUrl = pixel.generatePixelUrl("m1", "u1", "c1");
Modules
| Module | Description |
|---|---|
src/core/tracker/tracker.ts |
Validates and records events, dedupes lifecycle types, emits on a typed bus. |
src/core/tracker/tracker.schemas.ts |
Zod boundary schemas for CreateDeliveryEventInput. |
src/core/aggregator/aggregator.ts |
Campaign rollups, contact histories, time-series bucketing. |
src/core/tracking-pixel/tracking-pixel.ts |
Open-tracking pixel URL generator + parser. |
src/core/tracking-pixel/token-codec.ts |
Shared XOR + base64url codec used by pixel and link wrappers. |
src/core/tracking-link/tracking-link.ts |
Click-tracking URL wrapper, HTML rewrite, token parse. |
src/core/webhook-parser/webhook-parser.ts |
Pluggable provider webhook → input normaliser. |
src/stores/memory/memory-store.ts |
DeliveryEventStore contract + in-memory implementation + computeStats. |
src/stores/surreal/surreal-store.ts |
SurrealDB-backed implementation using positional SurQL. |
src/errors/delivery-tracking-error.ts |
DeliveryTrackingError + stable code registry. |
src/otel/instruments.ts |
Lazy OTel counter set (tracked, deduped, webhook_parsed, token_issued, token_rejected). |
Features
- Lifecycle dedupe per
messageIdforopened,delivered,bounced,unsubscribed,complained. - Typed
EventBus<DeliveryTrackerEventMap>with one topic perDeliveryEventType. - Backend-agnostic via
DeliveryEventStore— drop-inInMemoryDeliveryEventStore,SurrealDeliveryEventStore, or your own. - Time-series bucketing by
hour/day/week. - Pluggable webhook parsers for SendGrid, Mailgun, AWS SES, and a generic shape.
- Zod boundary validation on every event the tracker accepts.
- Counters emitted via
@opentelemetry/apifor ingest, dedupe, parser activity, and token issuance/rejection.
Subpath exports
./tracker, ./aggregator, ./tracking-pixel, ./tracking-link, ./webhook-parser, ./stores/memory, ./stores/surreal, ./errors, ./otel.
Errors
Every failure raised by the package extends DeliveryTrackingError. The stable code registry:
DELIVERY_TRACKING.INVALID_INPUTDELIVERY_TRACKING.INVALID_TOKENDELIVERY_TRACKING.UNSUPPORTED_PROVIDERDELIVERY_TRACKING.STORE_FAILUREDELIVERY_TRACKING.QUERY_FAILURE