import datalyr from '@datalyr/web';datalyr.init({ workspaceId: 'abc123xyz'});
The npm package is the recommended approach — it bundles into your own domain for better privacy, avoids ad blockers, and gives you full access to the SDK API including TypeScript support, plugins, and tree-shaking.
// After user logs indatalyr.identify('user_123', { email: 'john@example.com', name: 'John Doe', plan: 'pro'});
Links anonymous visitor to known user for attribution.
The signature is identify(userId, traits) — pass the email as both the userId and a traits.email, e.g. datalyr.identify('john@example.com', { email: 'john@example.com' }). It is notidentify({ email }). This is the deterministic web→app match key for App Campaigns.
Automatically capture the visitor’s email — without writing an identify() call — from form/optin submits, same-origin XHR/fetch bodies, and (on Shopify) the logged-in customer. This is the lowest-friction way to light up deterministic web→app matching on App Campaigns landers. Off by default.Recommended — turn it on in the dashboard (no code): enable Auto Identify in Settings → Identity & Attribution. It applies to the live SDK in ~5 minutes with no snippet change. See the Identity Bridge.Script tag — one attribute:
When it captures an email it calls identify() for you (firing $identify).
Precedence is built-in defaults ← dashboard ← explicit init()/attribute. An explicit autoIdentify in code (or data-auto-identify on the tag) always wins over the dashboard toggle, so you can pin a per-environment value in code and still manage everything else from the dashboard.
autoIdentify captures from form submits and same-origin requests. If your optin POSTs to a third-party email tool on another domain, the XHR path won’t see it — use an explicit identify() call instead. It also stores the captured email in the browser, so for health or other redacted verticals do not enable it — there is no automatic client-side redaction.
// Set once, sent with all eventsdatalyr.setSuperProperties({ app_version: '2.1.0', environment: 'production'});// Unset super propertydatalyr.unsetSuperProperty('environment');
// Get current session IDconst sessionId = datalyr.getSessionId();// Get session dataconst session = datalyr.getSessionData();// Start new session manuallydatalyr.startNewSession();
// Get anonymous IDconst anonId = datalyr.getAnonymousId();// Get user ID (if identified)const userId = datalyr.getUserId();// Get distinct ID (userId or anonId)const distinctId = datalyr.getDistinctId();// Alias user (connect IDs)datalyr.alias('user_123', 'previous_id');
// Opt user out of trackingdatalyr.optOut();// Opt user back indatalyr.optIn();// Check opt-out statusconst isOptedOut = datalyr.isOptedOut();// Set consent preferencesdatalyr.setConsent({ analytics: true, advertising: false});
This method captures full attribution data, flushes the event queue, and redirects the user to the app store. For Android, it encodes referrer parameters into the Play Store URL for deterministic attribution.Learn more about App Campaigns
Same Root Domain (Subdomains):
Enable in Settings → Advanced → Cross-Domain Tracking → Include Subdomains.Cookies set on .example.com work across www.example.com, app.example.com, blog.example.com.Different Domains:
Enable in Settings → Advanced → Cross-Domain Tracking and add domains to whitelist.Links automatically include _dl_visitor parameter to maintain identity.