analytics/examples/nestjs-backend
2026-01-29 08:20:58 -08:00
..
analytics.interceptor.ts docs(examples): 📝 Refactor example configurations to align with latest best practices, adding clearer workflows and feature demonstrations 2026-01-29 08:20:58 -08:00
analytics.module.ts docs(examples): 📝 Refactor example configurations to align with latest best practices, adding clearer workflows and feature demonstrations 2026-01-29 08:20:58 -08:00
order.service.ts docs(examples): 📝 Refactor example configurations to align with latest best practices, adding clearer workflows and feature demonstrations 2026-01-29 08:20:58 -08:00
README.md docs(examples): 📝 Refactor example configurations to align with latest best practices, adding clearer workflows and feature demonstrations 2026-01-29 08:20:58 -08:00
track-analytics.decorator.ts docs(examples): 📝 Refactor example configurations to align with latest best practices, adding clearer workflows and feature demonstrations 2026-01-29 08:20:58 -08:00
user.controller.ts docs(examples): 📝 Refactor example configurations to align with latest best practices, adding clearer workflows and feature demonstrations 2026-01-29 08:20:58 -08:00

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)