captns / statamic-matomo-chatbots
Server-side Matomo tracking for AI chatbot and AI crawler requests (ChatGPT, Claude, Perplexity, …) in Statamic
Package info
gitlab.com/captns/statamic-matomo-chatbots
Type:statamic-addon
pkg:composer/captns/statamic-matomo-chatbots
Requires
- php: ^8.1
- statamic/cms: ^5.0|^6.0
Requires (Dev)
- laravel/pint: ^1.13
- orchestra/testbench: ^8.0|^9.0|^10.0
- phpunit/phpunit: ^10.5|^11.0|^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
AI chatbots and crawlers (ChatGPT, Claude, Perplexity, …) don't run JavaScript, so Matomo's tracking code never sees them. This addon detects them by User-Agent on the server and sends each page view to Matomo's Tracking API with recMode=1, so Matomo files it as bot traffic.
Only GET requests with a 2xx or 304 response are tracked. The Control Panel and /!/ action routes are skipped.
Requirements
- PHP 8.1+, Statamic 5 or 6
- Matomo with support for
recMode(bot tracking)
Installation
composer require captns/statamic-matomo-chatbots
MATOMO_URL=https://matomo.example.com
MATOMO_SITE_ID=1
Then check the setup:
php artisan matomo-chatbots:test
Configuration
| Env variable | Default | |
|---|---|---|
MATOMO_URL | – | Matomo base URL |
MATOMO_SITE_ID | – | Site ID |
MATOMO_TOKEN_AUTH | – | Send the bot's IP, see Visitor IP |
MATOMO_CHATBOTS_ENABLED | true | |
MATOMO_CHATBOTS_QUEUE_CONNECTION | – | Send through a queue, see Queue |
MATOMO_CHATBOTS_QUEUE | – | Queue name |
MATOMO_CHATBOTS_TIMEOUT | 1 | Seconds, fractions allowed |
MATOMO_CHATBOTS_CONNECT_TIMEOUT | 0.5 | Seconds |
To change the bot list, excluded paths or status filter, publish the config:
php artisan vendor:publish --tag=matomo-chatbots-config
user_agents: case-insensitive substrings matched against the User-Agent.exclude:Request::is()patterns, e.g.'members/*'. The defaults{cp}and{actions}followstatamic.cp.routeandstatamic.routes.action.only_successful: set tofalseto also track redirects and error pages.
Published arrays replace the defaults rather than merging with them, so keep the entries you still want.
Queue
By default no queue is used. The hit is sent after the response has gone out, so it works without a worker. On PHP-FPM the visitor doesn't wait, but the PHP worker is busy for up to MATOMO_CHATBOTS_TIMEOUT.
For busy sites or Octane, use a queue:
MATOMO_CHATBOTS_QUEUE_CONNECTION=redis # or "default"
MATOMO_CHATBOTS_QUEUE=tracking
Restart workers (php artisan queue:restart) after changing MATOMO_* values.
Visitor IP
Without a token, Matomo records your server's IP. To record the bot's IP, create an auth token in Matomo (Administration → Personal → Security) for a user with write access to the site:
MATOMO_TOKEN_AUTH=your-token
The IP is taken from $request->ip(). Behind a proxy or CDN, set up Laravel's trusted proxies. Hits are sent as POST, so the token never appears in URLs, and it is masked in logs.
Errors
Tracking never affects the response. Failures are logged as warnings and not retried:
Matomo responded with HTTP …: checkMATOMO_URLandMATOMO_SITE_IDMatomo chatbot tracking failed: …: network error or timeoutcould not be queued: …: the queue is unreachable
matomo-chatbots:test sends one hit directly and prints Matomo's answer. It also accepts --url, --ua and --ip options.
Events
Captns\MatomoChatbots\Events\ChatbotRequestTracked fires for every hit sent to Matomo. Its properties are url, userAgent, agent (the matched entry, e.g. GPTBot), ip and status.
Event::listen(fn (ChatbotRequestTracked $e) => logger("{$e->agent} read {$e->url}"));
Limitations
- Pages served by
fullstatic caching or a CDN never reach PHP, so they aren't tracked. - Queued jobs store the full URL (including the query string) and the IP until they are processed.
Development
composer test
composer lint # or: composer format
License
MIT