Proxy setups
Cloudflare Workers proxy for Loogika
A Cloudflare Worker can serve Loogika from your own domain without a server of your own. The Worker forwards every request under /stats/ to https://app.loogika.com/api/ and adds the visitor's IP address. You need a domain on Cloudflare, a Worker with a route and one switch in Site Settings.
Last updated .
What do you need before you start?
You need a Cloudflare account that holds your domain, with Workers enabled. You also need your Loogika site ID from Site Settings, under the Tracking Script tab. The proxy guide explains the idea behind the setup.
How do you create the Worker?
- Log in to the Cloudflare dashboard and go to Workers & Pages, then Create application, then Create Worker.
- Name the Worker, for example
loogika-proxy, and click Deploy. - Replace the default code of the Worker with the code below.
- Click Save and Deploy.
// Cloudflare Worker: Loogika proxy
// Forwards /stats/* on your domain to /api/* on app.loogika.com.
const LOOGIKA_HOST = 'https://app.loogika.com';
export default {
async fetch(request) {
const url = new URL(request.url);
// Everything else goes to your site as before
if (!url.pathname.startsWith('/stats/')) {
return fetch(request);
}
// /stats/track becomes /api/track, and the query string is kept
const target = new URL(url.pathname.replace('/stats/', '/api/'), LOOGIKA_HOST);
target.search = url.search;
// Cloudflare puts the visitor's IP address in CF-Connecting-IP
const clientIp = request.headers.get('CF-Connecting-IP') || '0.0.0.0';
// Keep all browser headers and add the IP headers
const proxyHeaders = new Headers(request.headers);
proxyHeaders.set('X-Real-IP', clientIp);
proxyHeaders.set('X-Forwarded-For', clientIp);
return fetch(target.toString(), {
method: request.method,
headers: proxyHeaders,
body: request.method !== 'GET' && request.method !== 'HEAD' ? request.body : undefined,
});
},
};
The code uses the ES module form with export default, which current Workers use. It forwards every path under /stats/, so session replay and identify() work as well. The generic setup page lists the paths one by one.
How do you connect the Worker to your domain?
Add a route, so the Worker runs only for requests to /stats/ and not for the rest of your site.
- Go to Workers & Pages, open your Worker and choose Triggers.
- Click Add route.
- Enter
yourdomain.com/stats/*as the Route and pick your domain as the Zone. - Click Save.
The DNS record of the domain must go through Cloudflare's proxy, shown as an orange cloud. A Worker route does not run on a record that bypasses the proxy.
How do you change the script tag?
Load the script from /stats/script.js on your own domain. Keep the file name script.js, because the script uses it to find the address for its events.
<script src="https://yourdomain.com/stats/script.js?siteId=YOUR_SITE_ID" defer></script>
Then open Site Settings in Loogika, go to General and turn on First-Party Proxy. The switch tells Loogika to read the visitor's IP address from the headers the Worker sets.
How do you check that the Worker works?
Open your site with the browser's developer tools on the Network tab. The script and the POST requests to /stats/track should go to your own domain and return status 200.
Then open the realtime view in Loogika from two different networks, for example your office and your phone. Two visitors in two places mean the Worker passes on the IP address correctly.
Can you test the Worker on your own machine?
Yes, with Wrangler, the command line tool from Cloudflare. It runs the Worker locally and deploys it from your terminal.
npm install -g wrangler
wrangler login
wrangler init loogika-proxy
wrangler dev
wrangler deploy
Why does the Worker proxy not work?
| Symptom | Cause and fix |
|---|---|
Requests to /stats/ do not reach the Worker | Check that the route is yourdomain.com/stats/*, that the zone is your domain and that the DNS record shows the orange cloud |
| Every visitor has the same location | Check that the code sets X-Forwarded-For from CF-Connecting-IP, and switch on First-Party Proxy in Site Settings |
| The Worker fails to deploy | Check the code for syntax errors in the editor, or run it locally with wrangler dev |
The proxy troubleshooting page covers problems that are not specific to Cloudflare.