Step 1: Create an API Key
Create an API key in Promptwatch. Go to Settings → API Keys in your Promptwatch dashboard.
Step 2: Create a Worker
Create a new Cloudflare Worker in your account:
- Go to your Cloudflare dashboard → Workers & Pages
- Click Create Worker
- Give it a name (e.g.,
promptwatch-analytics)
- Replace the default code with the script below
Step 3: Worker Script
Copy the following script and paste it into the Cloudflare Worker editor:
/**
* Cloudflare Worker - stream page views to analytics server
*
* Sends JSON in the expected crawler logs schema.
* Content type filtering is handled server-side.
*/
export default {
async fetch(request, env, ctx) {
const { pathname, search } = new URL(request.url);
// 1. Make the upstream request (cache/origin)
const response = await fetch(request);
const contentType = response.headers.get("content-type")?.split(";")[0];
const queryString = search.startsWith("?") && search.length > 1 ? search.slice(1) : null;
// 2. Build the log object
const log = {
timestamp: new Date().toISOString(),
status_code: response.status,
request_method: request.method,
request_path: pathname,
query_string: queryString,
content_type: contentType,
client_ip: request.headers.get("CF-Connecting-IP") || null,
hostname: request.headers.get("Host") || null,
user_agent: request.headers.get("User-Agent") || null,
referrer: request.headers.get("Referer") || null,
};
// 3. Fire-and-forget upload - never delays the visitor
ctx.waitUntil(
fetch(env.ANALYTICS_ENDPOINT, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-Key": env.ANALYTICS_KEY,
},
body: JSON.stringify(log),
}).catch(() => {}), // swallow network errors
);
return response;
},
};
Step 4: Environment Variables
In your Worker settings, add these environment variables:
ANALYTICS_ENDPOINT=https://logs.promptwatch.com/event
ANALYTICS_KEY=YOUR_API_KEY
Replace YOUR_API_KEY with your actual API key.
Step 5: Deploy to a Route
Deploy your Worker to a route:
- Go to your domain’s Workers Routes in Cloudflare
- Add a new route:
yourdomain.com/*
- Select your Worker from the dropdown
- Save and deploy
The Worker will intercept all requests to your domain. It transparently passes through all traffic while logging HTML page views to Promptwatch.
Step 6: Finish
Once deployed, your Worker will automatically send crawler logs to Promptwatch.