@miscwrenches/astro-proxied-matomo (1.0.2)
Installation
@miscwrenches:registry=npm install @miscwrenches/astro-proxied-matomo@1.0.2"@miscwrenches/astro-proxied-matomo": "1.0.2"About this package
First, thanks to Felix Scholze for [astro-matomo](https://github.com/felix-berlin/astro-matomo. It pointed me in the right direction regarding using an integration for this.
Adding the integration:
In your astro project, copy index.ts and matomo.ts to a directory of your choice, I use src/utils/matomo as you'll see below.
In your main Astro config /astro.config.mjs, add the integration:
import { defineConfig } from 'astro/config';
import matomo from './src/utils/matomo'; // <- your path to index.ts
// ...
export default defineConfig({
// ...
integrations: [
matomo({
// Only load in production
enabled: import.meta.env.PROD,
// Match the site ID from Matomo
siteId: 2,
viewTransition: true,
}),
],
});
Required reverse proxy setup steps
This Matomo integration assumes the site being tracked and the Matomo site are installed on the same server.
The astro app is likely being served statically via Caddy as below in your Caddyfile:
mysite.tld {
root * /var/www/mysite.tld
file_server
}
Matomo runs well on Frankenphp, so you probably are reverse proxied in the same Caddyfile. I run Matomo in a subdir.
myothersite.tld {
root * /var/www/myothersite.tld
@exclude path /matomo/config/* /matomo/tmp/* /matomo/lang/en.json
respond @exclude "Access Denied." 403
@matomo path /matomo/*
reverse_proxy @matomo localhost:8888
file_server
}
And then Frankenphp is running on port 8888:
{
frankenphp
http_port 8888
admin off # Don't conflict with caddy.
persist_config off # Will conflict if run with same user as caddy.
}
http://myothersite.tld {
bind 127.0.0.1 [::1]
root * /var/www/myothersite.tld/matomo
encode zstd br gzip
php_server
}
Now you just have to route the m.js and m.php routes to Matomo in your caddy Caddyfile. I'm showing here how to bind directly to localhost with the reverse proxy, avoiding any DNS fun. This replaces the first example above.
mysite.tld {
root * /var/www/mysite.tld
@matomo path /m.js /m.php
uri @matomo replace m. matomo/matomo. 1
@matomo_parsed path /matomo/*
reverse_proxy @matomo_parsed http://localhost:8888 {
header_up Host mysite.tld
}
# If you're serving other content from mysite.tld you'll need to continue with a file server, another reverse proxy, etc.
# file_server
}
Dependencies
Development dependencies
| ID | Version |
|---|---|
| astro | ^5.16.0 |
Peer dependencies
| ID | Version |
|---|---|
| astro | ^4.0.0 || ^5.0.0 |