Proxy
Proxy with Netlify
A Netlify Function mounted on your two paths relays the script and events with Netlify's visitor IP and location.
Set it up
Add the function
netlify/functions/metricraven.mjs// netlify/functions/metricraven.mjs · Netlify Functions export default async (request, context) => { const { pathname } = new URL(request.url); const script = pathname === "/a/script" && request.method === "GET"; const event = pathname === "/a/event" && request.method === "POST"; if (!script && !event) return new Response("Not found", { status: 404 }); const headers = new Headers({ "x-visitor-ip": context.ip }); for (const name of ["content-type", "origin", "user-agent"]) { const value = request.headers.get(name); if (value) headers.set(name, value); } const geo = context.geo || {}; for (const [name, value] of [ ["x-visitor-country", geo.country?.code], ["x-visitor-region", geo.subdivision?.name], ["x-visitor-city", geo.city], ["x-visitor-latitude", geo.latitude], ["x-visitor-longitude", geo.longitude], ]) { if (value) headers.set(name, encodeURIComponent(value)); } const upstream = await fetch(script ? "https://metricraven.com/js/script.js" : "https://metricraven.com/api/v1/collect", { method: request.method, headers, body: event ? await request.arrayBuffer() : undefined, signal: AbortSignal.timeout(15000), }); return new Response(upstream.body, { status: upstream.status, headers: { "Content-Type": upstream.headers.get("content-type") || "application/octet-stream", "Cache-Control": script ? "public, max-age=3600" : "no-store", }, }); }; // Serve the function at these paths on your site's own domain. export const config = { path: ["/a/script", "/a/event"] };config.pathserves the function at/a/scriptand/a/eventdirectly, so it needs no_redirectslines.Deploy
Push to your connected repository or run
netlify deploy --prod.Point your script tag at your own paths
Replace your MetricRaven tag with this one.
srcloads the tracker from your domain anddata-apisends its events there too.Your website's shared layout<script defer src="/a/script" data-api="/a/event" data-site-id="YOUR_SITE_ID"></script>
Verify it works
Open your live site. In the browser's Network panel, /a/script loads the tracker and /a/event answers with ok: true. Nothing goes to metricraven.com from the browser.
In Traffic, your own visit should show your real country and city. If every visit appears to come from your server's or host's location, the visitor IP isn't being forwarded.
Notes
- A
_redirectsproxy rule such as/a/event https://metricraven.com/api/v1/collect 200is not enough on its own: it can't addx-visitor-ip, so every visit would appear to come from Netlify.
How the proxy works and which headers it must send: Use your own domain.
Details
- The proxy forwards only
Content-Type,Origin,User-Agentand the visitor's IP asx-visitor-ip. Your site's cookies and auth headers never leave your server. Originmatters: MetricRaven checks it against your site's allowed domains. A missing or wrong origin is refused withdomain_not_allowed.- MetricRaven allows about 120 events per minute per visitor IP. Without
x-visitor-ip, all of your visitors share the proxy's IP, so busy sites lose events as well as locations. - A proxy doesn't guarantee that every blocker will allow tracking.