Skip to main content

Browser NPM SDK

Install:

npm install @drivemetadata-ai/sdk

Import:

import {
alias,
consent,
flush,
getDmdHealth,
group,
identify,
initDmdSDK,
page,
reset,
track
} from '@drivemetadata-ai/sdk/browser';

Initialize in browser runtime:

initDmdSDK({
clientId: 'client_xxx',
workspaceId: 'workspace_xxx',
appId: 'app_xxx',
token: 'public_token',
consent: {
analytics: 'granted',
advertising: 'denied',
personalization: 'denied',
functional: 'granted',
saleOfData: 'denied'
}
});

If consent is omitted, analytics defaults to pending and events are dropped until consent is granted.

Track event:

track('Product Viewed', {
productId: 'sku_123'
});

Identify user:

identify('user_123', {
plan: 'enterprise'
});

Track page view:

page();

Group account:

group('account_123', {
tier: 'enterprise'
});

Alias anonymous to known user:

alias('anonymous_123', 'user_123');

Update consent:

consent.update({
analytics: 'denied',
advertising: 'denied'
});

Reset identity on logout:

reset();

Inspect health:

console.log(getDmdHealth());

Flush queued events:

await flush();

SSR note:

The browser package can be imported during SSR, but initDmdSDK must run only in the browser. For Next.js, call it inside a client component or use the React/Next adapter from a later SDK phase.

Diagnostics:

getDmdHealth() reports initialized state, consent state, queue size, offline state, dropped-event count, last error, and last dropped-event reason.

Delivery Troubleshooting

Use getDmdHealth() to inspect queue size, offline state, last error, and dropped delivery records.

import { flush, getDmdHealth } from '@drivemetadata-ai/sdk/browser';

console.log(getDmdHealth());
await flush();

Common drop reasons:

ReasonMeaningFix
consent_deniedAnalytics consent is not grantedCall setConsent({ analytics: 'granted' }) after user consent
payload_too_largeEvent exceeds configured payload sizeReduce properties or increase delivery.maxPayloadBytes
queue_limit_exceededOffline queue is fullIncrease delivery.maxQueueSize or flush more often
queue_ttl_expiredEvent stayed offline past TTLIncrease delivery.queueTtlMs if appropriate

Delivery behavior:

The browser SDK adds message IDs and idempotency keys to delivery payloads. Queue diagnostics include expired records, and queue flushing uses a cross-tab lease to avoid duplicate ownership when multiple tabs are open.

metaData.requestId, metaData.anonymousId, and metaData.sessionId are UUID strings.

metaData.timestamp, metaData.requestSentAt, and metaData.requestReceivedAt use UTC YYYY-MM-DD HH:mm:ss format. Invalid timestamp strings are normalized to the current UTC time before sending.