Skip to content

Identity & Tracking

The SDK tracks every visitor, even before they log in.

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

There are two ways to identify a user:

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
const { customer } = await register('[email protected]', 'securepass123', 'Jane');
// Or log in an existing customer
const { customer } = await login('[email protected]', 'securepass123');
// Access checks now automatically include the verified JWT
const 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.

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.

import { init } from '@latch/sdk';
init({
apiKey: 'pk_...',
apiUrl: 'https://your-latch-api',
userId: getCurrentUserId(),
});
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.

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.

import { trackEvent } from '@latch/sdk';
trackEvent('article_view', {
slug: 'my-premium-article',
premium: true,
});

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.