Self-hosted proxy
If you run your own server or edge functions, you can forward Foresite's two requests through your own domain:
GET /s/YOUR_SITE_ID.js→https://t.foresite.dev/s/YOUR_SITE_ID.jsPOST /e→https://t.foresite.dev/e
On both routes, your proxy must send:
X-Foresite-Proxy-Secret: your site's proxy secret, from Settings → Install. Without it, Foresite ignores the two headers below, so nobody can fake visitors' addresses or redirect your events.X-Forwarded-For: the visitor's IP address, so countries and unique visitors are counted correctly. Foresite uses it only in memory and never stores it.X-Forwarded-Host: your own domain, so the script sends events back through your proxy.
Don't forward cookies. Foresite never reads them.
nginx
location ~ ^/(s/YOUR_SITE_ID\.js|e)$ {
proxy_pass https://t.foresite.dev;
proxy_ssl_server_name on;
proxy_set_header Host t.foresite.dev;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Foresite-Proxy-Secret "YOUR_PROXY_SECRET";
proxy_set_header Cookie "";
}
Cloudflare Workers
export default {
async fetch(request) {
const url = new URL(request.url)
if (url.pathname !== '/e' && !url.pathname.startsWith('/s/')) return fetch(request)
const headers = new Headers({
'Content-Type': request.headers.get('Content-Type') || 'text/plain',
'User-Agent': request.headers.get('User-Agent') || '',
'X-Forwarded-For': request.headers.get('CF-Connecting-IP') || '',
'X-Forwarded-Host': url.host,
'X-Foresite-Proxy-Secret': 'YOUR_PROXY_SECRET',
})
return fetch('https://t.foresite.dev' + url.pathname, {
method: request.method,
headers,
body: request.method === 'POST' ? await request.text() : undefined,
})
},
}
Next.js on Vercel
Vercel's rewrites in vercel.json or next.config.js can't add request headers, so they can't send your proxy secret. Use two route handlers instead. They need the app folder, which a Pages Router project can add just for these files.
Add your proxy secret as an environment variable named FORESITE_PROXY_SECRET in your Vercel project's Settings → Environment Variables, then add:
// app/foresite.ts
const UPSTREAM = 'https://t.foresite.dev'
export async function forward(request: Request, path: string) {
const upstream = await fetch(UPSTREAM + path, {
method: request.method,
headers: {
'Content-Type': request.headers.get('content-type') || 'text/plain',
'User-Agent': request.headers.get('user-agent') || '',
'X-Forwarded-For': request.headers.get('x-real-ip') || '',
'X-Forwarded-Host': request.headers.get('host') || '',
'X-Foresite-Proxy-Secret': process.env.FORESITE_PROXY_SECRET || '',
},
body: request.method === 'POST' ? await request.text() : undefined,
cache: 'no-store',
})
const headers = new Headers()
for (const name of ['content-type', 'cache-control']) {
const value = upstream.headers.get(name)
if (value) headers.set(name, value)
}
return new Response(upstream.body, { status: upstream.status, headers })
}
// app/s/[script]/route.ts
import { forward } from '../../foresite'
export async function GET(request: Request, { params }: { params: Promise<{ script: string }> }) {
return forward(request, '/s/' + (await params).script)
}
// app/e/route.ts
import { forward } from '../foresite'
export async function POST(request: Request) {
return forward(request, '/e')
}
On Vercel, x-real-ip holds the visitor's IP address, set by Vercel itself so visitors can't fake it. On Next.js 14 and earlier, params isn't a promise: use params.script without await.
Your Foresite line then loads the script from your own domain, e.g. <script defer src="https://www.example.com/s/YOUR_SITE_ID.js"></script>, and events follow the same route.