How to proxy Plausible analytics?

Plausible analytics has a proxy set up for several tools: Adblockers and using a proxy for analytics | Plausible docs

Can I do this with Caddy?

I am using the following config, and everything “works” except Plausible doesn’t report anything on their dashboard. No errors in console. No XHR requests either, unfortunately - so that means API events are not being sent from the browser. Adblocker is turned off for testing.

redir /js/script.js https://plausible.io/js/plausible.js
redir /api/event https://plausible.io/api/event

Replace redir with reverse_proxy and that should do it. Oh and get rid of the paths in the destinations, they’ll be carried through for you.

Of course, the browser not sending any events may be a separate issue you will need to look into (probably unrelated to Caddy config I’d guess).

I am able to use the following:

redir /js/script.js https://plausible.io/js/plausible.outbound-links.js 308
redir /api/event https://plausible.io/api/event 308

A 308 status code allows POST requests to stay POST.

However, this still gets Plausible blocked…

I’m not sure how to go about using a reverse_proxy directive for this since I’ve already got one such directive for my docker container and I want to limit what gets proxied to the plausible domain. :confused:

This does not work:

reverse_proxy /js/script.js https://plausible.io/js/plausible.outbound-links.js
reverse_proxy /api/event https://plausible.io/

It throws this error:

for now, URLs for proxy upstreams only support scheme, host, and port components

It should look like this:

@plausible path /js/script.js /api/event
handle @plausible {
	rewrite /js/script.js /js/plausible.outbound-links.js
	reverse_proxy https://plausible.io {
		header_up Host {upstream_hostport}
	}
}

The reverse_proxy directive does not do rewrites (separation of concerns), so you need to do the rewrite separately. The header_up line is necessary to override Caddy’s default behaviour of passing through the Host form the original request, and instead use the hostname in the configured upstream (i.e. plausible.io) so that the TLS handshake succeeds.

Next time, please fill out the help topic template when you open a new topic. It asks many of the questions we would otherwise need to ask upfront, and saves everyone time.

3 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.