From cd259d55c79ee13f5dc5fbb9be0b21d4d9626071 Mon Sep 17 00:00:00 2001 From: "Sebastian BURGIN-FIX (ext)" Date: Sat, 24 Oct 2026 15:05:37 +0200 Subject: [PATCH] feat(analytics): embed codebar analytics next to Fathom Loads https://analytics.codebar.ch/js/script.js in production when CODEBAR_ANALYTICS_URL is set (config default.services.codebar_analytics), and allows analytics.codebar.ch in script-src and connect-src (the tracker sends events with sendBeacon to /api/event). Fathom stays in place so both can run side by side until the switch. Co-Authored-By: Claude Opus 5.5 --- .env.example | 1 + app/Security/Presets/MyCspPreset.php | 2 ++ config/default.php | 6 +++++ .../_partials/_codebar_analytics.blade.php | 8 ++++++ resources/views/layouts/app.blade.php | 1 + .../Views/CodebarAnalyticsPartialTest.php | 26 +++++++++++++++++++ tests/Unit/Security/MyCspPresetTest.php | 10 +++++++ 7 files changed, 54 insertions(+) create mode 100644 resources/views/layouts/_partials/_codebar_analytics.blade.php create mode 100644 tests/Feature/Views/CodebarAnalyticsPartialTest.php diff --git a/.env.example b/.env.example index 1b305db0..2a08852d 100644 --- a/.env.example +++ b/.env.example @@ -70,6 +70,7 @@ CLOUDINARY_API_KEY=my-api-key CLOUDINARY_API_SECRET=my-api-secret LARAVEL_DEFAULT_FATHOM_SITE_ID=WECFOADW +CODEBAR_ANALYTICS_URL=https://analytics.codebar.ch LITELLM_URL=https://llm.codebar.net LITELLM_MASTER_KEY= GITHUB_TOKEN= diff --git a/app/Security/Presets/MyCspPreset.php b/app/Security/Presets/MyCspPreset.php index 2e809139..146cbeec 100644 --- a/app/Security/Presets/MyCspPreset.php +++ b/app/Security/Presets/MyCspPreset.php @@ -20,6 +20,7 @@ public function configure(Policy $policy): void Keyword::SELF, 'cdn.usefathom.com', 'cdn-eu.usefathom.com', + 'analytics.codebar.ch', $cdnHost ?: null, ]); @@ -78,6 +79,7 @@ public function configure(Policy $policy): void Keyword::SELF, 'cdn.usefathom.com', 'cdn-eu.usefathom.com', + 'analytics.codebar.ch', ]); } } diff --git a/config/default.php b/config/default.php index e52f2df8..0f024e82 100644 --- a/config/default.php +++ b/config/default.php @@ -14,6 +14,12 @@ EnvironmentEnum::PRODUCTION->value, ], ], + 'codebar_analytics' => [ + 'url' => env('CODEBAR_ANALYTICS_URL'), + 'environments' => [ + EnvironmentEnum::PRODUCTION->value, + ], + ], ], 'feature_policy' => [ diff --git a/resources/views/layouts/_partials/_codebar_analytics.blade.php b/resources/views/layouts/_partials/_codebar_analytics.blade.php new file mode 100644 index 00000000..4c382866 --- /dev/null +++ b/resources/views/layouts/_partials/_codebar_analytics.blade.php @@ -0,0 +1,8 @@ +@php + $environments = config('default.services.codebar_analytics.environments', []); + $url = config('default.services.codebar_analytics.url'); +@endphp + +@if (in_array(app()->environment(), $environments) && $url) + +@endif diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 606e325f..887bfff3 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -79,6 +79,7 @@ class="sr-only focus:not-sr-only focus:absolute focus:top-4 focus:left-4 focus:z @include('layouts._partials._fathom') +@include('layouts._partials._codebar_analytics') diff --git a/tests/Feature/Views/CodebarAnalyticsPartialTest.php b/tests/Feature/Views/CodebarAnalyticsPartialTest.php new file mode 100644 index 00000000..c463077f --- /dev/null +++ b/tests/Feature/Views/CodebarAnalyticsPartialTest.php @@ -0,0 +1,26 @@ +render()) + ->toContain(''); +})->group('analytics'); + +it('does not embed the codebar analytics script outside production', function () { + Config::set('default.services.codebar_analytics.url', 'https://analytics.codebar.ch'); + + expect(view('layouts._partials._codebar_analytics')->render())->not->toContain('analytics.codebar.ch'); +})->group('analytics'); + +it('does not embed the codebar analytics script without a url', function () { + Config::set('default.services.codebar_analytics.url', null); + app()['env'] = 'production'; + + expect(view('layouts._partials._codebar_analytics')->render())->not->toContain('group('analytics'); diff --git a/tests/Unit/Security/MyCspPresetTest.php b/tests/Unit/Security/MyCspPresetTest.php index a4db7812..b2b2277d 100644 --- a/tests/Unit/Security/MyCspPresetTest.php +++ b/tests/Unit/Security/MyCspPresetTest.php @@ -21,3 +21,13 @@ expect($contents)->toContain('res.cloudinary.com'); expect($contents)->toContain('www.gravatar.com'); })->group('unit', 'security'); + +it('allows the codebar analytics script and its event endpoint', function () { + $policy = new Policy; + + (new MyCspPreset)->configure($policy); + + expect($policy->getContents()) + ->toMatch('/script-src [^;]*analytics\.codebar\.ch/') + ->toMatch('/connect-src [^;]*analytics\.codebar\.ch/'); +})->group('unit', 'security');