1.1 KiB
1.1 KiB
NestJS Backend Integration
Server-side analytics tracking for NestJS applications.
Use Cases
- Track events from server-side rendered pages
- Track API events (signups, purchases, etc.)
- Add analytics context via interceptors
- Automatic request tracking with decorators
Files
analytics.module.ts- Module setupanalytics.interceptor.ts- Automatic request trackingtrack-analytics.decorator.ts- Method-level trackinguser.controller.ts- Example controller with trackingorder.service.ts- Example service with tracking
Key Concepts
1. Backend Client
The BackendAnalyticsClient is designed for server-side use:
- Generates its own session IDs (from headers or fresh)
- No browser APIs required
- Batch processing for performance
2. Interceptor vs Decorator
- Interceptor: Automatic tracking on all routes (or specific ones)
- Decorator: Explicit tracking on specific methods
3. Request Context
Pass request headers to extract:
- Session ID (
x-session-idheader from client) - User ID (from auth middleware)
- IP address (for geolocation)
- User agent (for device detection)