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

$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']
]);
$response = curl_exec($ch);
curl_close($ch);

echo $response;

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="yourwebsiteid"
  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:

  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
Something missing? Suggest features ✍️