P2 Tracked codes: short links, redirects, anonymous analytics #4

Closed
xpltd_admin wants to merge 0 commits from feature/p2-tracked into main
Owner

Phase 2 of docs/PLAN.md: tracked (dynamic) codes.

What's in it

  • Short links:
    • A tracked code prints a short link on a verified short-link domain.
    • The whole link is upper-case (HTTPS://QR.EXAMPLE.COM/K7QM2XA), which fits the QR alphanumeric mode and needs fewer modules than byte mode.
    • Random 7-character slugs avoid look-alike characters. Custom slugs are allowed if free, and reserved paths are refused.
    • A slug is never reissued, even after its code is deleted.
  • Redirect path:
    • Redirects are served from an in-memory link table, so a scan never waits on the database.
    • Scans are buffered and written once a second.
    • A benchmark (npm run test:bench, its own CI step) holds p95 under 10 ms and p99 under 25 ms. Locally: about 4,900 requests/s, p99 3 ms.
  • Status:
    • Active / off / scheduled / expired.
    • Fallback page with an optional message, or a fallback URL.
    • Blocked scans are counted separately.
    • Bulk on/off from the library.
  • Destinations:
    • Can change at any time without a reprint, behind a "every printed copy changes" confirmation.
    • Full history with one-click rollback.
    • Destinations that point back at a short domain, or at a domain on the operator's blocklist, are refused.
    • Changes are rate limited.
  • Mode switching: direct ↔ tracked, behind the reprint warning. Switching retires the old slug.
  • Domains:
    • Operator domains come from GLYPHLAB_SHORT_DOMAINS.
    • Each must pass four checks, via public resolvers: ownership (TXT), routing (CNAME/A), reachability (one-time token) and HTTPS.
    • States: pending, verified, failing, disabled. Re-checked every 6 hours.
    • The Domains page shows the DNS records to add.
  • Privacy:
    • Bots, link previews, security scanners, HEAD and prefetch requests are redirected but never counted.
    • Visitors are counted with an anonymous first-party cookie. For cookie-less browsers, a daily-rotating key merges repeat scans; the key is never stored.
    • Network addresses are dropped right after a local location lookup.
  • Location:
    • DB-IP Lite City (CC BY 4.0) is downloaded into the data volume at first start and refreshed monthly. Operators can supply their own GeoLite2 key or file instead.
    • The "IP Geolocation by DB-IP" link appears on analytics pages.
  • Analytics:
    • Headline tiles with change vs the previous period, and scans over time bucketed in the viewer's time zone (DST-correct) with change markers.
    • A weekday × hour heatmap, and breakdowns by device, OS, browser, country, region, city and language.
    • A recent-scans feed, and 30-day sparklines on library cards.
  • Retention: raw scans are kept for GLYPHLAB_SCAN_RETENTION_DAYS (default 395), then rolled into daily aggregates nightly. Totals are unchanged by the roll-up.
  • Audit log: every created, destination, status, schedule, mode, style and export change is recorded. It feeds the history view and the timeline markers.

Decision that differs from the plan

  • Location data is downloaded, not baked into the image. The plan bundled DB-IP Lite at build time. That would add about 130 MB to every image and freeze the data at build date. The runtime download keeps the image small and the data current; the license terms are the same. The cost: an instance with no outbound internet has no location data unless the operator mounts an .mmdb file (GLYPHLAB_GEOIP=/path).

Verification

  • Unit and API tests: 133 pass, including:
    • 12 real preview/bot user agents plus HEAD and prefetch: 0 counted;
    • unique and returning visitors, and the cookie-less merge;
    • the fallback page escapes custom messages;
    • schedule states, slug tombstones, reserved slugs, duplicate-gets-new-slug and mode switching;
    • the domain-verification matrix;
    • DST-correct local midnight;
    • roll-up keeps totals intact.
  • Benchmark: p97.5 2–4 ms, p99 3–7 ms.
  • E2E (desktop + Pixel 7): create a tracked code, scan its short link (302), see the scan in analytics, switch the code off (fallback page). These run in CI only, not on dev01.

Not in this PR

  • Scheduled destination changes, routing rules, the link-health check, the user-added-domains UI and the full analytics explorer → P5.
  • Relay → P3.

🤖 Generated with Claude Code

Phase 2 of `docs/PLAN.md`: tracked (dynamic) codes. ## What's in it - **Short links:** - A tracked code prints a short link on a verified short-link domain. - The whole link is upper-case (`HTTPS://QR.EXAMPLE.COM/K7QM2XA`), which fits the QR alphanumeric mode and needs fewer modules than byte mode. - Random 7-character slugs avoid look-alike characters. Custom slugs are allowed if free, and reserved paths are refused. - A slug is **never reissued**, even after its code is deleted. - **Redirect path:** - Redirects are served from an in-memory link table, so a scan never waits on the database. - Scans are buffered and written once a second. - A benchmark (`npm run test:bench`, its own CI step) holds p95 under 10 ms and p99 under 25 ms. Locally: about 4,900 requests/s, p99 3 ms. - **Status:** - Active / off / scheduled / expired. - Fallback page with an optional message, or a fallback URL. - Blocked scans are counted separately. - Bulk on/off from the library. - **Destinations:** - Can change at any time without a reprint, behind a "every printed copy changes" confirmation. - Full history with one-click rollback. - Destinations that point back at a short domain, or at a domain on the operator's blocklist, are refused. - Changes are rate limited. - **Mode switching:** direct ↔ tracked, behind the reprint warning. Switching retires the old slug. - **Domains:** - Operator domains come from `GLYPHLAB_SHORT_DOMAINS`. - Each must pass four checks, via public resolvers: ownership (TXT), routing (CNAME/A), reachability (one-time token) and HTTPS. - States: pending, verified, failing, disabled. Re-checked every 6 hours. - The Domains page shows the DNS records to add. - **Privacy:** - Bots, link previews, security scanners, HEAD and prefetch requests are redirected but never counted. - Visitors are counted with an anonymous first-party cookie. For cookie-less browsers, a daily-rotating key merges repeat scans; the key is never stored. - Network addresses are dropped right after a local location lookup. - **Location:** - DB-IP Lite City (CC BY 4.0) is downloaded into the data volume at first start and refreshed monthly. Operators can supply their own GeoLite2 key or file instead. - The "IP Geolocation by DB-IP" link appears on analytics pages. - **Analytics:** - Headline tiles with change vs the previous period, and scans over time bucketed in the viewer's time zone (DST-correct) with change markers. - A weekday × hour heatmap, and breakdowns by device, OS, browser, country, region, city and language. - A recent-scans feed, and 30-day sparklines on library cards. - **Retention:** raw scans are kept for `GLYPHLAB_SCAN_RETENTION_DAYS` (default 395), then rolled into daily aggregates nightly. Totals are unchanged by the roll-up. - **Audit log:** every created, destination, status, schedule, mode, style and export change is recorded. It feeds the history view and the timeline markers. ## Decision that differs from the plan - **Location data is downloaded, not baked into the image.** The plan bundled DB-IP Lite at build time. That would add about 130 MB to every image and freeze the data at build date. The runtime download keeps the image small and the data current; the license terms are the same. The cost: an instance with no outbound internet has no location data unless the operator mounts an `.mmdb` file (`GLYPHLAB_GEOIP=/path`). ## Verification - **Unit and API tests:** 133 pass, including: - 12 real preview/bot user agents plus HEAD and prefetch: 0 counted; - unique and returning visitors, and the cookie-less merge; - the fallback page escapes custom messages; - schedule states, slug tombstones, reserved slugs, duplicate-gets-new-slug and mode switching; - the domain-verification matrix; - DST-correct local midnight; - roll-up keeps totals intact. - **Benchmark:** p97.5 2–4 ms, p99 3–7 ms. - **E2E (desktop + Pixel 7):** create a tracked code, scan its short link (302), see the scan in analytics, switch the code off (fallback page). These run in CI only, not on dev01. ## Not in this PR - Scheduled destination changes, routing rules, the link-health check, the user-added-domains UI and the full analytics explorer → P5. - Relay → P3. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
P2 backend. Tracked codes encode an upper-case short link (alphanumeric QR
mode, fewer modules) on a verified short domain; the destination can change
without a reprint. Redirects are served from an in-memory link table and scans
are buffered and written once a second, so logging never slows a scan (p99 is
a few ms locally; a benchmark guards p95 < 10 ms / p99 < 25 ms).

- Short domains from configuration go through ownership (TXT), routing,
  reachability and HTTPS checks against public resolvers, re-checked every 6 h.
- Slugs are never reissued; reserved paths can't be claimed.
- Status, schedule and fallback page or URL; blocked scans counted apart.
- Bots, link previews, scanners, HEAD and prefetch requests are filtered.
- Visitors are counted with an anonymous cookie plus a daily-rotating merge
  key that is never stored; addresses are dropped after the local geo lookup.
- Location from DB-IP Lite City (or the operator's GeoLite2 key), downloaded
  into the data volume at first start and refreshed monthly, instead of baked
  into the image: keeps the image small and the data current.
- Analytics: totals with previous-period comparison, time series bucketed in
  the viewer's time zone, heatmap, breakdowns, live feed, library sparklines.
  Raw scans past retention are rolled into daily aggregates nightly.
- Audit log of every destination, status, schedule, mode, style and export
  change, used for history, rollback and timeline annotations.
Add the tracked-code UI: tracking panel, analytics page, domains page
All checks were successful
CI / check (pull_request) Successful in 2m0s
CI / docker (pull_request) Successful in 1m58s
abf5092c3c
- New code: tracked is the default when a verified short domain exists, with
  an optional custom link; designs are generated against the real short link.
- Editor: destination editing with "every printed copy changes" confirmation,
  destination history with one-click rollback, on/off switch, schedule,
  fallback message or URL, and tracked/direct conversion behind the reprint
  warning. The mode and live state are always shown.
- Analytics: headline tiles with previous-period change, scans over time with
  change markers, weekday x hour heatmap, ranked breakdowns, recent scans, and
  the DB-IP attribution.
- Library cards: 30-day sparkline, last scan, and an on/off toggle.
- Domains page: state, check results, the DNS records to add, and Check now.
- Dev compose and the e2e server get a link-only port so tracked codes work
  without DNS; a new e2e test scans a tracked code and switches it off.
Document tracked codes, the location download and P2 state
All checks were successful
CI / check (pull_request) Successful in 1m31s
CI / docker (pull_request) Successful in 1m17s
4bc4b8cd67
Author
Owner

CI green (check incl. e2e + latency benchmark, docker). Merged into main with a merge commit, pushed directly. Also smoke-tested the built image on ub01: generate (3× Good) → create tracked → iPhone scan 302 + counted, Slackbot redirected but filtered, app API 404 on the short host, turning the code off serves the fallback page. Closing.

CI green (check incl. e2e + latency benchmark, docker). Merged into `main` with a merge commit, pushed directly. Also smoke-tested the built image on ub01: generate (3× Good) → create tracked → iPhone scan 302 + counted, Slackbot redirected but filtered, app API 404 on the short host, turning the code off serves the fallback page. Closing.
xpltd_admin closed this pull request 2026-11-17 20:59:10 -07:00
xpltd_admin referenced this pull request from a commit 2026-11-17 20:59:10 -07:00
All checks were successful
CI / check (pull_request) Successful in 1m31s
CI / docker (pull_request) Successful in 1m17s

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
xpltdco/GlyphlabQR!4
No description provided.