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

# Proxy DataFast with PHP

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

## 1. Create Proxy Endpoints

Create two PHP files to handle the proxy requests:

### script.php
```php
<?php
header('Content-Type: application/javascript');
header('Cache-Control: public, max-age=31536000');

$ch = curl_init('https://datafa.st/js/script.js');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$script = curl_exec($ch);
curl_close($ch);

echo $script;
```

### events.php
```php
<?php
header('Content-Type: application/json');

// Get the real client IP address
function getClientIp() {
    if (!empty($_SERVER['HTTP_X_REAL_IP'])) {
        return $_SERVER['HTTP_X_REAL_IP'];
    }
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        return trim($ips[0]);
    }
    return $_SERVER['REMOTE_ADDR'] ?? '';
}

// Get the origin from request headers or construct from server variables
$origin = $_SERVER['HTTP_ORIGIN'] ?? 
    ($_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'];

$clientIp = getClientIp();

$ch = curl_init('https://datafa.st/api/events');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('php://input'));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'User-Agent: ' . $_SERVER['HTTP_USER_AGENT'],
    'Origin: ' . $origin,
    'x-datafast-real-ip: ' . $clientIp
]);
$response = curl_exec($ch);
curl_close($ch);

echo $response;
```

> **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.

## 2. Configure Your Web Server

### Apache (.htaccess)
```apache
RewriteEngine On
RewriteRule ^js/script\.js$ script.php [L]
RewriteRule ^api/events$ events.php [L]
```

### Nginx
```nginx
location /js/script.js {
    try_files $uri $uri/ /script.php?$query_string;
}

location /api/events {
    try_files $uri $uri/ /events.php?$query_string;
}
```

## 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 changes

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
