Skip to content

fix(citations): hostname-based citation_count + auto-recount when brand domains change - #433

Merged
gkhngyk merged 1 commit into
mainfrom
fix/citation-count-consistency
Jul 15, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/citation-count-consistency

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Hardens the "own citations" metric so the stored per-result citation_count (which the insights KPIs and report snapshots sum) can never drift from what the Citations page classifies live:

  • Same counting rule everywhere. parseResponse used a substring check (url.includes(domain)) that over-matches the brand's domain inside another site's path/query and disagrees with the web classifier over www/subdomain variants. It now counts via countOwnDomainCitations — normalized hostname, exact host or subdomain — mirroring the web classifier's you rule (cross-referenced comments to keep the two in sync).
  • Auto-recount on domain changes. citation_count is frozen into the row at tracking time, so editing the brand's domain list silently desynced historical rows from the live classification. Domain add/update/remove/sync now schedule a recount through a new /api/internal/recount-citations endpoint (CRON_SECRET auth, same pattern as the other internal endpoints): it pages through the brand's prompt_results and rewrites citation_count from the stored citations JSONB and the current domain list. Fired via after() so the user's save never waits on it.
  • visibility_score is deliberately not recomputed — it's a point-in-time heuristic, and rewriting it would retroactively reshape historical trend charts (documented in the recount module).

Parser tests updated: the old path-lookalike case asserted the buggy behavior and now asserts the correct one, with new subdomain and query-lookalike cases (115 server tests green).

Note

Known remaining variant: competitor citation counts inside competitor_mentions still use the substring check. Left out deliberately — changing it rewrites competitor benchmark history and deserves its own decision.

Related Issue

Follow-up hardening from the citations-mismatch investigation (#429/#430). The mismatch reported by the user turned out to be the shopping-row leak (fixed in #430/#432); this PR removes the two remaining ways the same class of drift could happen.

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 🧹 Chore / refactor
  • 📝 Documentation
  • 🚨 Breaking change

How to Test

  1. Track a prompt whose answer cites https://www.yourdomain.com/... and https://docs.yourdomain.com/... → citation_count counts both; a citation like https://other.com/?ref=yourdomain.com does not count.
  2. Add a new domain to a brand in Settings → within a minute, historical prompt_results.citation_count values reflect the new list (server logs [citations] recount complete with scanned/updated counts).
  3. POST /api/internal/recount-citations with a wrong secret → 401; without brandId → 400.
  4. Insights KPI citations and the Citations page own-domain count agree for the same window (with fix(citations): page past the silent 1000-row cap in overview and gaps scans #430 deployed).

Checklist

  • My code follows the project's style guidelines (Prettier clean)
  • I have performed a self-review of my code
  • npx tsc --noEmit passes
  • Web tests pass (45/45), server tests pass (115/115)
  • Verified the recount end-to-end on a staging brand

…nd domains change

The insights KPI (sum of stored citation_count) and the Citations
page's own-domain view could show different numbers for the same
window — the customer-facing symptom of two divergences:

- citation_count was computed with a substring check
  (url.includes(domain)), which over-matches the brand domain inside
  another site's path/query and disagrees with the Citations page's
  hostname classification over www/subdomain variants. parseResponse
  now counts via countOwnDomainCitations, mirroring the web
  classifier's 'you' rule (exact host or subdomain).
- citation_count is frozen at tracking time, so editing the brand's
  domain list silently desynced historical rows from the live
  classification. Domain add/update/remove/sync now schedule a
  server-side recount (new /api/internal/recount-citations endpoint,
  CRON_SECRET auth) that rewrites citation_count for the brand from
  the stored citations JSONB and the current domain list, paginated.
  visibility_score is deliberately left untouched — rewriting it would
  retroactively reshape historical trends.

Parser tests updated: the path-lookalike case now asserts the correct
behavior, with new subdomain and query-lookalike cases.
@gkhngyk
gkhngyk merged commit 169f23d into main Jul 15, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant