A browser analytics script counts the browsers that ran it. Most crawlers fetch the HTML, read the markup and leave, so the Pug Web SDK, like every other client-side tag, never sees them. We covered the consequence in what AI crawlers do to your analytics: the requests land in your server logs and your bandwidth bill, and nowhere in your analytics.
Logs tell you how many requests arrived. They don’t tell you which company sent them, why, or whether the request
came from that company at all. So Pug has a second way in: @pug-sh/crawlers, an MIT package with no
dependencies that runs in your site’s own request handling and reports each request from a known crawler. Version
0.1.3 is on npm, and this site reports its own crawlers with it.
Add it to your request handling
npm install @pug-sh/crawlers
It needs your project’s private key (prv_…), kept on the server in an environment
variable, since the same key can read your analytics. On Cloudflare Pages, this is the middleware pug.sh runs,
without its types:
// functions/_middleware.ts
import { crawlers } from '@pug-sh/crawlers'
export async function onRequest(context) {
const response = await context.next()
crawlers({ apiKey: context.env.PUG_PRIVATE_KEY })
.track(context.request, response, context)
return response
} context.next() serves the page first, and passing context lets the platform keep the
function alive until the report is sent, so a crawler’s response never waits on Pug. A Worker passes the
ctx from its fetch handler the same way. Cloudflare serves static assets without running a Worker
unless run_worker_first is on, so turn it on, or crawlers’ page fetches never reach track.
Express takes one line:
app.use(crawlers({ apiKey: process.env.PUG_PRIVATE_KEY }).express()) Next.js runs it in proxy.ts:
const pug = crawlers({ apiKey: process.env.PUG_PRIVATE_KEY })
export function proxy(request: NextRequest, event: NextFetchEvent) {
pug.track(request, undefined, event)
return NextResponse.next()
} The proxy runs before the page, so these reports carry no status code. Hono, Bun, Deno and plain Node have adapters too, and any other server can send the same report as one POST, which the AI crawlers docs show with curl.
Five reasons a crawler fetches a page
Knowing that OpenAI read your pricing page is half the answer. OpenAI runs three crawlers, and each means something
different: ChatGPT-User fetches a page because someone asked ChatGPT a question, OAI-SearchBot builds the index
ChatGPT search answers from, and GPTBot collects pages to train models. So Pug files every crawler under one of
five purposes, in $crawler_category:
answers: fetched live because a person asked an AI product. ChatGPT-User, Claude-User, Perplexity-User.search: builds a search index that results and AI answers draw on. OAI-SearchBot, Googlebot, bingbot.training: collects pages to train models. GPTBot, ClaudeBot, CCBot.seo: crawls for an SEO tool. AhrefsBot, SemrushBot, MJ12bot.other: AI-related, but none of the above. OAI-AdsBot, GoogleOther.
The AI agents come from a table we keep by hand: 35 rows, each with its company, its purpose, the operator’s page that describes it and, where the operator publishes them, its IP ranges. No open list splits answers, search and training cleanly. ai.robots.txt has the right names, but 82 of its 208 entries give their purpose in free text, and Cloudflare Radar’s split is the cleanest but licensed for non-commercial use. Each row follows its operator’s own description, and the table lives in protobuf, crawlers.proto in the Pug repository, so the Go server and the npm package read the same rows:
CRAWLER_GPTBOT = 24 [
(token) = "GPTBot",
(company) = "OpenAI",
(category) = CATEGORY_TRAINING,
(ip_ranges) = "https://openai.com/gptbot.json",
(operator_page) = "https://developers.openai.com/api/docs/bots"
];
Search engines and SEO tools aren’t kept by hand. They are the 177 entries tagged search-engine and
the 550 tagged seo in crawler-user-agents, the MIT list Pug’s
bot detection already reads. They carry no company and no IP ranges.
A few rows are judgment calls, and they follow the operator. Applebot is search, since it powers
Spotlight, Siri and Safari, though Apple says its data may also train Apple’s models. Amazonbot is
training. Google’s and Apple’s training crawls arrive as plain Googlebot and Applebot
(Google-Extended and Applebot-Extended are robots.txt tokens that no request carries), so training
undercounts both companies.
Matching a user agent
A token matches as a whole word, in any case, and the longest match wins: Googlebot matches
Googlebot-Image/1.0 but not NotGooglebot. Only the first 512 bytes are read. Real
crawlers send under 300, and a few of the list’s entries are regular expressions that take about 5 ms to scan a
2 KB user agent, which a batch of 1,000 events would pay on every one.
The package runs the same match before it sends anything
(src/match.ts; the server’s copy is
internal/crawler/crawler.go), which is how your visitors’
requests stay on your server. The server matches again and has the last word: a user agent it doesn’t know is dropped as
unknown_agent, as happens when a package release knows a crawler your server doesn’t yet.
Telling GPTBot from a scraper using its name
Anyone can put GPTBot in a User-Agent header, and scrapers do. Most operators publish the IP ranges
their crawlers use, so Pug checks each request’s address against them: 27 of the 35 hand-kept rows can be checked.
Each request gets one of three verdicts in $crawler_verified:
verified: the address is inside the operator’s published ranges.failed: a public address outside them, which usually means an impostor.unchecked: the operator publishes no ranges (Meta, ByteDance and Baidu among them, and every list entry), or the address is private or shared, which means the package saw a proxy rather than the crawler.
The server fetches the 23 range files when it starts and every 6 hours after, at most 5 MB and 10 seconds each. A
failed fetch keeps the last good copy, so an operator’s outage can’t turn every request failed. The
checker is internal/crawlerips/crawlerips.go. The
address is used for the check and dropped: Pug’s events table has no IP column.
The check only means something because a crawler_request event needs a private key. With the public
key from a web page, anyone could report any address. That is also its cost: the private key can read your
analytics, and the package puts it in an edge function. A write-only key would fix that for every server SDK, and
Pug doesn’t have one yet.
In the dashboard, a crawler that fails its check gets a line under the crawler table, such as “38 requests claimed
to be GPTBot but failed OpenAI’s IP check”. When every one of a crawler’s requests fails, the cause is more likely
your setup: behind a CDN, your origin sees the CDN’s address. Return the CDN’s client-IP header from the
getIP option, or run the package at the edge, which also sees the requests a CDN answers from cache.
What leaves your server
Only requests whose user agent names a known crawler. For each one, the package sends:
{"events": [{
"eventId": "0193b2c4-8f1e-7a2b-9c3d-4e5f6a7b8c9d",
"occurTime": "2026-11-14T09:30:00Z",
"kind": "crawler_request",
"client": {"userAgent": "... GPTBot/1.2 ...", "ip": "20.171.207.5"},
"autoProperties": {
"$url": {"stringValue": "https://example.com/pricing"},
"$status_code": {"intValue": 200}
}
}]}
The URL goes without its query string or fragment, the address only for the check, and the status code when the
adapter can see one. It never sends cookies, other headers, bodies, or requests other than GET and HEAD. Crawlers
fetch scripts, styles, images and fonts too, which adds volume and nothing worth reading, so those stay home unless
you set includeAssets.
Each report is one POST, sent after the response, with a 2-second timeout. A network error, a 409 or a server error
gets one retry with the same eventId and occurTime, so Pug stores the request once. The
package never throws, so it can’t break your site, and it logs each kind of failure once per process, including
reports Pug drops, with the reason.
Version 0.1.1 fetched with redirect: 'error', so that a redirect could never hand the key to another
host. Cloudflare Workers throw on that option, so every report from a Worker or a Pages Function failed before it
left. 0.1.2 uses redirect: 'manual': a 3xx still isn’t followed, and it comes back as a refusal the
package logs. The change is
commit 170bd39.
What you see in Pug
Each report becomes a crawler_request event with $bot set, which every insight already
leaves out, so your funnels, retention and visitor counts don’t move. A crawler gets a distinct ID such as
crawler-gptbot and one session per UTC day, and it is never listed as a person, even with bots
included.
The Overview has an AI and search crawlers section built from those events: a tile per purpose, a tile of the visits ChatGPT, Perplexity, Claude and Gemini sent you, a table of who reads your site grouped by company, and the pages crawlers read, with tabs for the ones AI answers fetched, the ones training crawlers took, and the ones that returned errors.
Otherwise they are ordinary events, so any insight can ask its own question: break
crawler_request down by $crawler_category for requests over time, rank
$crawler_company with Top K, or filter $status_code to 404 for the pages crawlers can’t
find. Over the MCP server, an agent can ask which AI products read /pricing last
week.
What it misses
- Sites with no server code to hook, such as GitHub Pages or a storage bucket.
- Scrapers with a browser’s user agent. They look like people to the package, and they don’t run the Web SDK either.
- Google’s and Apple’s training crawls, which arrive as Googlebot and Applebot.
- Requests a CDN answers from cache, when the package runs at your origin.
- A real crawler on new addresses. It reads
faileduntil its operator publishes them and the next refresh picks them up.
Crawler requests also count toward your Pug usage like any event. A busy content site can get more of them than
page views, and SEO tools are among the busiest crawlers, so seo is the first category to leave out:
crawlers({
apiKey: process.env.PUG_PRIVATE_KEY,
categories: ['answers', 'search', 'training'],
}) The package is MIT-licensed and developed in the open at pug-sh/sdk-crawlers, and the AI crawlers docs cover every adapter and option.