ad blockers resistent code
This commit is contained in:
@@ -18,7 +18,12 @@ class TrackingScriptController extends Controller
|
||||
->header('Content-Type', 'application/javascript');
|
||||
}
|
||||
|
||||
$collectUrl = route('collect.store');
|
||||
// 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');
|
||||
|
||||
$js = view('tracking.tracker', [
|
||||
'trackingId' => $trackingId,
|
||||
|
||||
+11
-1
@@ -115,11 +115,21 @@ class Website extends Model
|
||||
|
||||
public function embedSnippet(): string
|
||||
{
|
||||
$scriptUrl = route('tracking.script', $this->tracking_id);
|
||||
$scriptUrl = route('tracking.script.alt', $this->tracking_id);
|
||||
|
||||
return <<<HTML
|
||||
<!-- Analytics Tracker -->
|
||||
<script defer src="{$scriptUrl}"></script>
|
||||
HTML;
|
||||
}
|
||||
|
||||
public function embedSnippetLegacy(): string
|
||||
{
|
||||
$scriptUrl = route('tracking.script', $this->tracking_id);
|
||||
|
||||
return <<<HTML
|
||||
<!-- Analytics Tracker (Legacy) -->
|
||||
<script defer src="{$scriptUrl}"></script>
|
||||
HTML;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,12 +15,16 @@
|
||||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
|
||||
<div class="p-6">
|
||||
<h3 class="text-lg font-medium text-gray-900 mb-2">{{ __('Install the tracking code') }}</h3>
|
||||
<p class="text-sm text-gray-600 mb-6">
|
||||
<p class="text-sm text-gray-600 mb-2">
|
||||
{{ __('Copy and paste the following code snippet into the') }}
|
||||
<code class="text-sm bg-gray-100 px-1.5 py-0.5 rounded"><head></code>
|
||||
{{ __('section of every page on') }}
|
||||
<strong>{{ $website->domain }}</strong>.
|
||||
</p>
|
||||
<p class="text-sm text-green-700 bg-green-50 border border-green-200 rounded px-3 py-2 mb-6">
|
||||
<strong>{{ __('Ad-blocker resistant:') }}</strong>
|
||||
{{ __('This snippet uses paths that bypass most ad blockers.') }}
|
||||
</p>
|
||||
|
||||
<div class="relative" x-data="{ copied: false }">
|
||||
<pre class="bg-gray-900 text-gray-100 rounded-lg p-4 text-sm overflow-x-auto"><code id="snippet-code">{{ $website->embedSnippet() }}</code></pre>
|
||||
@@ -79,11 +83,11 @@ ta('event', 'purchase', {
|
||||
</div>
|
||||
<div>
|
||||
<dt class="text-sm font-medium text-gray-500">{{ __('Script URL') }}</dt>
|
||||
<dd class="mt-1 text-sm text-gray-900 font-mono break-all">{{ route('tracking.script', $website->tracking_id) }}</dd>
|
||||
<dd class="mt-1 text-sm text-gray-900 font-mono break-all">{{ route('tracking.script.alt', $website->tracking_id) }}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt class="text-sm font-medium text-gray-500">{{ __('Collection Endpoint') }}</dt>
|
||||
<dd class="mt-1 text-sm text-gray-900 font-mono break-all">{{ route('collect.store') }}</dd>
|
||||
<dd class="mt-1 text-sm text-gray-900 font-mono break-all">{{ route('collect.store.alt') }}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
@@ -5,6 +5,7 @@ use App\Http\Controllers\CollectController;
|
||||
use App\Http\Controllers\TrackingScriptController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
// Original endpoints (kept for backward compatibility)
|
||||
Route::get('/t/{trackingId}.js', [TrackingScriptController::class, 'script'])
|
||||
->middleware('collect.cors')
|
||||
->name('tracking.script');
|
||||
@@ -19,6 +20,21 @@ Route::middleware('collect.cors')->group(function () {
|
||||
->name('collect.pixel');
|
||||
});
|
||||
|
||||
// Ad-blocker resistant endpoints (use these in production)
|
||||
Route::get('/cdn/resources/{trackingId}.js', [TrackingScriptController::class, 'script'])
|
||||
->middleware('collect.cors')
|
||||
->name('tracking.script.alt');
|
||||
|
||||
Route::middleware('collect.cors')->group(function () {
|
||||
Route::post('/cdn/feedback', [CollectController::class, 'store'])
|
||||
->middleware('throttle:collect')
|
||||
->name('collect.store.alt');
|
||||
|
||||
Route::get('/cdn/status.gif', [CollectController::class, 'pixel'])
|
||||
->middleware('throttle:collect')
|
||||
->name('collect.pixel.alt');
|
||||
});
|
||||
|
||||
Route::prefix('v1')->middleware(['auth:sanctum', 'throttle:api'])->name('api.v1.')->group(function () {
|
||||
Route::get('websites', [ApiWebsiteController::class, 'index'])->name('websites.index');
|
||||
Route::get('websites/{website}/stats', [ApiWebsiteController::class, 'stats'])->name('websites.stats');
|
||||
|
||||
Reference in New Issue
Block a user