Skip to main content

Cloudflare Worker Setup

The Cloudflare Worker integration is the recommended way to set up Server-Side Tracking. It runs at the edge on Cloudflare's network, capturing every request before it reaches your origin server - including cached responses.

Prerequisites​

  • A website proxied through Cloudflare (orange cloud enabled)
  • A Travatar account with an App ID and API Token
  • Access to Cloudflare Dashboard → Workers & Pages

Setup Steps​

1. Get Your Credentials​

  1. Log in to app.travatar.ai
  2. Go to Settings → Server Tracking
  3. Copy your App ID and generate an API Token (save it - it's shown only once)

2. Create the Worker​

  1. Go to Cloudflare Dashboard → Workers & Pages
  2. Click Create Worker
  3. Name it travatar-sst (or any name you prefer)
  4. Replace the default code with the following:
const WEBHOOK_URL = "https://log-tracker.travatar.ai/webhook";

export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);

const metadata = {
timestamp: new Date().toISOString(),
app_id: env.TRAVATAR_APP_ID,

// Request info
method: request.method,
url: request.url,
path: url.pathname,
query: url.search,
host: url.hostname,
origin: url.origin,

// All headers
headers: Object.fromEntries(request.headers),

// Referral & navigation
referer: request.headers.get("referer"),
origin_header: request.headers.get("origin"),

// Client identification
user_agent: request.headers.get("user-agent"),
accept_language: request.headers.get("accept-language"),
accept: request.headers.get("accept"),

// Client IP
client_ip: request.headers.get("cf-connecting-ip"),
x_forwarded_for: request.headers.get("x-forwarded-for"),

// Cloudflare headers
cf_ipcountry: request.headers.get("cf-ipcountry"),
cf_ray: request.headers.get("cf-ray"),
cf_visitor: request.headers.get("cf-visitor"),

// Cloudflare request properties (geo, network, bot detection, TLS)
cf: request.cf
? {
country: request.cf.country,
city: request.cf.city,
continent: request.cf.continent,
region: request.cf.region,
regionCode: request.cf.regionCode,
latitude: request.cf.latitude,
longitude: request.cf.longitude,
postalCode: request.cf.postalCode,
timezone: request.cf.timezone,
isEUCountry: request.cf.isEUCountry,
asn: request.cf.asn,
asOrganization: request.cf.asOrganization,
colo: request.cf.colo,
tlsVersion: request.cf.tlsVersion,
httpProtocol: request.cf.httpProtocol,
botManagement: request.cf.botManagement,
}
: null,

// Security hints
sec_ch_ua: request.headers.get("sec-ch-ua"),
sec_ch_ua_mobile: request.headers.get("sec-ch-ua-mobile"),
sec_ch_ua_platform: request.headers.get("sec-ch-ua-platform"),
sec_fetch_dest: request.headers.get("sec-fetch-dest"),
sec_fetch_mode: request.headers.get("sec-fetch-mode"),
sec_fetch_site: request.headers.get("sec-fetch-site"),
dnt: request.headers.get("dnt"),
};

// Send metadata to Travatar (non-blocking)
ctx.waitUntil(
fetch(WEBHOOK_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-TRAVATAR-SST-TOKEN": env.TRAVATAR_SST_TOKEN,
"X-TRAVATAR-SST-SOURCE": "cf_worker",
},
body: JSON.stringify(metadata),
})
.then((res) => {
if (!res.ok) {
console.error(`Webhook error: ${res.status} ${res.statusText}`);
}
})
.catch((err) => {
console.error(`Webhook failed: ${err.message}`);
})
);

// Forward the original request to origin
const response = await fetch(request);

return response;
},
};
  1. Click Deploy

3. Add Environment Variables​

  1. In the Worker settings, go to Settings → Variables
  2. Add two environment variables:
VariableValue
TRAVATAR_APP_IDYour App ID from Settings → Server Tracking
TRAVATAR_SST_TOKENYour API Token from Settings → Server Tracking
  1. Click Encrypt for TRAVATAR_SST_TOKEN to keep it secure
  2. Click Save

4. Add a Route​

  1. Go to your domain in Cloudflare → Workers Routes
  2. Click Add Route
  3. Configure:
    • Route: yourdomain.com/*
    • Worker: travatar-sst
  4. Click Save

5. Verify​

  1. Visit your website in a browser
  2. Go to app.travatar.ai → AI Traffic or Non-Human Traffic
  3. You should see your visit appear within 1-2 minutes

How It Works​

Visitor → Cloudflare Edge → Worker
├── Forward to origin → Response to visitor
└── POST to Travatar (non-blocking, fire-and-forget)

The Worker intercepts every request at the Cloudflare edge:

  1. It passes the request through to your origin server normally
  2. In parallel, it sends request metadata to Travatar
  3. The visitor receives the response with zero added latency

Cloudflare-Specific Data​

The Cloudflare Worker captures rich data not available from other integrations:

  • Geo - country, city, region, coordinates, timezone, EU status
  • Network - ASN, organization, Cloudflare colo (data center)
  • Bot detection - Cloudflare Bot Management scores (if enabled on your plan)
  • TLS - version, cipher, protocol
  • Client hints - sec-ch-ua, platform, mobile detection

Filtering Requests​

You can modify the Worker to skip static assets and reduce invocations:

// Skip static assets
const skip = ["/favicon.ico", "/_next/", "/static/", ".css", ".js", ".png", ".jpg", ".svg", ".woff"];
if (skip.some((s) => url.pathname.startsWith(s) || url.pathname.endsWith(s))) {
return fetch(request);
}

Place this at the beginning of the fetch function, right after const url = new URL(request.url).

Troubleshooting​

No data in dashboard​

  1. Check that the Worker route matches your domain (yourdomain.com/*)
  2. Verify environment variables are set correctly
  3. Check Worker logs in Cloudflare Dashboard → Workers → your worker → Logs

Worker errors in logs​

  • Webhook error: 401 - Check that TRAVATAR_SST_TOKEN matches the token from Settings → Server Tracking
  • Webhook error: 404 - Check that TRAVATAR_APP_ID matches your App ID exactly
  • Webhook failed: ... - The Travatar endpoint may be temporarily unreachable. Events are fire-and-forget, so this is harmless.

High Worker invocations​

The Worker runs on every request, including static assets. If you want to reduce invocations, add path filtering (see above). Cloudflare's free plan includes 100,000 requests/day.