Add server-side PostHog tracking for AI agent analytics#752
Add server-side PostHog tracking for AI agent analytics#752nearestnabors wants to merge 3 commits intomainfrom
Conversation
- Disable bot filtering in client-side PostHog to capture AI agent traffic - Add posthog-node for server-side tracking - Create AI agent classification system with 40+ patterns (OpenAI, Anthropic, Perplexity, etc.) - Track markdown API requests with agent classification properties: - is_ai_agent (boolean) - ai_agent_type (e.g., "ClaudeBot") - ai_agent_provider (e.g., "Anthropic") Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| } | ||
|
|
||
| // AI agent detection patterns with classification | ||
| const AI_AGENT_CLASSIFIERS: Array<{ |
There was a problem hiding this comment.
Keeping this list up-to-date ourselves seems tedious. Is there a pacakge we can install from someone else who is maintaining this list?
There was a problem hiding this comment.
... and if not, we should publish and own that package.
There was a problem hiding this comment.
It doesn't change that much/is relatively stable. I think publishing such a package is a great idea! But perhaps we should wait to do so until this needs updating?
There was a problem hiding this comment.
ok, cool - then move this into its own data file and we can keep it up-to-date more simply
app/_lib/posthog-server.ts
Outdated
|
|
||
| posthog.capture({ | ||
| distinctId: event.distinctId, | ||
| event: "$pageview", |
There was a problem hiding this comment.
If these agents are using browsers, will we get 2 events for each pageview (one from the frontend and one from the backend)? Perhaps a different event name would clear that up (backend_pageview?)
There was a problem hiding this comment.
Changed the event name from $pageview to server_markdown_request.
This way:
- Client-side: $pageview (from posthog-js when JS executes)
- Server-side: server_markdown_request (from posthog-node when markdown is served)
No double-counting, and it's clear in dashboards which is which. You can reply to the PR
comment with that explanation.
| } catch { | ||
| // Silently fail - tracking errors should not affect the response | ||
| } |
There was a problem hiding this comment.
log the error so we can fix it!
Using a distinct event name instead of $pageview prevents confusion when AI agents that use headless browsers trigger both client-side and server-side events. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
evantahler
left a comment
There was a problem hiding this comment.
A nit about where to store the browserlist, otherwise, let's try it!
| } | ||
|
|
||
| // AI agent detection patterns with classification | ||
| const AI_AGENT_CLASSIFIERS: Array<{ |
There was a problem hiding this comment.
ok, cool - then move this into its own data file and we can keep it up-to-date more simply
Summary
opt_out_useragent_filter: true) to capture AI agent trafficposthog-nodefor server-side tracking of markdown API requestsis_ai_agent(boolean)ai_agent_type(e.g., "ClaudeBot")ai_agent_provider(e.g., "Anthropic")This enables tracking AI agent traffic that doesn't execute client-side JavaScript (e.g., agents hitting cached CDN content).
Related
Based on Q1 Agent-Led Growth (ALG) proposal and conversation with PostHog support (Lucas Ricoy).
Test plan
curl -H "User-Agent: ClaudeBot" http://localhost:3000/en/homeis_ai_agentevents after deployment🤖 Generated with Claude Code