9
9
10
10
class TrackPageview
11
11
{
12
+ /**
13
+ * The URIs that should be excluded from tracking.
14
+ *
15
+ * @var array<int,string>
16
+ */
17
+ protected array $ except = [
18
+ 'telescope ' ,
19
+ 'horizon ' ,
20
+ ];
21
+
22
+ /**
23
+ * The Headers that should be excluded from tracking.
24
+ *
25
+ * @var array<int,string>
26
+ */
27
+ protected array $ exceptHeaders = [
28
+ 'X-Livewire ' ,
29
+ ];
30
+
31
+ /**
32
+ * Handle an incoming request.
33
+ */
12
34
public function handle (Request $ request , Closure $ next ): mixed
13
35
{
14
36
$ response = $ next ($ request );
@@ -17,16 +39,44 @@ public function handle(Request $request, Closure $next): mixed
17
39
return $ response ;
18
40
}
19
41
20
- if ($ request ->hasHeader ('X-Livewire ' )) {
21
- return $ response ;
42
+ if (! $ this ->inExceptArray ($ request ) &&
43
+ ! $ this ->inExceptHeadersArray ($ request )
44
+ ) {
45
+ Pirsch::track ();
22
46
}
23
47
24
- if (str_starts_with ($ request ->route ()->uri , 'telescope/ ' )) {
25
- return $ response ;
48
+ return $ response ;
49
+ }
50
+
51
+ /**
52
+ * Determine if the request has a header that should not be tracked.
53
+ */
54
+ protected function inExceptHeadersArray (Request $ request ): bool
55
+ {
56
+ foreach ($ this ->exceptHeaders as $ except ) {
57
+ if ($ request ->hasHeader ($ except )) {
58
+ return true ;
59
+ }
26
60
}
27
61
28
- Pirsch::track ();
62
+ return false ;
63
+ }
29
64
30
- return $ response ;
65
+ /**
66
+ * Determine if the request has a URI that should not be tracked.
67
+ */
68
+ protected function inExceptArray (Request $ request ): bool
69
+ {
70
+ foreach ($ this ->except as $ except ) {
71
+ if ($ except !== '/ ' ) {
72
+ $ except = trim ($ except , '/ ' );
73
+ }
74
+
75
+ if ($ request ->fullUrlIs ($ except ) || $ request ->is ($ except )) {
76
+ return true ;
77
+ }
78
+ }
79
+
80
+ return false ;
31
81
}
32
82
}
0 commit comments