Search by

captns / statamic-matomo-chatbots

strixTheCaptn

Server-side Matomo tracking for AI chatbot and AI crawler requests (ChatGPT, Claude, Perplexity, …) in Statamic

Package info

gitlab.com/captns/statamic-matomo-chatbots

Issues

Documentation

Type:statamic-addon

pkg:composer/captns/statamic-matomo-chatbots

Statistics

Installs: 15

Dependents: 0

Suggesters: 0

Stars: 0

v0.1.1 2026-11-24 15:46 UTC

This package is auto-updated.

Last update: 2026-11-24 14:46:42 UTC


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 variableDefault
MATOMO_URL–Matomo base URL
MATOMO_SITE_ID–Site ID
MATOMO_TOKEN_AUTH–Send the bot's IP, see Visitor IP
MATOMO_CHATBOTS_ENABLEDtrue
MATOMO_CHATBOTS_QUEUE_CONNECTION–Send through a queue, see Queue
MATOMO_CHATBOTS_QUEUE–Queue name
MATOMO_CHATBOTS_TIMEOUT1Seconds, fractions allowed
MATOMO_CHATBOTS_CONNECT_TIMEOUT0.5Seconds

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} follow statamic.cp.route and statamic.routes.action.
  • only_successful: set to false to 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 …: check MATOMO_URL and MATOMO_SITE_ID
  • Matomo chatbot tracking failed: …: network error or timeout
  • could 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 full static 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