Sufyaan Studio Engineering3 min read

How to Track Traffic and APIs on Cloudflare Workers

A developer guide to server-side analytics, subrequest tracking, and HTMLRewriter script injection on Cloudflare Workers and Pages.

Direct Tutorial Summary
By Sufyaan Studio EngineeringVerified August 2026

To track traffic on Cloudflare Workers, send an asynchronous POST subrequest to https://analytics-collect.sufyaanstudio.workers.dev/api/send inside ctx.waitUntil(), or use HTMLRewriter to inject the 1.15 KB tracker into outgoing HTML responses.

KEY TAKEAWAYS
  • Step 1: Non-Blocking Subrequest via ctx.waitUntil
  • Step 2: Add Website UUID Secret in Wrangler
  • Step 3: Monitor Ingestion in Live Realtime Dashboard

Step 1: Non-Blocking Subrequest via ctx.waitUntil

In your Cloudflare Worker fetch handler, invoke ctx.waitUntil() to dispatch the pageview without adding any response latency.

CODE STEP 1
export default { async fetch(request: Request, env: any, ctx: ExecutionContext): Promise<Response> { const response = await fetch(request); const url = new URL(request.url); ctx.waitUntil( fetch('https://analytics-collect.sufyaanstudio.workers.dev/api/send', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ w: env.ANALYTICS_WEBSITE_ID, u: url.pathname + url.search, r: request.headers.get('Referer') || '', }), }) ); return response; } };

Step 2: Add Website UUID Secret in Wrangler

Save your website UUID to your Cloudflare Worker configuration.

CODE STEP 2
npx wrangler secret put ANALYTICS_WEBSITE_ID

Step 3: Monitor Ingestion in Live Realtime Dashboard

Open your Analytics dashboard to observe real-time pageviews and geographic distribution instantly.

FAQ & VERIFICATION

Guide FAQ

Start tracking your websites with confidence.

Create an account in 30 seconds. Up to 25,000 events free every month.

analytics