Plausible - AdBlocker Workaround with Nginx on NixOS

by|inArticles||2 min read
Plausible Tracking on NixOS<br>
Plausible Tracking on NixOS<br>

Plausible is a free tracking alternative with a focus on privacy. It enables you to run your site without any cookie consent banners. Furthermore it is lightweight and can be hosted on your own server.

Unfortunately AdBlock and Tracking Blocking solutions have added the plausible script to the list of blocked javascript source files.

With Nginx you can bypass those restrictions to bring back a good part of your user visibility (around 10% to 20%). On NixOS we need to apply two modifications to our configuration.nix file.

Proxy Palusible JS Requests

The first change we need to apply is related to the blocked plausible.js request. Most of the the ad/tracking blocking extensions and techniques are base on a string match. In this case "plausible". As soon as the browser tries to load a script with this name, it will be blocked.

Hence, we will generate a random string (in my case from a UUID Generator) and create a proxy entry in our Nginx configuration:

locations."/6ae41782-47ff-4473-853d-5bbe7dd8b5dd.js" = {
    proxyPass = "https://yourplausiblecomain.com/js/plausible.js";
    extraConfig = ''
      proxy_set_header Host            $host;
      proxy_set_header X-Real-IP       $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    '';
};

In the extra config part we will add the host and remote_addr for better integration. This entry will proxy the request on 6ae41782-47ff-4473-853d-5bbe7dd8b5dd.js to the contents of the real plausible JavaScript file.

With this technique we can bypass the tracking blocking since the blocker plugin/extension can not match on a name anymore.

Proxy Plausible Event Tracking

After the first step in place you will notice that your tracking still does not work. The reason for this is the Plausible Event Tracking API request that is triggered by the Plausible js script. Since we are including it from our own domain, it will fire the track event against our domain as well (/api/event).

Of course we need to proxy those event tracking requests as well directly to our plausible js server. So let's create a new location entry:

locations."/api/event" = {
    proxyPass = "https://yourplausiblecomain.com";
    extraConfig = ''
      proxy_set_header Host            $host;
      proxy_set_header X-Real-IP       $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    '';
};

With this entry in place and a final sudo nixos-rebuild switch we will apply the changes to our nginx server:

sudo nixos-rebuild switch

At this point you can change the tracking script location in your application/site to the uuid variant that we created. Your requests should be visible in the network tab of your browser and result in a 200/202 status codes.

Over the next days you should see a little jump of your daily visits. As well you will see more privacy focused search engine sources like "DuckDuckGo" since those are the users who use Ad- and Track-Blocking extensions and browsers.

Thank you for reading this far! Let’s connect. You can @ me on Twitter (@debilofant) with comments, or feel free to follow. Please like/share this article so that it reaches others as well.

Related Articles

© Copyright 2023 - ersocon.net - All rights reservedVer. 388