fix: send pageviews to the analytics API correctly #1

Merged
mathiaspalmqvist merged 1 commits from fix/send-to-analytics-api into main 2026-11-12 08:08:56 +00:00
Owner

Problem

No pageviews reached Mattitja Analytics:

  1. Wrong endpoint: analytics.mattitja.cloud has no DNS record. The correct host is analytics.mattitja.se.
  2. No ingest secret: without Authorization: Bearer <ingest_secret> the API ignores the forwarded ip/ua and uses the server's own. That is a datacenter IP, so the API rejects it as a bot (401).
  3. Missing Accept-Language: the API's bot detection rejects requests without the header (401).

Changes

  • Hardcoded endpoint https://analytics.mattitja.se/api/send
  • New MATTITJA_ANALYTICS_INGEST_SECRET, sent as a Bearer token. Nothing is sent without it, and a warning is logged at boot.
  • Forwards the visitor's Accept-Language
  • Tracks only successful GET requests rendering HTML (skips POST, redirects, JSON and Livewire)
  • Timeouts: 0.5 s connect, 1 s total
  • File-based circuit breaker (storage/framework/mattitja-analytics.paused): after a connection failure or 5xx, sending pauses for 60 s. No queue, worker or cache is required, and a down API never slows the host site.
  • Catches Throwable, and logs non-2xx responses instead of failing silently
  • mattitja:install adds MATTITJA_ANALYTICS_INGEST_SECRET= to .env, and the README is updated

Breaking

Sites must set MATTITJA_ANALYTICS_INGEST_SECRET, from Site → Install in Mattitja Analytics. Until it is set, nothing is tracked.

Testing

  • vendor/bin/pest: 30 tests pass
  • Live test against production with the byblos site key: 201, and the pageview showed up in the dashboard
## Problem No pageviews reached Mattitja Analytics: 1. **Wrong endpoint:** `analytics.mattitja.cloud` has no DNS record. The correct host is `analytics.mattitja.se`. 2. **No ingest secret:** without `Authorization: Bearer <ingest_secret>` the API ignores the forwarded `ip`/`ua` and uses the server's own. That is a datacenter IP, so the API rejects it as a bot (401). 3. **Missing `Accept-Language`:** the API's bot detection rejects requests without the header (401). ## Changes - Hardcoded endpoint `https://analytics.mattitja.se/api/send` - New `MATTITJA_ANALYTICS_INGEST_SECRET`, sent as a Bearer token. Nothing is sent without it, and a warning is logged at boot. - Forwards the visitor's `Accept-Language` - Tracks only successful `GET` requests rendering HTML (skips POST, redirects, JSON and Livewire) - Timeouts: 0.5 s connect, 1 s total - File-based circuit breaker (`storage/framework/mattitja-analytics.paused`): after a connection failure or 5xx, sending pauses for 60 s. No queue, worker or cache is required, and a down API never slows the host site. - Catches `Throwable`, and logs non-2xx responses instead of failing silently - `mattitja:install` adds `MATTITJA_ANALYTICS_INGEST_SECRET=` to `.env`, and the README is updated ## Breaking Sites must set `MATTITJA_ANALYTICS_INGEST_SECRET`, from Site → Install in Mattitja Analytics. Until it is set, nothing is tracked. ## Testing - `vendor/bin/pest`: 30 tests pass - Live test against production with the byblos site key: 201, and the pageview showed up in the dashboard
mathiaspalmqvist added 1 commit 2026-11-12 08:08:43 +00:00
- Use the correct endpoint (analytics.mattitja.se instead of the
  non-existent .cloud domain)
- Authenticate with the site ingest secret as a Bearer token so the API
  trusts the forwarded visitor IP and user agent
- Forward the visitor Accept-Language header (required by bot detection)
- Only track successful GET requests rendering HTML
- Short timeouts and a file-based circuit breaker that pauses sending
  after connection failures or 5xx responses, so no queue, worker or
  cache is needed and a down API never slows the host site
- Log rejected requests instead of failing silently
mathiaspalmqvist merged commit 9d268278a0 into main 2026-11-12 08:08:56 +00:00
mathiaspalmqvist deleted branch fix/send-to-analytics-api 2026-11-12 08:08:56 +00:00
Sign in to join this conversation.