Skip to main content
The Web SDK tracks website activity, user identity, sessions, and attribution.

Install

npm install @datalyr/web
import datalyr from '@datalyr/web'

datalyr.init({ workspaceId: 'YOUR_WORKSPACE_ID' })
await datalyr.ready()
Choose one installation method. Do not install the package and script tag on the same page.

Track an event

datalyr.track('signup_clicked', {
  location: 'pricing_page'
})

Identify a user

datalyr.identify('user_123', {
  email: '[email protected]',
  plan: 'pro'
})
Call identify after the user signs in or when you otherwise know their stable ID.

Track a page

datalyr.page({
  title: 'Pricing',
  variant: 'A'
})
The SDK tracks the first page view and SPA navigation by default. Use page when you need to record one manually.

Core methods

MethodUse it to
init(config)Initialize the SDK with a workspace ID.
ready()Wait for asynchronous initialization to finish.
track(name, properties?)Track an event.
identify(userId, traits?)Attach a stable ID and traits to the visitor.
page(properties?)Track a page view manually.
alias(userId, previousId?)Link two IDs that belong to the same person.
reset()Clear identity when a user signs out.
flush()Send queued events immediately.
optOut() / optIn()Change tracking preference.
setConsent(consent)Apply analytics and marketing consent.

Privacy

datalyr.setConsent({
  analytics: true,
  marketing: false
})
Use reset() on logout so the next user does not inherit the previous user’s identity.

Configuration

Only workspaceId is required. These are the options most implementations need.
OptionTypeDefaultWhat it controls
workspaceIdstringRequiredThe workspace that receives events.
debugbooleanfalseConsole logging during development.
trackPageViewsbooleantrueTracks the first page view.
trackSPAbooleantrueTracks client-side navigation.
trackSessionsbooleantrueAdds session data to events.
sessionTimeoutnumber60Session timeout in minutes.
attributionWindownumber90Attribution window in days.
trackedParamsstring[][]Additional URL parameters to capture.
privacyMode'standard' | 'strict''standard'Limits collection in strict mode.
respectGlobalPrivacyControlbooleantrueRespects the browser GPC signal.
respectDoNotTrackbooleanfalseRespects the browser DNT signal when enabled.
cookieDomainstring | 'auto''auto'Cookie domain used for identity.
cookieExpiresnumber365Cookie lifetime in days.
autoIdentifybooleanfalseEnables automatic email identification.
enableContainerbooleantrueLoads configured container scripts.
platform'shopify' | 'checkoutchamp' | 'generic'Enables platform-specific behavior.
stripePaymentLinksbooleantrueAdds Datalyr identity to supported Stripe payment links.
Start with the defaults. Change batching, retry, cookie, or attribution options only when your implementation requires it.

Identity

Use a stable ID from your own system:
datalyr.identify(currentUser.id, {
  email: currentUser.email,
  plan: currentUser.plan
})
Useful identity methods:
MethodReturns
getAnonymousId()Persistent anonymous browser ID.
getVisitorId()Current visitor ID.
getUserId()Identified user ID, or null.
getDistinctId()User ID when identified; otherwise the anonymous ID.
getSessionId()Current session ID.
Use alias(newId, previousId) only when both IDs belong to the same person. Use reset() for logout or account switching.

Attribution

const attribution = datalyr.getAttribution()
const journey = datalyr.getJourney()
The SDK captures supported click IDs, UTM parameters, and referrer data. URL values are redacted before collection when they contain known sensitive parameters. You can also set known attribution manually:
datalyr.setAttribution({
  utm_source: 'partner',
  utm_campaign: 'spring_launch'
})

Checkout metadata

Use the SDK helpers when creating a checkout on your server:
const stripe = datalyr.getStripeMetadata()
const whop = datalyr.getWhopCheckoutMetadata()
These helpers return the current visitor identity in the shape expected by the supported checkout flow.

Super properties

Super properties are added to every later event:
datalyr.setSuperProperties({ app_version: '2.4.0' })
datalyr.unsetSuperProperty('app_version')

Lifecycle

await datalyr.flush()
datalyr.destroy()
flush() sends queued events. destroy() removes listeners and stops the SDK; only use it when the integration is being torn down permanently.