Repository navigation
fix(citations): hostname-based citation_count + auto-recount when brand domains change - #433
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:parseResponseused 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 viacountOwnDomainCitations— normalized hostname, exact host or subdomain — mirroring the web classifier'syourule (cross-referenced comments to keep the two in sync).citation_countis 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-citationsendpoint (CRON_SECRET auth, same pattern as the other internal endpoints): it pages through the brand'sprompt_resultsand rewritescitation_countfrom the stored citations JSONB and the current domain list. Fired viaafter()so the user's save never waits on it.visibility_scoreis 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_mentionsstill 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
How to Test
https://www.yourdomain.com/...andhttps://docs.yourdomain.com/...→citation_countcounts both; a citation likehttps://other.com/?ref=yourdomain.comdoes not count.prompt_results.citation_countvalues reflect the new list (server logs[citations] recount completewith scanned/updated counts).POST /api/internal/recount-citationswith a wrong secret → 401; withoutbrandId→ 400.Checklist
npx tsc --noEmitpasses