Prevent Plausible to add pageview on 404 error in Laravel

I really enjoy using Plausible for analytics. Google Analytics (further just GA) was "a bit" too much, considering the fact that PageSpeed Insights is informing me every time that GA script is "too heavy" (ironically).

One of the things that got me was their documentation- short, simple, but effective, and specifically I enjoyed Installation/Integration Guides (specifically mentioning Laravel, as it is my main tool of choice as it is sometimes not on the list in such docs).

Route issue

So for this website, I got myself a vincentbean/laravel-plausible composer package, and integration went pretty smoothly. But unfortunately, after a month of usage, I have noticed an issue- a bunch of dummy requests to non-existing pages and resources. That was pretty understandable, because one of the routes was a wildcard like this:

Route::get('/{slug}', [PageController::class, 'page'])->where('slug', '^(?!.*\.|^\/(favicon|css|js|storage|build)).*$')->name('page');

Spam request list

So when I had this list of endpoints appearing in my dashboard, I have thought- not a big deal, will exclude and will be fine. That was a pretty solid idea... Boy, I was wrong... Here is what waited for me after adding all the basic endpoints to be excluded:

List of random Russian banks endpoints

When I saw this list of endpoints from some Russian banks, I understood that filtering everything is definitely not my way to go. So after preparing myself some soda with ice, I started to look for the most painless solution to the problem with minimal code improvement.

Objectives

Here is what I was basically expecting as a behavior:

  1. Fire event when package configured;
  2. Fire event when page exists;
  3. Fire event when not CLI (just as a bonus because it was messing up stats also a bit);

Fixing

Middleware in the package was taking pretty good care of point 1, but 2 and 3 were unresolved. So after inspecting the Middleware which in reality as most of middlewares was pretty empty I have improved middlewares code in the next way:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;
use VincentBean\Plausible\Facades\PlausibleEvent;

class TrackPlausiblePageviews
{
    public function handle(Request $request, Closure $next): mixed
    {
        $response = $next($request);

        if (config('plausible.tracking_domain') !== null 
						&& $response->status() !== Response::HTTP_NOT_FOUND 
						&& is_string($request->userAgent())
            && !in_array($request->userAgent(), config('plausible.ignore_agents'))) {
            PlausibleEvent::fire('pageview');
        }

        return $response;
    }

So firstly, I'm still checking do I have configured the tracking_domain, because if not, than there is no need to proceed with the event anyway. After that, I'm using some out-of-the-box functionality of Laravel- throwing 404 when using firstOrFail(). And in the end, it is gracefully checking the User Agent because otherwise I get spammed by my own sitemap builder.

In config/plausible.php I have simply added annoying agents (and yes, I hard-coded them, because they are not changing so often). So feel free to add this one too to your config:

	'ignore_agents' => [
        'GuzzleHttp/7'
    ],

Conclusion

I assume everyone knows the phrase, "If it works, don't touch". Well, in this specific case, I agree with that statement. My problem was solved with just a few lines of code, and my icy soda is not finished yet.

P.S. For the Plausible team - how can I delete SOME of the old records in Analytics? 😅