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';
  }
}));

// 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, use the [managed proxy](/docs/managed-proxy) for automatic visitor IP handling, or review your proxy provider's forwarding settings.

## 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. Use the [managed proxy](/docs/managed-proxy) for automatic visitor IP handling, or review your proxy provider's forwarding settings.
