Cloudflare Workers
Use a Cloudflare Worker to stream HTML page views to AI Search Index. Works on any Cloudflare plan.
Works on Free, Pro, Business, and Enterprise plans
Endpoint
POST
/api/v1/logs/customURL: https://aisearchindex.com/api/v1/logs/custom
Headers:
x-api-key: bot_YOUR_BOT_TRACKING_KEYWorker Script
Create a new Worker with the following code:
worker.js
/**
* Cloudflare Worker – stream HTML page views to AI Search Index
* Excludes common static assets and only logs text/html + text/plain (robots.txt).
*/
const IGNORE_EXT = /\.(?:png|jpe?g|gif|svg|webp|ico|mp4|webm|mp3|wav|ogg|css|js|mjs|json|map|pdf|zip|rar|7z|tar|gz|woff2?|ttf|eot)$/i;
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
// Skip static assets
if (IGNORE_EXT.test(url.pathname)) {
return fetch(request);
}
const response = await fetch(request);
const contentType = response.headers.get("content-type")?.split(";")[0];
// Only log HTML and plain text (robots.txt) responses
if (!contentType || !["text/html", "text/plain"].includes(contentType) || response.status >= 500) {
return response;
}
const log = {
timestamp: Date.now(),
method: request.method,
host: request.headers.get("Host") || url.hostname,
path: url.pathname,
status_code: response.status,
ip: request.headers.get("CF-Connecting-IP") || "",
user_agent: request.headers.get("User-Agent") || "",
referer: request.headers.get("Referer") || null,
query_params: Object.fromEntries(url.searchParams.entries()),
content_type: contentType,
};
// Send log asynchronously (non-blocking)
ctx.waitUntil(
fetch(env.AIS_ENDPOINT, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": env.AIS_BOT_KEY,
},
body: JSON.stringify(log),
}).catch(() => {}) // Fail silently
);
return response;
},
};Environment Variables
Set these environment variables in your Worker settings:
| Variable | Value |
|---|---|
| AIS_ENDPOINT | https://aisearchindex.com/api/v1/logs/custom |
| AIS_BOT_KEY | Your Bot Tracking Key (e.g., bot_xxx...) |
Deployment Steps
- 1
Create Worker
Go to Cloudflare Dashboard → Workers & Pages → Create Application → Create Worker
- 2
Add Code
Replace the default code with the Worker script above
- 3
Set Environment Variables
Go to Settings → Variables → Add the AIS_ENDPOINT and AIS_BOT_KEY variables
- 4
Deploy
Click "Save and Deploy"
- 5
Add Route
Go to your website in Cloudflare → Workers Routes → Add route:
yourdomain.com/*→ Select your Worker
Alternative: Using Wrangler CLI
wrangler.toml
name = "ai-search-index"
main = "worker.js"
compatibility_date = "2024-01-01"
[vars]
AIS_ENDPOINT = "https://aisearchindex.com/api/v1/logs/custom"
# Add AIS_BOT_KEY as a secret:
# wrangler secret put AIS_BOT_KEYTerminal
# Deploy the worker
wrangler deploy
# Add your Bot Tracking Key as a secret
wrangler secret put AIS_BOT_KEY