AI
Count AI crawlers
See which AI bots read your pages, for training, AI search or an assistant answering someone.
AI crawlers such as GPTBot, ClaudeBot and PerplexityBot never run the tracking script, so they are counted where your pages are served. Your server, edge or middleware sends each bot request's user agent and path to EarnedStats with your site's API key; the AI traffic page then shows hits per day, each bot and its purpose, and the paths it reads most.
1. Get the key
Copy the site's API key from Settings, Integrations, Server side API. Keep it on the server, in an environment variable.
2. Send bot requests
Send only requests whose user agent names a bot (the snippets check that first), without waiting for the answer, so your pages are never slowed down.
// middleware.ts
import { NextResponse, type NextRequest, type NextFetchEvent } from 'next/server';
const BOTS = /GPTBot|OAI-SearchBot|ChatGPT-User|ClaudeBot|Claude-User|Claude-SearchBot|PerplexityBot|Perplexity-User|Google-Extended|Applebot-Extended|meta-externalagent|Bytespider|CCBot|DuckAssistBot|MistralAI-User/i;
export function middleware(req: NextRequest, event: NextFetchEvent) {
const ua = req.headers.get('user-agent') ?? '';
if (BOTS.test(ua)) {
event.waitUntil(fetch('https://earnedstats.com/api/v1/crawlers', {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${process.env.EARNEDSTATS_API_KEY}` },
body: JSON.stringify({ ua, path: req.nextUrl.pathname }),
}).catch(() => {}));
}
return NextResponse.next();
}// In front of your site at the edge
const BOTS = /GPTBot|OAI-SearchBot|ChatGPT-User|ClaudeBot|Claude-User|PerplexityBot|Perplexity-User|Google-Extended|meta-externalagent|CCBot/i;
export default {
async fetch(request, env, ctx) {
const ua = request.headers.get('user-agent') ?? '';
if (BOTS.test(ua)) {
ctx.waitUntil(fetch('https://earnedstats.com/api/v1/crawlers', {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${env.EARNEDSTATS_API_KEY}` },
body: JSON.stringify({ ua, path: new URL(request.url).pathname }),
}).catch(() => {}));
}
return fetch(request);
},
};// Express or any Node server
app.use((req, res, next) => {
const ua = req.get('user-agent') || '';
if (/GPTBot|ClaudeBot|PerplexityBot|ChatGPT-User|OAI-SearchBot|Google-Extended|CCBot/i.test(ua)) {
fetch('https://earnedstats.com/api/v1/crawlers', {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${process.env.EARNEDSTATS_API_KEY}` },
body: JSON.stringify({ ua, path: req.path }),
}).catch(() => {});
}
next();
});Batch many hits in one request with { "hits": [{ "ua": "...", "path": "/..." }] }, up to 100 at a time, for example from a log reader. Requests from browsers and unknown bots are ignored, and query strings are dropped.
Bots we recognise
| Purpose | Bots |
|---|---|
| Training (collects pages to train models) | GPTBot, ClaudeBot, Google-Extended, Applebot-Extended, Meta-ExternalAgent, Bytespider, CCBot |
| Search (indexes pages so an AI search product can cite them) | OAI-SearchBot, Claude-SearchBot, PerplexityBot |
| User-triggered (reads a page because someone asked) | ChatGPT-User, Claude-User, Perplexity-User, DuckAssistBot, MistralAI-User |
Decide which of them may read your site in your robots.txt, by name.