Identity & Tracking
Anonymous vs. identified users
Section titled “Anonymous vs. identified users”The SDK tracks every visitor, even before they log in.
Anonymous tracking
Section titled “Anonymous tracking”On first load, the SDK creates a browser-local anonymous ID. It is:
- sent with access checks
- attached to tracked events
- persisted across page loads and sessions
Identifying users
Section titled “Identifying users”There are two ways to identify a user:
1. Authenticated identity (recommended)
Section titled “1. Authenticated identity (recommended)”Use the built-in customer auth for cryptographically verified identity:
import { init, login, register, checkAccess } from '@latch/sdk';
init({ apiKey: 'pk_...', apiUrl: 'https://your-latch-api' });
// Register a new customer
// Or log in an existing customer
// Access checks now automatically include the verified JWTconst access = await checkAccess();When authenticated, the SDK attaches the JWT as a Bearer token to access checks. The API verifies the token cryptographically rather than trusting a client-side userId.
See the Authentication page for the full auth API.
2. Manual identity (simple, unverified)
Section titled “2. Manual identity (simple, unverified)”For simpler setups where identity spoofing is not a concern, call identify():
import { identify } from '@latch/sdk';
identify('user_123');Once identified, access checks include userId and subscription lookups can succeed for that user.
Pre-setting identity
Section titled “Pre-setting identity”import { init } from '@latch/sdk';
init({ apiKey: 'pk_...', apiUrl: 'https://your-latch-api', userId: getCurrentUserId(),});Resetting identity
Section titled “Resetting identity”import { resetIdentity } from '@latch/sdk';
resetIdentity();resetIdentity() clears userId and falls back to the browser’s stored anonymous ID.
It does not rotate the anonymous ID or reset meter history.
Event tracking
Section titled “Event tracking”Pageviews
Section titled “Pageviews”init() automatically queues an initial pageview. For SPAs, call trackPageview() after client-side navigation.
import { trackPageview } from '@latch/sdk';
trackPageview({ section: 'technology' });Captured fields include url, path, title, and referrer.
Custom events
Section titled “Custom events”import { trackEvent } from '@latch/sdk';
trackEvent('article_view', { slug: 'my-premium-article', premium: true,});Batching
Section titled “Batching”The SDK flushes queued events:
- every 5 seconds
- when the queue reaches 20 items
- on
beforeunload
If a flush fails, events are placed back in the queue for retry.