@datalyr/web SDK for TypeScript support and advanced tracking.
Use Cases
SaaS Applications Track signups, feature usage, and subscription conversions with full TypeScript support. E-commerce Track product views, cart interactions, and purchases with server-side rendering support. Marketing Sites Monitor landing page performance, form submissions, and ad campaign attribution. Web Apps Track user behavior, feature adoption, and custom events with full control.Before You Start
- You have a DATALYR account
- You’ve created a workspace
- You have a Next.js project (App Router or Pages Router)
- Node.js 14+ installed
Installation Method
Choose between SDK (recommended) or Script tag:Method 1: Using @datalyr/web SDK (Recommended)
Full TypeScript support, type-safe events, and programmatic control.Method 2: Using Script Tag
Simple HTML script in your layout file. No npm install needed.Method 1: Install @datalyr/web SDK
Step 1: Install the Package
Step 2: Initialize in App Router (Next.js 13+)
Create a client component for DATALYR initialization:app/providers/datalyr-provider.tsx
app/layout.tsx
Step 3: Initialize in Pages Router (Next.js 12 and below)
Add topages/_app.tsx:
Step 4: Add Environment Variable
.env.local
Method 2: Using Script Tag
App Router (Next.js 13+)
Add toapp/layout.tsx:
Pages Router
Add topages/_document.tsx:
Track Custom Events
Using @datalyr/web SDK
Using window.datalyr (Script Tag Method)
Identify Users
Track users for cross-device attribution:Track Page Views (App Router)
For App Router, manually track page views on route changes:Server-Side Tracking
For server-side events (API routes, Server Components):TypeScript Support
The@datalyr/web SDK includes full TypeScript definitions:
Troubleshooting
Events not showing in dashboard? Check that:- Your workspace ID is correct in environment variables
- You’re using
NEXT_PUBLIC_prefix for client-side variables - SDK is initialized before tracking events
- Ad blockers are disabled
_app.tsx or root layout, not in individual pages).
TypeScript errors?
Install type definitions:
What Gets Tracked Automatically
The SDK automatically captures:- Page views (when you call
datalyr.page()) - Visitor IDs (persistent across sessions)
- UTM parameters and ad click IDs
- Referrer and traffic source
- Device and browser information
- Session tracking
Next Steps
Track Custom Events
Learn the full event tracking API
Identify Users
Track users across devices
Connect Integrations
Link Meta, Google, TikTok Ads
View Dashboard
See your tracking data