-
Notifications
You must be signed in to change notification settings - Fork 7
Add server-side PostHog tracking for AI agent analytics #752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nearestnabors
wants to merge
3
commits into
main
Choose a base branch
from
feature/posthog-ai-agent-tracking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+286
−21
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import { PostHog } from "posthog-node"; | ||
|
|
||
| // Server-side PostHog client singleton | ||
| let posthogClient: PostHog | null = null; | ||
|
|
||
| export function getPostHogServer(): PostHog { | ||
| if (!posthogClient) { | ||
| const apiKey = process.env.NEXT_PUBLIC_POSTHOG_KEY; | ||
| if (!apiKey) { | ||
| throw new Error("NEXT_PUBLIC_POSTHOG_KEY is not set"); | ||
| } | ||
| posthogClient = new PostHog(apiKey, { | ||
| host: process.env.NEXT_PUBLIC_POSTHOG_HOST || "https://us.i.posthog.com", | ||
| // Flush events immediately in serverless environments | ||
| flushAt: 1, | ||
| flushInterval: 0, | ||
| }); | ||
| } | ||
| return posthogClient; | ||
| } | ||
|
|
||
| // AI agent detection patterns with classification | ||
| const AI_AGENT_CLASSIFIERS: Array<{ | ||
| pattern: RegExp; | ||
| type: string; | ||
| provider: string; | ||
| }> = [ | ||
| // OpenAI | ||
| { pattern: /GPTBot/i, type: "GPTBot", provider: "OpenAI" }, | ||
| { pattern: /ChatGPT-User/i, type: "ChatGPT-User", provider: "OpenAI" }, | ||
| { pattern: /OAI-SearchBot/i, type: "OAI-SearchBot", provider: "OpenAI" }, | ||
|
|
||
| // Anthropic | ||
| { pattern: /ClaudeBot/i, type: "ClaudeBot", provider: "Anthropic" }, | ||
| { pattern: /Claude-User/i, type: "Claude-User", provider: "Anthropic" }, | ||
| { | ||
| pattern: /Claude-SearchBot/i, | ||
| type: "Claude-SearchBot", | ||
| provider: "Anthropic", | ||
| }, | ||
| { pattern: /anthropic/i, type: "Anthropic-Agent", provider: "Anthropic" }, | ||
|
|
||
| // Perplexity | ||
| { pattern: /PerplexityBot/i, type: "PerplexityBot", provider: "Perplexity" }, | ||
| { | ||
| pattern: /Perplexity-User/i, | ||
| type: "Perplexity-User", | ||
| provider: "Perplexity", | ||
| }, | ||
|
|
||
| { pattern: /Google-Extended/i, type: "Google-Extended", provider: "Google" }, | ||
| { pattern: /Googlebot/i, type: "Googlebot", provider: "Google" }, | ||
|
|
||
| // Amazon | ||
| { pattern: /Amazonbot/i, type: "Amazonbot", provider: "Amazon" }, | ||
| { pattern: /amazonq/i, type: "Amazon-Q", provider: "Amazon" }, | ||
| { pattern: /amazon-q/i, type: "Amazon-Q", provider: "Amazon" }, | ||
|
|
||
| // Apple | ||
| { | ||
| pattern: /Applebot-Extended/i, | ||
| type: "Applebot-Extended", | ||
| provider: "Apple", | ||
| }, | ||
|
|
||
| // Meta | ||
| { pattern: /meta-externalagent/i, type: "Meta-Agent", provider: "Meta" }, | ||
|
|
||
| // ByteDance | ||
| { pattern: /Bytespider/i, type: "Bytespider", provider: "ByteDance" }, | ||
|
|
||
| // Cohere | ||
| { pattern: /cohere-ai/i, type: "Cohere-AI", provider: "Cohere" }, | ||
|
|
||
| // Common Crawl | ||
| { pattern: /CCBot/i, type: "CCBot", provider: "CommonCrawl" }, | ||
|
|
||
| // Developer tools | ||
| { pattern: /cursor/i, type: "Cursor", provider: "Cursor" }, | ||
| { pattern: /github.copilot/i, type: "GitHub-Copilot", provider: "GitHub" }, | ||
| { pattern: /copilot/i, type: "Copilot", provider: "GitHub" }, | ||
| { pattern: /codeium/i, type: "Codeium", provider: "Codeium" }, | ||
| { pattern: /tabnine/i, type: "Tabnine", provider: "Tabnine" }, | ||
|
|
||
| // Other AI services | ||
| { pattern: /gemini/i, type: "Gemini", provider: "Google" }, | ||
| { pattern: /bard/i, type: "Bard", provider: "Google" }, | ||
| { pattern: /phind/i, type: "Phind", provider: "Phind" }, | ||
| { pattern: /you\.com/i, type: "You.com", provider: "You.com" }, | ||
| { pattern: /ai21/i, type: "AI21", provider: "AI21" }, | ||
| { pattern: /huggingface/i, type: "HuggingFace", provider: "HuggingFace" }, | ||
|
|
||
| // Agent frameworks | ||
| { pattern: /langchain/i, type: "LangChain", provider: "LangChain" }, | ||
| { pattern: /llamaindex/i, type: "LlamaIndex", provider: "LlamaIndex" }, | ||
| { pattern: /autogpt/i, type: "AutoGPT", provider: "AutoGPT" }, | ||
| { pattern: /agentgpt/i, type: "AgentGPT", provider: "AgentGPT" }, | ||
| { pattern: /babyagi/i, type: "BabyAGI", provider: "BabyAGI" }, | ||
|
|
||
| // Doc AI tools | ||
| { pattern: /kapa\.ai/i, type: "Kapa.ai", provider: "Kapa" }, | ||
| { pattern: /mendable/i, type: "Mendable", provider: "Mendable" }, | ||
| { pattern: /inkeep/i, type: "Inkeep", provider: "Inkeep" }, | ||
| { pattern: /glean/i, type: "Glean", provider: "Glean" }, | ||
| ]; | ||
|
|
||
| export type AIAgentClassification = { | ||
| isAIAgent: boolean; | ||
| agentType: string | null; | ||
| agentProvider: string | null; | ||
| }; | ||
|
|
||
| export function classifyAIAgent(userAgent: string): AIAgentClassification { | ||
| for (const classifier of AI_AGENT_CLASSIFIERS) { | ||
| if (classifier.pattern.test(userAgent)) { | ||
| return { | ||
| isAIAgent: true, | ||
| agentType: classifier.type, | ||
| agentProvider: classifier.provider, | ||
| }; | ||
| } | ||
| } | ||
| return { | ||
| isAIAgent: false, | ||
| agentType: null, | ||
| agentProvider: null, | ||
| }; | ||
| } | ||
|
|
||
| export type ServerPageViewEvent = { | ||
| distinctId: string; | ||
| pathname: string; | ||
| userAgent: string; | ||
| referer?: string; | ||
| ip?: string; | ||
| acceptHeader?: string; | ||
| acceptLanguage?: string; | ||
| }; | ||
|
|
||
| export async function captureServerPageView(event: ServerPageViewEvent) { | ||
| const posthog = getPostHogServer(); | ||
| const classification = classifyAIAgent(event.userAgent); | ||
|
|
||
| // Use distinct event name to avoid double-counting with client-side $pageview | ||
| // This tracks server-side markdown requests (primarily from AI agents) | ||
| posthog.capture({ | ||
| distinctId: event.distinctId, | ||
| event: "server_markdown_request", | ||
| properties: { | ||
| $current_url: event.pathname, | ||
| $pathname: event.pathname, | ||
| $referrer: event.referer, | ||
| $useragent: event.userAgent, | ||
|
|
||
| // AI agent classification | ||
| is_ai_agent: classification.isAIAgent, | ||
| ai_agent_type: classification.agentType, | ||
| ai_agent_provider: classification.agentProvider, | ||
|
|
||
| // Request metadata | ||
| request_accept_header: event.acceptHeader, | ||
| request_accept_language: event.acceptLanguage, | ||
| request_source: "server", | ||
|
|
||
| // Mark as server-side capture | ||
| $lib: "posthog-node", | ||
| }, | ||
| }); | ||
|
|
||
| // Flush immediately for serverless | ||
| await posthog.flush(); | ||
| } | ||
|
|
||
| // Shutdown handler for graceful shutdown | ||
| export async function shutdownPostHog() { | ||
| if (posthogClient) { | ||
| await posthogClient.shutdown(); | ||
| posthogClient = null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... and if not, we should publish and own that package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, cool - then move this into its own data file and we can keep it up-to-date more simply