Files
analytics/app/Http/Controllers/TrackingScriptController.php
T

39 lines
1.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Website;
use Illuminate\Http\Response;
class TrackingScriptController extends Controller
{
public function script(string $trackingId): Response
{
$website = Website::where('tracking_id', $trackingId)
->where('is_active', true)
->first();
if (! $website) {
return response('', 404)
->header('Content-Type', 'application/javascript');
}
// Use ad-blocker resistant endpoint if script is loaded from /cdn/resources/
// Otherwise use the original endpoint for backward compatibility
$currentRoute = request()->route()->getName();
// $collectUrl = $currentRoute === 'tracking.script.alt'
// ? 'route('collect.store.alt')'
// : route('collect.store');
$collectUrl = 'https://assets.libremetrics.com/assets/client/data';
$js = view('tracking.tracker', [
'trackingId' => $trackingId,
'collectUrl' => $collectUrl,
])->render();
return response($js)
->header('Content-Type', 'application/javascript')
->header('Cache-Control', 'public, max-age=3600');
}
}