Proxy setups
Proxy the Loogika script with Next.js rewrites
In Next.js you proxy Loogika with the rewrites function in next.config.js. Each rewrite maps a path on your own domain to the matching path on app.loogika.com. The same configuration works with the App Router and the Pages Router.
Last updated .
What do you need before you start?
You need Next.js 12 or later and your Loogika site ID. The site ID sits in the script tag under Site Settings in Loogika, on the Tracking Script tab.
The proxy guide explains why a proxy on your own domain helps against ad blockers. The page you are reading covers the Next.js configuration only.
Which rewrites does Loogika need in next.config.js?
Loogika needs seven rewrites, one for each address the script can call. Add the rewrites function below to next.config.js:
// next.config.js
/** @type {import('next').NextConfig} */
const loogikaHost = 'https://app.loogika.com';
const nextConfig = {
async rewrites() {
return [
// Scripts
{
source: '/stats/script.js',
destination: `${loogikaHost}/api/script.js`,
},
{
source: '/stats/replay.js',
destination: `${loogikaHost}/api/replay.js`,
},
// Requests the script sends
{
source: '/stats/track',
destination: `${loogikaHost}/api/track`,
},
{
source: '/stats/identify',
destination: `${loogikaHost}/api/identify`,
},
{
source: '/stats/session-replay/record/:siteId',
destination: `${loogikaHost}/api/session-replay/record/:siteId`,
},
{
source: '/stats/site/tracking-config/:siteId',
destination: `${loogikaHost}/api/site/tracking-config/:siteId`,
},
{
source: '/stats/site/:siteId/feature-flags/evaluate',
destination: `${loogikaHost}/api/site/:siteId/feature-flags/evaluate`,
},
];
},
};
module.exports = nextConfig;
In a next.config.mjs file the list is the same. Replace the last line with export default nextConfig;.
You can swap /stats for another path, as long as the rewrites and the script tag use the same one. Keep the file name script.js. The script works out where its events go by cutting its own address at /script.js. With another file name, every request ends up at a wrong address.
What happens if you only proxy script.js and track?
Pageviews still arrive, but the script cannot read your settings. It then runs on its built-in defaults and ignores what you chose in Site Settings.
With the defaults, Loogika tracks pageviews, route changes in single-page apps, query strings and outbound links. Session replay, error tracking and Web Vitals stay off, and so do button clicks, copy tracking and form tracking. Calls to identify() and setTraits() also fail, because they need the /stats/identify rewrite.
How do you load the script through the proxy?
Point the script at the proxied path with the Script component from next/script. In the App Router you add it to the root layout:
// app/layout.tsx
import Script from 'next/script';
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
{children}
<Script
src="/stats/script.js?siteId=YOUR_SITE_ID"
strategy="afterInteractive"
/>
</body>
</html>
);
}
In the Pages Router the same component goes into pages/_app.tsx:
// pages/_app.tsx
import Script from 'next/script';
import type { AppProps } from 'next/app';
function MyApp({ Component, pageProps }: AppProps) {
return (
<>
<Component {...pageProps} />
<Script
src="/stats/script.js?siteId=YOUR_SITE_ID"
strategy="afterInteractive"
/>
</>
);
}
export default MyApp;
Replace YOUR_SITE_ID with your own site ID. Restart the development server after you change next.config.js, because Next.js reads the rewrites only at start.
Do you need to set the IP header yourself in Next.js?
No. Next.js rewrites pass the browser's headers on and put the visitor's IP address in X-Forwarded-For. You write no header code.
You still have to tell Loogika to trust that header. Turn on First-Party Proxy under Site Settings, General. Without the switch, every visit can look as if it comes from your server.
How do you check that the Next.js proxy works?
Open your site with the browser's developer tools and look at the Network tab. Work through these three checks:
- The request for
/stats/script.jsgoes to your own domain and returns status 200. - A click to another page sends a POST request to
/stats/trackon your own domain. - Your visit shows up in the realtime view in Loogika, with your real location.
What goes wrong most often with Next.js rewrites?
| Symptom | Fix |
|---|---|
| The rewrites have no effect | Restart the development server. Check that rewrites is declared as an async function |
The script loads, but /stats/track returns 404 | Compare each source path with the list above. The paths are case-sensitive |
| Pageviews arrive, but session replay records nothing | Add the rewrites for /stats/replay.js and /stats/session-replay/record/:siteId |
| Every visitor has the same location | Turn on First-Party Proxy under Site Settings, General |
Rewrites stream request bodies straight through. Next.js buffers request bodies only when your app has a proxy.ts file, and the default cap is then 10 MB. The proxy troubleshooting page covers problems that are not specific to Next.js.