Proxy DataFast with Laravel

Learn how to proxy DataFast analytics through your Laravel application to bypass adblockers and improve accuracy.

1. Create Proxy Controller

Create a new controller to handle the proxy requests:

php artisan make:controller DataFastProxyController

2. Implement Proxy Logic

Add the following to app/Http/Controllers/DataFastProxyController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;

class DataFastProxyController extends Controller
{
    public function script()
    {
        // Cache the script for 1 year
        return Cache::remember('datafast_script', 31536000, function () {
            $response = Http::get('https://datafa.st/js/script.js');
            return response($response->body(), 200, [
                'Content-Type' => 'application/javascript',
                'Cache-Control' => 'public, max-age=31536000'
            ]);
        });
    }

    public function events(Request $request)
    {
        // Get origin from request or construct from URL
        $origin = $request->header('Origin') ?? $request->getSchemeAndHttpHost();
        
        // Get real client IP for accurate location tracking
        $clientIp = $request->header('X-Real-IP') 
            ?? $request->header('X-Forwarded-For')
            ?? $request->ip();
        
        if (str_contains($clientIp, ',')) {
            $clientIp = trim(explode(',', $clientIp)[0]);
        }
        
        $response = Http::withHeaders([
            'User-Agent' => $request->header('User-Agent'),
            'Content-Type' => 'application/json',
            'Origin' => $origin
        ])->post('https://datafa.st/api/events', $request->all());

        return response($response->body(), $response->status());
    }
}

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

3. Add Routes

Add the following routes to routes/web.php:

Route::get('/js/script.js', [DataFastProxyController::class, 'script']);
Route::post('/api/events', [DataFastProxyController::class, 'events']);

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

5. Deploy Your Application

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. Verify your proxy sends x-real-ip or x-forwarded-for headers with the client IP
  2. If the issue persists, contact support with your proxy configuration
Something missing? Suggest features ✍️