Skip to content

feat: add middleware to track AI coding agent traffic#2499

Open
caffeinum wants to merge 1 commit intolangfuse:mainfrom
caffeinum:ai-analytics-improvements
Open

feat: add middleware to track AI coding agent traffic#2499
caffeinum wants to merge 1 commit intolangfuse:mainfrom
caffeinum:ai-analytics-improvements

Conversation

@caffeinum
Copy link

@caffeinum caffeinum commented Feb 5, 2026

Summary

Adds Next.js middleware to detect and track AI coding agents (Claude Code, Codex, OpenCode, etc.) visiting the docs.

Uses the 2027-track npm package:

import { withAIAnalytics } from "2027-track/next";
export default withAIAnalytics();

How it works

AI coding agents send Accept: text/markdown header when fetching documentation - browsers never do this. The middleware detects this signal and sends anonymized events to an external analytics API.

Detection signals:

  • Accept: text/markdown (primary - browsers never send this)
  • axios user-agent → Claude Code
  • text/markdown with q= weights → OpenCode
  • ChatGPT-User user-agent → Codex

Configuration

No configuration needed. The middleware sends events to an external tracking API - no env vars or secrets required in this repo.

Optional: Set AI_ANALYTICS_ENDPOINT="" to disable tracking entirely.

Data collected

  • Host (docs.langfuse.com)
  • Path (/docs/tracing)
  • Agent type (claude-code, opencode, codex, etc.)
  • Country (from Vercel headers)
  • Timestamp

Privacy: Events are sent server-side from Vercel Edge. Visitor IP addresses never reach the analytics endpoint.

Dashboard

See collected data: screenshot

Package: https://www.npmjs.com/package/2027-track
Source: https://github.com/caffeinum/ai-docs-analytics


Important

Add middleware in middleware.ts to track AI coding agents using 2027-track, with route configuration and dependency update in package.json.

  • Middleware:
    • Adds middleware.ts to track AI coding agents using 2027-track package.
    • Detects agents via Accept: text/markdown header and specific user-agents.
    • Configures route matching to exclude static assets and images.
  • Dependencies:
    • Adds 2027-track to dependencies in package.json.

This description was created by Ellipsis for 6767081. You can customize this summary. It will automatically update as commits are pushed.

@vercel
Copy link

vercel bot commented Feb 5, 2026

@caffeinum is attempting to deploy a commit to the langfuse Team on Vercel.

A member of the Team first needs to authorize it.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Feb 5, 2026
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@dosubot dosubot bot added the enhancement New feature or request label Feb 5, 2026
@caffeinum caffeinum marked this pull request as draft February 5, 2026 18:10
@caffeinum caffeinum changed the title Improve AI analytics middleware: timeout + kill switch feat: add middleware to track AI coding agent traffic Feb 5, 2026
@caffeinum caffeinum force-pushed the ai-analytics-improvements branch from 4a0b170 to 6767081 Compare February 6, 2026 00:05
@caffeinum caffeinum marked this pull request as ready for review February 6, 2026 00:05
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Feb 6, 2026
@caffeinum caffeinum force-pushed the ai-analytics-improvements branch 2 times, most recently from a9387c7 to 84affc6 Compare February 6, 2026 02:53
@caffeinum caffeinum force-pushed the ai-analytics-improvements branch from 84affc6 to 242b4ca Compare February 6, 2026 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants