Skip to main content
Server-side tracking uses @grainql/analytics-web, a separate package from @grainql/tag. Grain Tag is for browser-side analytics. For browser tracking, see Installation.
Use Grain Analytics on your backend to track API calls, background jobs, and server-side events.

Install the Package

npm
yarn
pnpm

Initialize the SDK

Keep your secret key safe! Never commit it to version control or expose it in client-side code. Use environment variables.

Track Server-Side Events

Serverless environments: Use { flush: true } to send events immediately before the function terminates. Otherwise, events might not get sent.

Track Background Jobs

Use Remote Configuration

Control server-side behavior without redeploying:

Express.js Middleware

Track all requests automatically:

Fastify Plugin

AWS Lambda Example

Lambda optimization: Initialize the Grain client outside your handler function so it’s reused across invocations. This improves performance and reduces cold starts.

Vercel Serverless Functions

Track User Events Server-Side

Associate events with specific users:

Error Tracking

Track backend errors to understand failure patterns:

Testing Your Integration

Run it:
Check your dashboard to see the event come in.

What’s Next?

Core API Reference

See all available methods and options

Authentication

Learn about server-side authentication

Query API

Query your analytics data programmatically

Configuration Guide

Advanced configuration options
Production checklist:
  • Store secret key in environment variables
  • Use { flush: true } for serverless functions
  • Set up error handling and retry logic
  • Monitor batch queue size in long-running processes