# Proxy with Netlify

> Serve the MetricRaven script and events from a Netlify Function on your own domain.

Source: https://metricraven.com/docs/proxy-netlify  
Markdown: https://metricraven.com/docs/proxy-netlify.md  
Section: Proxy

A Netlify Function mounted on your two paths relays the script and events with Netlify's visitor IP and location.

## Set it up

1. **Add the function**

   ```javascript title="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.path` serves the function at `/a/script` and `/a/event` directly, so it needs no `_redirects` lines.

2. **Deploy**

   Push to your connected repository or run `netlify deploy --prod`.

3. **Point your script tag at your own paths**

   Replace your MetricRaven tag with this one. `src` loads the tracker from your domain and `data-api` sends its events there too.

   ```html title="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 `_redirects` proxy rule such as `/a/event https://metricraven.com/api/v1/collect 200` is not enough on its own: it can't add `x-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](https://metricraven.com/docs/proxy).

## Details

- The proxy forwards only `Content-Type`, `Origin`, `User-Agent` and the visitor's IP as `x-visitor-ip`. Your site's cookies and auth headers never leave your server.
- `Origin` matters: MetricRaven checks it against your site's allowed domains. A missing or wrong origin is refused with `domain_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.
