Docs·Proxy guide
Proxy with a Cloudflare Worker
If your site is behind Cloudflare, a Worker on the route www.example.com/sp/* serves Spreelytics from /sp/. This guide uses /sp/. Any path works if nothing else on your site uses it and it doesn't contain words like "analytics" or "tracking", which blockers look for.
Create the Worker
Create a Worker with this code and give it the route www.example.com/sp/*:
const SCRIPTS = "https://cdn.spreelytics.com/cdn/sdk/";
const API = "https://sdk.spreelytics.com/1.0/";
export default {
async fetch(request) {
const url = new URL(request.url);
const path = url.pathname.replace(/^\/sp\//, "");
if (path.startsWith("js/")) {
return fetch(SCRIPTS + path.slice(3) + url.search);
}
const proxied = new Request(API + path + url.search, request);
proxied.headers.delete("Cookie");
proxied.headers.set("X-Forwarded-For", request.headers.get("CF-Connecting-IP") || "");
return fetch(proxied);
},
};
Point the script at your proxy
In your Spreelytics snippet, load the script through /sp/js/ and add api_host:
<script src="/sp/js/spreelytics-loader.js"></script>
<script>
Spreelytics.init({
client_key: "YOUR_CLIENT_KEY",
api_host: "https://www.example.com/sp",
});
</script>
Check it works
Open your site with the browser's Network tab open and filtered by /sp/. The script files and
requests like track/page should come from your own domain, with status 200. In Spreelytics,
Settings → Tracking script says "Last seen just now" and Realtime shows you in your own country.
- Wrong country, or
200and nothing arrives: the proxy isn't passing the visitor's IP address inX-Forwarded-For. See The visitor's IP address below. 401: theOriginandRefererheaders didn't arrive, or your domain isn't in Allowed domains.
The visitor's IP address
The Worker copies CF-Connecting-IP, the visitor's address as Cloudflare sees it, into X-Forwarded-For.
Related
- Proxy overview: the other ways to set it up.
- Filter traffic: which traffic Spreelytics drops.