Get agent traffic tracking live on your docs in under an hour. This playbook walks you through installing the Lightsage middleware, verifying detection, and creating your first analytics dashboard.
Make sure you have these before starting.
Vercel, Cloudflare Pages, or any Node.js-compatible server
The 7-day trial works for getting started
Ability to deploy middleware changes
Install Lightsage middleware
Configure environment variables
Verify agent detection is working
Set up conversion tracking
Create first dashboard
~5 minutes
The Lightsage middleware runs at the edge and detects AI agent traffic by analyzing request headers and patterns. It adds minimal latency (typically under 5ms) and works with any JavaScript runtime.
npm install @lightsage/middlewareOr use yarn: yarn add @lightsage/middleware
Create a middleware.ts file in your project root (for Next.js) or the appropriate location for your framework.
import { lightsage } from '@lightsage/middleware';
export const middleware = lightsage({
publicKey: process.env.LIGHTSAGE_PUBLIC_KEY!,
});
export const config = {
matcher: [
// Match all paths except static files and API routes
'/((?!_next/static|_next/image|favicon.ico|api/).*)',
],
};Tip: The matcher configuration ensures the middleware only runs on content pages, not static assets. This keeps performance optimal.
~2 minutes
Get your public key from the Lightsage dashboard and add it to your environment configuration.
pk_)LIGHTSAGE_PUBLIC_KEY=pk_live_your_key_hereAdd the environment variable in Project Settings > Environment Variables. Make sure to add it to all environments (Production, Preview, Development).
Add the variable in Settings > Environment variables. Use the wrangler CLI for local development: wrangler secret put LIGHTSAGE_PUBLIC_KEY
Set the environment variable in your deployment configuration, Docker compose file, or server environment.
~5 minutes
After deploying, verify that the middleware is correctly detecting traffic. You can test this locally or in production.
Simulate an AI agent request to verify detection:
# Simulate Claude Code user agent
curl -H "User-Agent: claudebot/1.0" http://localhost:3000/docs
# Simulate Cursor agent
curl -H "User-Agent: cursor-ai/1.0" http://localhost:3000/docs
# Simulate ChatGPT plugin
curl -H "User-Agent: ChatGPT-User" http://localhost:3000/docsFor troubleshooting, enable debug logging:
export const middleware = lightsage({
publicKey: process.env.LIGHTSAGE_PUBLIC_KEY!,
debug: process.env.NODE_ENV === 'development',
});Debug mode logs detected agents and attribution data to the console.
At this point, you should see test requests in your Lightsage dashboard. If not, check the Troubleshooting section below.
~10 minutes
Connect agent visits to actual conversions like signups, API key generation, or purchases. This lets you attribute revenue to specific agents.
npm install @lightsage/clientCall trackConversion when a user completes a valuable action:
import { trackConversion } from '@lightsage/client';
async function handleSignup(formData: FormData) {
// Your existing signup logic
const user = await createUser(formData);
// Track the conversion
await trackConversion({
event: 'signup',
userId: user.id,
properties: {
plan: 'free',
source: 'docs',
},
});
}| Event | When to fire |
|---|---|
| signup | User creates an account |
| api_key_created | User generates their first API key |
| first_api_call | User makes their first successful API request |
| upgrade | User upgrades to a paid plan |
For purchase events, use server-side tracking for accuracy:
import { Lightsage } from '@lightsage/node';
const lightsage = new Lightsage({
secretKey: process.env.LIGHTSAGE_SECRET_KEY!,
});
export async function POST(req: Request) {
const event = await parseStripeWebhook(req);
if (event.type === 'checkout.session.completed') {
await lightsage.trackConversion({
event: 'purchase',
userId: event.data.object.client_reference_id,
revenue: event.data.object.amount_total / 100,
currency: 'USD',
});
}
}~8 minutes
Set up a dashboard to monitor agent traffic patterns and conversion attribution.
What percentage of your docs traffic comes from AI agents vs browsers?
Which agents visit most: Claude Code, Cursor, ChatGPT, Codex?
Which documentation pages do agents visit most frequently?
How often do agent sessions lead to signups vs browser sessions?
Get notified when agent traffic patterns change significantly:
Agent analytics is now live. Give it 24-48 hours to collect meaningful data, then review your dashboard to understand how AI agents interact with your docs.
If you have existing middleware, compose them together:
import { lightsage } from '@lightsage/middleware';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
const lightsageMiddleware = lightsage({
publicKey: process.env.LIGHTSAGE_PUBLIC_KEY!,
});
export async function middleware(request: NextRequest) {
// Run Lightsage first
const response = await lightsageMiddleware(request);
// Add your custom middleware logic
// Example: Add security headers
response.headers.set('X-Frame-Options', 'DENY');
return response;
}Use the Cloudflare-specific adapter for optimal edge performance:
import { lightsageCloudflare } from '@lightsage/middleware/cloudflare';
export const onRequest = lightsageCloudflare({
publicKey: env.LIGHTSAGE_PUBLIC_KEY,
});Add the Lightsage script to your Mintlify configuration:
{
"integrations": {
"lightsage": {
"publicKey": "pk_live_your_key_here"
}
}
}Note: Mintlify's built-in integration handles agent detection automatically. No middleware setup required.
Still stuck? Check the Agent Analytics overview or contact support.
Set up takes 30 minutes. Start tracking which AI agents visit your docs today.