Source: https://datafa.st/docs/laravel-proxy
Markdown source: https://datafa.st/docs/laravel-proxy.md
Description: How to proxy DataFast analytics through your Laravel application

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

```bash
php artisan make:controller DataFastProxyController
```

## 2. Implement Proxy Logic

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

```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 day
        return Cache::remember('datafast_script', 86400, function () {
            $response = Http::get('https://datafa.st/js/script.js');
            return response($response->body(), 200, [
                'Content-Type' => 'application/javascript',
                'Cache-Control' => 'public, max-age=86400'
            ]);
        });
    }

    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,
            'x-datafast-real-ip' => $clientIp
        ])->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](/docs/script-configuration#api-url-data-api-url-optional)

## 3. Add Routes

Add the following routes to `routes/web.php`:

```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:

```html
<script
  defer
  data-website-id="dfid_******"
  data-domain="your_domain.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. 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
