Skip to content

Laravel Proxy

Route usertrax through your Laravel application to serve the script and events from your own domain.

  1. Create the proxy controller

    Terminal-Fenster
    php artisan make:controller UsertraxProxyController
  2. Implement proxy logic

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

    <?php
    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use Illuminate\Http\Response;
    use Illuminate\Support\Facades\Cache;
    use Illuminate\Support\Facades\Http;
    class UsertraxProxyController extends Controller
    {
    public function script(): Response
    {
    $body = Cache::remember('usertrax_script', 86_400, function (): string {
    return Http::get('https://usertrax.io/cvs.js')->body();
    });
    return response($body, 200, [
    'Content-Type' => 'application/javascript',
    'Cache-Control' => 'public, max-age=86400',
    ]);
    }
    public function events(Request $request): Response
    {
    $response = Http::withHeaders([
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'User-Agent' => $request->header('User-Agent', ''),
    'X-Auth-Key' => $request->header('X-Auth-Key', ''),
    ])->post('https://api.usertrax.io/api/events', $request->all());
    return response($response->body(), $response->status(), [
    'Content-Type' => 'application/json',
    ]);
    }
    }
  3. Register routes

    Add to routes/web.php:

    use App\Http\Controllers\UsertraxProxyController;
    Route::get('/cvs.js', [UsertraxProxyController::class, 'script']);
    Route::post('/api/usertrax/events', [UsertraxProxyController::class, 'events']);

    Use web.php, not api.php, so CSRF does not block browser POST requests. The events route accepts JSON from the tracker script.

  4. Update your Blade layout

    <script>
    window.usertraxConfig = {
    endpoint: "/api/usertrax/events",
    };
    </script>
    <script src="/cvs.js" data-key="{{ config('services.usertrax.key') }}" defer></script>
  5. Deploy

    The proxy is active once routes are deployed. Clear route cache if you use it: php artisan route:clear.

  1. Visit your website
  2. Open the Network tab
  3. Confirm requests go through your domain
  • 419 CSRF token mismatch: Ensure the events route is in web.php with CSRF excluded, or add the route to $except in VerifyCsrfToken middleware
  • 401 Unauthorized: Check that X-Auth-Key is forwarded and data-key is correct