Source: https://datafa.st/docs/nextjs-proxy
Markdown source: https://datafa.st/docs/nextjs-proxy.md
Description: Step-by-step tutorial to proxy DataFast analytics through your Next.js app to bypass adblockers and improve tracking accuracy.

# Proxy DataFast with Next.js

Learn how to proxy DataFast analytics through your own domain using Next.js to bypass adblockers and improve accuracy.

## 1. Add Rewrites Configuration

Create or update your `next.config.js` file in your project root:

```javascript
module.exports = {
  async rewrites() {
    return [
      {
        source: "/js/script.js",
        destination: "https://datafa.st/js/script.js",
      },
      {
        source: "/api/events",
        destination: "https://datafa.st/api/events",
      },
    ];
  },
};
```

Next.js will automatically proxy both the script and events endpoint with proper IP forwarding.

> If you already have an `/api/events` API endpoint, add `data-api-url` to the DataFast script tag to send events to your own API endpoint. For example, `data-api-url="/datafast-events"` will send events to `/datafast-events` instead of `/api/events`. Read more [here](/docs/script-configuration#api-url-data-api-url-optional)

## 2. Update Your Script Tag

Replace your existing DataFast script with the proxied version:

```html
<Script
  strategy="afterInteractive"
  data-website-id="dfid_******"
  data-domain="your_domain.com"
  src="/js/script.js"
></Script>
```

## 3. Deploy Your Site

The proxy configuration will take effect automatically after deployment.

## Verification

To verify the proxy is working:
1. Visit your website
2. Open the network tab in your browser's developer tools
3. Check that analytics requests are going through your domain instead of datafa.st

## Troubleshooting

#### *All visitors showing from the same location*

If your analytics show all visitors from a single location (usually your server's location), your proxy isn't forwarding visitor IPs.

**To fix:**
1. Make sure your proxy sends the `x-datafast-real-ip` header with the actual visitor IP address (not your proxy server IP) when forwarding events to DataFast's `/api/events` endpoint
