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
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
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
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)
RewriteEngine On RewriteRule ^js/script\.js$ script.php [L] RewriteRule ^api/events$ events.php [L]
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:
<script
defer
data-website-id="dfid_******"
data-domain="yourdomain.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:
- Visit your website
- Open the network tab in your browser's developer tools
- 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:
- Make sure your proxy sends the
x-datafast-real-ipheader with the actual visitor IP address (not your proxy server IP) when forwarding events to DataFast's/api/eventsendpoint - If the issue persists, contact support with your proxy configuration