Source: https://usedataowl.com/docs/cloudflare-proxy
Markdown source: https://usedataowl.com/docs/cloudflare-proxy.md
Description: Route the analytics script and event ingestion through a Cloudflare Worker on your own domain to get past privacy browser blocks.

# Proxy Web Analytics with Cloudflare Workers to Bypass Ad Blockers

Add this to `worker.js`. Replace `/_ds` with a path of your own.

```js title="worker.js"
// Cloudflare Worker — route: example.com/_ds/*
const UPSTREAM = "https://usedataowl.com"

export default {
  async fetch(request) {
    const url = new URL(request.url)
    const upstreamPath = url.pathname.replace(/^\/_ds/, "")
    const headers = new Headers(request.headers)
    headers.set("x-datasense-ip", request.headers.get("cf-connecting-ip") ?? "")
    headers.set("x-datasense-country", request.cf?.country ?? "")
    headers.set("x-datasense-region", encodeURIComponent(request.cf?.region ?? ""))
    headers.set("x-datasense-city", encodeURIComponent(request.cf?.city ?? ""))
    headers.delete("cookie")

    return fetch(UPSTREAM + upstreamPath + url.search, {
      method: request.method,
      headers,
      // Bytes, not text: session replay chunks are gzipped.
      body: request.method === "POST" ? await request.arrayBuffer() : undefined,
    })
  },
}
```

Then load the script from your own domain:

```html
<script
  defer
  data-website-id="YOUR_SITE_ID"
  src="/_ds/js/script.cookies.js"
></script>
```

## Verify

Open your site with an ad blocker on. In the browser's network tab, `/_ds/js/script.cookies.js` and `/_ds/api/e` load from your own domain, and the visit shows up on the **Realtime** page.
