API
Public functions and configuration types of @casoon/trackr. The type declarations shipped in the package are the authoritative source.
Client
initsince 0.1import { init } from "@casoon/trackr/client";
function init(options: TrackrConfig): void;
interface TrackrConfig {
endpoint: string;
debug?: boolean;
}Sends a pageview and, from 0.2, a pageview after every pushState, replaceState,
popstate and hashchange. Call it once per page load.
Parameters
endpointstring- URL the events are posted to, usually a route on the same site.
debugboolean- Log every sent event to the console.
tracksince 0.1import { track } from "@casoon/trackr/client";
function track(name: string, props?: Record<string, string | number | boolean>): void;Sends a custom event for the current path. Before init() it only logs a warning.
Server
createHandlersince 0.1import { createHandler } from "@casoon/trackr/server";
function createHandler(config: HandlerConfig): (request: Request) => Promise<Response>;
interface HandlerConfig {
storage: StorageAdapter;
privacy?: PrivacyConfig;
botFilter?: boolean;
}
interface PrivacyConfig {
anonymizeIp?: boolean; // default true
stripPii?: boolean; // default true
stripQueryParams?: string[];
}Accepts POST with a JSON event. Status codes are listed in the
Quickstart.
Parameters
storageStorageAdapter- Receives every accepted event.
privacyPrivacyConfig- Merged with the defaults; see the Privacy guide.
botFilterboolean- Drop requests that look like bots. Off unless set.
createPixelHandlersince 0.2import { createPixelHandler } from "@casoon/trackr/server/pixel";
function createPixelHandler(config: HandlerConfig): (request: Request) => Promise<Response>;Records a pageview from the url and ref query parameters and returns a transparent GIF.
See Tracking pixel.
@casoon/trackr/server also exports the building blocks the handler uses: isBot(request),
anonymizeIp(ip), stripPii(url), sanitizeProps(props), applyPrivacy(event, config) and
resolvePrivacyConfig(config).
Storage
postgressince 0.1postgres(connectionStringOrClient: string | { query(text: string, values?: unknown[]): Promise<{ rows: unknown[] }> }): StorageAdapterapisince 0.1api(config: { url: string; headers?: Record<string, string>; transform?: (event: TrackrEvent) => unknown }): StorageAdapterwebhooksince 0.2webhook(config: WebhookConfig): StorageAdapter
interface WebhookConfig {
url: string;
headers?: Record<string, string>;
secret?: string;
transform?: (event: TrackrEvent) => unknown;
retry?: { attempts?: number; baseDelay?: number };
}ga4since 0.2ga4(config: Ga4Config): StorageAdapter
interface Ga4Config {
measurementId: string;
apiSecret: string;
debug?: boolean; // default false
nonPersonalizedAds?: boolean; // default true
stripQueryParams?: string[];
}multisince 0.2multi(...adapters: StorageAdapter[]): StorageAdapterbatchsince 0.2batch(adapter: StorageAdapter, options?: BatchOptions): BatchedAdapter
interface BatchOptions {
maxSize?: number; // default 10
maxWait?: number; // ms, default 5000
onError?: (error: unknown, events: TrackrEvent[]) => void;
}
interface BatchedAdapter extends StorageAdapter {
flush(): Promise<void>;
readonly pending: number;
}Behaviour and examples for every adapter: Storage adapters. The
full type declarations are in the package (dist/**/*.d.ts) and on
npm.