Laravel proxy

Proxy attribu.tech analytics through Laravel.

This is a server-specific guide. See Proxy the tracking script for an overview of why and how proxying works.

Add routes

Register two routes in routes/web.php:

use App\Http\Controllers\ProxyController;

Route::get('/js/attribu.js', [ProxyController::class, 'scriptJs']);
Route::post('/api/proxy/events', [ProxyController::class, 'events']);

Create the controller

Generate the controller and add the proxy methods:

php artisan make:controller ProxyController
<?php

namespace App\Http\Controllers;

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

class ProxyController extends Controller
{
    public function scriptJs()
    {
        $response = Http::get('https://attribu.tech/js/script.js');

        return response($response->body(), $response->status())
            ->header('Content-Type', 'application/javascript')
            ->header('Cache-Control', 'public, max-age=3600');
    }

    public function events(Request $request)
    {
        $response = Http::withHeaders([
                'Content-Type' => 'application/json',
                'X-Forwarded-For' => $request->ip(),
            ])
            ->withBody($request->getContent(), 'application/json')
            ->post('https://attribu.tech/api/events');

        return response($response->body(), $response->status())
            ->header('Content-Type', 'application/json');
    }
}
Laravel's Http facade uses Guzzle under the hood. Make sure the php-curl extension is enabled on your server.

Update your script tag

Point the script src to your proxied path and set data-api-url to your proxied API endpoint:

<script
  defer
  data-website-id="YOUR_SITE_ID"
  data-domain="yourdomain.com"
  data-api-url="https://yourdomain.com/api/proxy/events"
  src="/js/attribu.js"
></script>
Replace YOUR_SITE_ID with your actual Website ID from attribu.tech. Replace yourdomain.com with your website's root domain.

Verify installation

  1. Visit your site in an incognito/private window.
  2. Open your browser's Developer Tools and go to the Network tab.
  3. Confirm the script loads from /js/attribu.js on your domain.
  4. Confirm the pageview POST goes to /api/proxy/events on your domain.
  5. Check your attribu.tech dashboard. A new pageview should appear within a few seconds.

What's next

Copied