From 5892d649673718eb814dc1828b6d045769a6ff3f Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Fri, 8 Mar 2024 14:29:31 +0100 Subject: [PATCH 1/6] Improve Middleware except --- src/Http/Middleware/TrackPageview.php | 62 ++++++++++++++++++++++++--- tests/TrackPageviewTest.php | 11 +++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/Http/Middleware/TrackPageview.php b/src/Http/Middleware/TrackPageview.php index 1c8f9ac..4e8467c 100644 --- a/src/Http/Middleware/TrackPageview.php +++ b/src/Http/Middleware/TrackPageview.php @@ -9,6 +9,28 @@ class TrackPageview { + /** + * The URIs that should be excluded from tracking. + * + * @var array + */ + protected array $except = [ + 'telescope', + 'horizon', + ]; + + /** + * The Headers that should be excluded from tracking. + * + * @var array + */ + protected array $exceptHeaders = [ + 'X-Livewire', + ]; + + /** + * Handle an incoming request. + */ public function handle(Request $request, Closure $next): mixed { $response = $next($request); @@ -17,16 +39,44 @@ public function handle(Request $request, Closure $next): mixed return $response; } - if ($request->hasHeader('X-Livewire')) { - return $response; + if (! $this->inExceptArray($request) && + ! $this->inExceptHeadersArray($request) + ) { + Pirsch::track(); } - if (str_starts_with($request->route()->uri, 'telescope/')) { - return $response; + return $response; + } + + /** + * Determine if the request has a header that should not be tracked. + */ + protected function inExceptHeadersArray(Request $request): bool + { + foreach ($this->exceptHeaders as $except) { + if ($request->hasHeader($except)) { + return true; + } } - Pirsch::track(); + return false; + } - return $response; + /** + * Determine if the request has a URI that should not be tracked. + */ + protected function inExceptArray(Request $request): bool + { + foreach ($this->except as $except) { + if ($except !== '/') { + $except = trim($except, '/'); + } + + if ($request->fullUrlIs($except) || $request->is($except)) { + return true; + } + } + + return false; } } diff --git a/tests/TrackPageviewTest.php b/tests/TrackPageviewTest.php index 317ba74..5d86cb5 100644 --- a/tests/TrackPageviewTest.php +++ b/tests/TrackPageviewTest.php @@ -46,3 +46,14 @@ Pirsch::shouldNotHaveBeenCalled(); }); + +it('skips Horizon', function () { + Pirsch::spy(); + + Route::middleware(TrackPageview::class) + ->get('horizon/test', fn () => 'Hello World'); + + $this->get('/horizon/test'); + + Pirsch::shouldNotHaveBeenCalled(); +}); From abd0a71f2fb1bd8cf27818f39aa89753c6654bb1 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Tue, 26 Mar 2024 13:33:06 +0100 Subject: [PATCH 2/6] Update README.md --- README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/README.md b/README.md index 3abd03f..8467930 100644 --- a/README.md +++ b/README.md @@ -72,3 +72,52 @@ Pirsch::track( ], ); ``` + +### Filter pages + +You can configure the `TrackPageview` middleware to exclude specific pages from being tracked. + +Create a new middleware like this: + +```php +namespace App\Http\Middleware; + +use Pirsch\Http\Middleware\TrackPageview as Middleware; + +class TrackPageview extends Middleware +{ + /** + * The URIs that should be excluded from tracking. + * + * @var array + */ + protected array $except = [ + 'url/to/exclude', + ]; + + /** + * The Headers that should be excluded from tracking. + * + * @var array + */ + protected array $exceptHeaders = [ + 'X-ExcludedHeader', + ]; +} +``` + +- `except` is an array with all URIs paths taht you want to exclude from tracking. +- `exceptHeaders` is an array with all Headers that you want to exclude from tracking. + +Then replace the `TrackPageview` middleware with this on in your middleware configuration: + +```php +protected $middlewareGroups = [ + 'web' => [ + // ... + \App\Http\Middleware\TrackPageview::class, + ], + + // ... +]; +``` From 62878004a5f812e81e170be7a42a3df1a1b0fdeb Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Tue, 26 Mar 2024 13:55:56 +0100 Subject: [PATCH 3/6] Update README.md --- README.md | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8467930..2fd7690 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,22 @@ This package is the official Laravel integration for [Pirsch Analytics](https:// #### Automatically This package comes with a `TrackPageview` middleware that allows you to track pageviews automatically. -Apply the middleware to your web routes by adding it to the `web` key of the `$middlewareGroups` property in your `app/Http/Kernel.php` class: +Apply the middleware to your web routes by adding it to the `web` middleware group within your application's `bootstrap/app.php` file: +```php +use Pirsch\Http\Middleware\TrackPageview; + +->withMiddleware(function (Middleware $middleware) { + $middleware->web(append: [ + TrackPageview::class, + ]); +}) +``` + +
+For Laravel <= 10.x +Add the middleware to the web key of the $middlewareGroups property in your app/Http/Kernel.php class: + ```php protected $middlewareGroups = [ 'web' => [ @@ -45,6 +59,7 @@ protected $middlewareGroups = [ // ... ]; ``` +
#### Manually @@ -109,8 +124,22 @@ class TrackPageview extends Middleware - `except` is an array with all URIs paths taht you want to exclude from tracking. - `exceptHeaders` is an array with all Headers that you want to exclude from tracking. -Then replace the `TrackPageview` middleware with this on in your middleware configuration: +Then replace the `TrackPageview` middleware with this one on your `bootstrap/app.php` middleware configuration: + +```php +use App\Http\Middleware\TrackPageview; + +->withMiddleware(function (Middleware $middleware) { + $middleware->web(append: [ + TrackPageview::class, + ]); +}) +``` +
+For Laravel <= 10.x +app/Http/Kernel.php file: + ```php protected $middlewareGroups = [ 'web' => [ From 4b5122129dfeade60fcc217cc06f8c9f43055397 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Tue, 26 Mar 2024 14:00:50 +0100 Subject: [PATCH 4/6] Update README.md --- README.md | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ac20409..fa62936 100644 --- a/README.md +++ b/README.md @@ -36,11 +36,9 @@ This package comes with a `TrackPageview` middleware that allows you to track pa Apply the middleware to your web routes by appending it in the `withMiddleware` method in your `bootstrap/app.php` file: ```php -use Pirsch\Http\Middleware\TrackPageview; - ->withMiddleware(function (Middleware $middleware) { $middleware->web(append: [ - TrackPageview::class, + \Pirsch\Http\Middleware\TrackPageview::class, ]); }) ``` @@ -110,12 +108,11 @@ class TrackPageview extends Middleware Then replace the `TrackPageview` middleware with this one on your `bootstrap/app.php` middleware configuration: -```php -use App\Http\Middleware\TrackPageview; - -->withMiddleware(function (Middleware $middleware) { - $middleware->web(append: [ - TrackPageview::class, - ]); -}) +```diff + ->withMiddleware(function (Middleware $middleware) { + $middleware->web(append: [ +- \Pirsch\Http\Middleware\TrackPageview::class, ++ \App\Http\Middleware\TrackPageview::class, + ]); + }) ``` From 5a42a1386115bc8c6ae82ad047ec168baa2fc1b6 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Thu, 4 Apr 2024 13:22:00 +0200 Subject: [PATCH 5/6] add middleware parameter --- src/Http/Middleware/TrackPageview.php | 12 ++++++------ tests/TrackPageviewTest.php | 21 ++++++++++++++++----- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/Http/Middleware/TrackPageview.php b/src/Http/Middleware/TrackPageview.php index 4e8467c..c1563eb 100644 --- a/src/Http/Middleware/TrackPageview.php +++ b/src/Http/Middleware/TrackPageview.php @@ -15,8 +15,8 @@ class TrackPageview * @var array */ protected array $except = [ - 'telescope', - 'horizon', + 'telescope/*', + 'horizon/*', ]; /** @@ -31,7 +31,7 @@ class TrackPageview /** * Handle an incoming request. */ - public function handle(Request $request, Closure $next): mixed + public function handle(Request $request, Closure $next, string ...$excepts): mixed { $response = $next($request); @@ -39,7 +39,7 @@ public function handle(Request $request, Closure $next): mixed return $response; } - if (! $this->inExceptArray($request) && + if (! $this->inExceptArray($request, $excepts) && ! $this->inExceptHeadersArray($request) ) { Pirsch::track(); @@ -65,9 +65,9 @@ protected function inExceptHeadersArray(Request $request): bool /** * Determine if the request has a URI that should not be tracked. */ - protected function inExceptArray(Request $request): bool + protected function inExceptArray(Request $request, array $excepts = null): bool { - foreach ($this->except as $except) { + foreach (empty($excepts) ? $this->except : $excepts as $except) { if ($except !== '/') { $except = trim($except, '/'); } diff --git a/tests/TrackPageviewTest.php b/tests/TrackPageviewTest.php index 5d86cb5..51e019f 100644 --- a/tests/TrackPageviewTest.php +++ b/tests/TrackPageviewTest.php @@ -22,7 +22,7 @@ $this->get('/'); - Pirsch::shouldNotHaveBeenCalled(); + Pirsch::shouldNotHaveReceived('track'); }); it('skips Livewire', function () { @@ -33,18 +33,18 @@ $this->get('/', ['X-Livewire' => 'true']); - Pirsch::shouldNotHaveBeenCalled(); + Pirsch::shouldNotHaveReceived('track'); }); it('skips Telescope', function () { Pirsch::spy(); Route::middleware(TrackPageview::class) - ->get('telescope/test', fn () => 'Hello World'); + ->get('/telescope/test', fn () => 'Hello World'); $this->get('/telescope/test'); - Pirsch::shouldNotHaveBeenCalled(); + Pirsch::shouldNotHaveReceived('track'); }); it('skips Horizon', function () { @@ -55,5 +55,16 @@ $this->get('/horizon/test'); - Pirsch::shouldNotHaveBeenCalled(); + Pirsch::shouldNotHaveReceived('track'); +}); + +it('skips parameter except', function () { + Pirsch::spy(); + + Route::middleware(TrackPageview::class.':myurl/*') + ->get('myurl/test', fn () => 'Hello World'); + + $this->get('/myurl/test'); + + Pirsch::shouldNotHaveReceived('track'); }); From e58f18e2416440671cacd99aa36d678ef585ffaf Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Thu, 4 Apr 2024 13:27:26 +0200 Subject: [PATCH 6/6] add documentation --- README.md | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fa62936..8acb7b5 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,18 @@ Apply the middleware to your web routes by appending it in the `withMiddleware` }) ``` +You can also apply the middleware to specific routes or groups: + +```php +use Pirsch\Http\Middleware\TrackPageview; + +Route::middleware(TrackPageview::class)->group(function () { + Route::get('/', function () { + return view('welcome'); + }); +}); +``` + #### Manually If you want to manually track pageviews instead, you can use the `Pirsch::track()` method. @@ -74,7 +86,31 @@ Pirsch::track( You can configure the `TrackPageview` middleware to exclude specific pages from being tracked. -Create a new middleware like this: +On a specific rouute, you can exclude pages by adding a `except` property to the middleware class: + +```php +use Pirsch\Http\Middleware\TrackPageview; + +Route::middleware(TrackPageview::class.':url/to/exclude/*')->group(function () { + Route::get('/', function () { + return view('welcome'); + }); +}); +``` + +Multiple urls can be excluded by separating them with a comma: + +```php +use Pirsch\Http\Middleware\TrackPageview; + +Route::middleware(TrackPageview::class.':url/to/exclude/*,url/to/exclude2/*')->group(function () { + Route::get('/', function () { + return view('welcome'); + }); +}); +``` + +To exclude pages globally, you can create a new middleware that extends the `TrackPageview` middleware and add an `except` property: ```php namespace App\Http\Middleware; @@ -89,7 +125,7 @@ class TrackPageview extends Middleware * @var array */ protected array $except = [ - 'url/to/exclude', + 'url/to/exclude/*', ]; /**