Proxy setups
Proxy the Loogika script with Astro middleware
In Astro you proxy Loogika with one middleware file. The middleware catches every request under a path such as /stats/ and forwards it to app.loogika.com. Astro must render on the server for the middleware to run on those requests.
Last updated .
What do you need before you start?
You need your Loogika site ID, a server adapter and Astro in version 5 or later. The examples on this page use the @astrojs/node adapter.
A fully static Astro site cannot proxy requests, because no server code runs when a visitor asks for a page. Use the proxy feature of your host for a static site, for example the Vercel setup. The proxy guide explains the idea behind the proxy.
How do you turn on server rendering in Astro?
Set output to 'server' and add an adapter in astro.config.mjs. Install the Node adapter first:
npm install @astrojs/node
// astro.config.mjs
import { defineConfig } from 'astro/config';
import node from '@astrojs/node';
export default defineConfig({
output: 'server',
adapter: node({
mode: 'standalone',
}),
});
Astro 5 removed the 'hybrid' output. With 'server' the middleware runs on every request. That includes the /stats/ paths, for which no page exists.
What does the Loogika middleware look like?
The middleware lives in src/middleware.ts, directly in the src folder. It rewrites /stats/ to /api/, copies the browser's headers and adds the visitor's IP address:
// src/middleware.ts
import { defineMiddleware } from 'astro:middleware';
const LOOGIKA_HOST = 'https://app.loogika.com';
export const onRequest = defineMiddleware(async (context, next) => {
const url = new URL(context.request.url);
if (url.pathname.startsWith('/stats/')) {
// clientAddress is the visitor IP as your adapter reports it
return proxyToLoogika(context.request, url, context.clientAddress);
}
return next();
});
async function proxyToLoogika(request: Request, url: URL, clientIp: string): Promise<Response> {
try {
// /stats/* becomes /api/*, and the query string is kept
const target = `${LOOGIKA_HOST}${url.pathname.replace('/stats/', '/api/')}${url.search}`;
// Forward the browser's headers and set the visitor IP
const proxyHeaders = new Headers(request.headers);
proxyHeaders.delete('host');
proxyHeaders.set('X-Forwarded-For', clientIp);
const hasBody = request.method !== 'GET' && request.method !== 'HEAD';
const response = await fetch(target, {
method: request.method,
headers: proxyHeaders,
body: hasBody ? await request.text() : undefined,
});
// fetch() has already decoded the body, so drop the encoding headers
const headers = new Headers(response.headers);
headers.delete('content-encoding');
headers.delete('content-length');
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers,
});
} catch (error) {
console.error('Loogika proxy error:', error);
return new Response('Analytics proxy error', { status: 500 });
}
}
The prefix match covers every address the Loogika script calls, so you list no single routes. In a JavaScript project, put the same code in src/middleware.js and remove the type annotations.
Why does the middleware copy all the browser's headers?
Every real browser sends headers such as Accept, Accept-Language and Accept-Encoding, and the bot detection in Loogika looks at them. A proxy that forwards only the user agent makes each visit look like an automated request.
The visitor's IP address comes from context.clientAddress, which your adapter fills in. Never replace a missing address with a fixed value such as 127.0.0.1. Loogika would then see one shared address, merge your visitors into one and place them all in the same location.
If another reverse proxy sits in front of your Astro server, check what clientAddress contains. Configure the adapter or that proxy so the value is the visitor's address and not the proxy's.
How do you load the script through the proxy?
Add the script tag to the head of your layout and point it at the proxied path:
---
// src/layouts/Layout.astro
const { title } = Astro.props;
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>{title}</title>
<script is:inline src="/stats/script.js?siteId=YOUR_SITE_ID" defer></script>
</head>
<body>
<slot />
</body>
</html>
With is:inline, Astro leaves the tag out of its bundle and prints it as an ordinary external script. Keep the file name script.js. The script works out where its events go by cutting its own address at /script.js.
Then switch on First-Party Proxy under Site Settings, General in Loogika. The switch tells Loogika to trust the IP address that your middleware sends.
How do you check that the Astro proxy works?
Start the site with npm run dev and open it with the browser's developer tools. In the Network tab, the script and the requests to /stats/track should go to your own domain and return status 200.
Your visit should then appear in the realtime view in Loogika with your real location. If it shows the location of your server instead, the IP address is not reaching Loogika.
Why does the Astro middleware not run?
| Cause | Fix |
|---|---|
| The site builds as static pages | Set output: 'server' and an adapter in astro.config.mjs |
| The middleware file is in a subfolder | Move it to src/middleware.ts |
| The adapter is not installed | Run npm install @astrojs/node, or install the adapter for your host |
On Vercel you use the @astrojs/vercel adapter and on Netlify the @astrojs/netlify adapter, both with output: 'server'. The proxy troubleshooting page covers problems that are not specific to Astro.