Source: https://datafa.st/docs/express-proxy
Markdown source: https://datafa.st/docs/express-proxy.md
Description: How to proxy DataFast analytics through your Express.js server

# Proxy DataFast with Express.js

Learn how to proxy DataFast analytics through your Express.js server to bypass adblockers and improve accuracy.

## 1. Install Required Dependencies

```bash
npm install express-http-proxy
```

## 2. Add Proxy Configuration

Add the following to your Express.js application:

```javascript
const express = require('express');
const proxy = require('express-http-proxy');
const app = express();

app.set('trust proxy', true);

// Proxy the script
app.use('/js/script.js', proxy('datafa.st', {
  https: true,
  proxyReqPathResolver: function (req) {
    return '/js/script.js';
  }
}));

// Proxy the events endpoint
app.use('/api/events', proxy('datafa.st', {
  https: true,
  proxyReqPathResolver: function (req) {
    return '/api/events';
  },
  proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
    // Get the real client IP address
    const clientIp = srcReq.headers['x-real-ip'] || 
                      srcReq.headers['x-forwarded-for']?.split(',')[0]?.trim() || 
                      srcReq.ip;
    
    // Add the x-datafast-real-ip header for accurate location tracking
    proxyReqOpts.headers['x-datafast-real-ip'] = clientIp;
    
    return proxyReqOpts;
  }
}));

// Your other routes...
app.listen(3000);
```

> **Note:** 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)

> **Important:** If you notice all visitors showing from the same location in your analytics, make sure you're sending 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.

## 3. Update Your Script Tag

Replace your existing DataFast script with the proxied version:

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

## 4. Deploy your server

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
