Skip to content

trackkit / index / AnalyticsInstance

Interface: AnalyticsInstance<E>

Defined in: types.ts:506

Public analytics instance exposed by Trackkit.

This is what callers interact with when using the facade API.

The generic parameter E allows compile-time checking of track() calls against a user-defined event map. When omitted, all event names and props are accepted (fully open, same as pre-typed-events behaviour).

Example

ts
type MyEvents = {
  signup: { plan: 'free' | 'pro' };
  purchase: { amount: number; currency: string };
};

// Typed — track() is checked against MyEvents
const analytics: AnalyticsInstance<MyEvents> = createAnalytics<MyEvents>({ ... });
analytics.track('signup', { plan: 'pro' });  // ✅
analytics.track('typo');                      // ❌ type error

// Untyped — any event name and any props accepted
const open: AnalyticsInstance = createAnalytics({ ... });
open.track('anything', { any: 'props' });     // ✅

Type Parameters

E

E extends EventMap = AnyEventMap

Event map type. Defaults to AnyEventMap (fully open).

Properties

name

name: string

Defined in: types.ts:508

Name of the active provider (e.g. 'ga4', 'plausible').

Methods

track()

track<K>(...args): void

Defined in: types.ts:524

Track a custom event.

When E is a concrete event map, both name and props are type-checked against it. Props are required when the event map declares required fields, and optional when all fields are optional (or when using the default AnyEventMap).

Type Parameters

K

K extends string

Inferred from the event name literal.

Parameters

args

...object extends E[K] ? [K, E[K], ConsentCategory] : [K, E[K], ConsentCategory]

Returns

void


pageview()

pageview(): void

Defined in: types.ts:533

Track a page view using the current page context.

Returns

void


identify()

identify(userId): void

Defined in: types.ts:540

Identify the current user.

Parameters

userId

User identifier or null to clear.

null | string

Returns

void


destroy()

destroy(): void

Defined in: types.ts:547

Clean up and destroy the instance.

After calling this, the instance should not be used again.

Returns

void

Released under the MIT License.