Laravel Proxy
Route usertrax through your Laravel application to serve the script and events from your own domain.
Create the proxy controller
Terminal-Fenster php artisan make:controller UsertraxProxyControllerImplement proxy logic
Add the following to
app/Http/Controllers/UsertraxProxyController.php:<?phpnamespace 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',]);}}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, notapi.php, so CSRF does not block browser POST requests. The events route accepts JSON from the tracker script.Update your Blade layout
<script>window.usertraxConfig = {endpoint: "/api/usertrax/events",};</script><script src="/cvs.js" data-key="{{ config('services.usertrax.key') }}" defer></script>Deploy
The proxy is active once routes are deployed. Clear route cache if you use it:
php artisan route:clear.
Verification
Section titled “Verification”- Visit your website
- Open the Network tab
- Confirm requests go through your domain
Troubleshooting
Section titled “Troubleshooting”- 419 CSRF token mismatch: Ensure the events route is in
web.phpwith CSRF excluded, or add the route to$exceptinVerifyCsrfTokenmiddleware - 401 Unauthorized: Check that
X-Auth-Keyis forwarded anddata-keyis correct