Skip to Content
Client Lib

Analytics

Batched client event tracking that is safe to call and honours opt-out.

Usage

import { beaconTransport, createAnalytics } from "@dapplelight/signet-lib/analytics"
import { api } from "./api"
import { user } from "./session"

export const analytics = createAnalytics({
  transport: beaconTransport("/api/analytics"),
  requireConsent: true,          // track nothing until optIn()
  traitKeys: ["plan"],           // the only traits identify() may send
})

analytics.optIn()
analytics.pageview()
analytics.track("invoice_paid", { invoiceId: "INV-1042", amount: 8000 })
analytics.identify(user.id, { plan: "team" })

await api.post("/invoices/INV-1042/pay", {}, { headers: analytics.headers() })

With no transport, every call is a no-op, so you can instrument first and wire it later.

API

ExportWhat it does
createAnalytics(config?)Client with track, pageview, identify, reset, flush, headers, optIn, optOut
noopAnalyticsA client that does nothing
beaconTransport(url)POSTs { events } with sendBeacon, falling back to fetch with keepalive
continueSession(prev, now, timeoutMs?)Pure rule: continue a visit or start a new one
ANALYTICS_HEADERSHeader names headers() sets: anonymousId, sessionId
EventProps, AnalyticsEvent, SessionState, AnalyticsContext, Transport, AnalyticsConfig, AnalyticsTypes

createAnalytics(config)

OptionTypeDefault
transport(events) => void | Promise<void>noneOmit for a no-op client
batchSizenumber10Flush after this many events
flushIntervalMsnumber5000
respectDoNotTrackbooleantrueHonour navigator.doNotTrack
optOutKeystring"signet-analytics-optout"localStorage key for the choice
requireConsentbooleanfalseTrack nothing until optIn()
sessionTimeoutMsnumber1800000A visit ends after this idle time
traitKeysstring[][]Trait keys identify() may send
storageKeystring"signet-analytics"Prefix for the stored ids

Client members

MemberWhat it does
track(name, props?)Queue an event
pageview(path?)track("pageview", ...) with path and referrer
identify(id, traits?)Link events to a user; sends $identify
reset()On sign-out: flush, forget user, new anonymous id
flush()Send the queue now. Also runs when the page is hidden
headers()Headers so server events join this visit. Empty while suppressed
context{ anonymousId, sessionId, userId? }
optOut() / optIn()Record the choice; opt-out also clears the stored ids
isOptedOut, pendingRead-only state