analytics/examples/nestjs-backend/README.md
2026-01-29 08:20:58 -08:00

40 lines
1.1 KiB
Markdown

# 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 setup
- `analytics.interceptor.ts` - Automatic request tracking
- `track-analytics.decorator.ts` - Method-level tracking
- `user.controller.ts` - Example controller with tracking
- `order.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-id` header from client)
- User ID (from auth middleware)
- IP address (for geolocation)
- User agent (for device detection)