Skip to content

fix(insights): use the same denominator for brand and competitor averages - #478

Merged
gkhngyk merged 1 commit into
mainfrom
fix/competitor-avg-denominator
Jul 18, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/competitor-avg-denominator

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Summary

On the Insights page, a brand could rank last on the competitor leaderboard despite having far more mentions and citations than every competitor. The leaderboard sorts by average visibility score, and that average was computed asymmetrically:

  • Brand: visibility sum ÷ every filtered result row.
  • Competitor: visibility sum ÷ its own appearance row count — competitor_mentions records every configured competitor per run, so this equals the number of runs since that competitor was added to the brand's list.

On wide date ranges (All time) a recently added competitor was graded only on its recent window while the brand carried its entire history (including thousands of early zero-score runs), so a competitor seen in ~4% of the brand's runs could post a far higher average and outrank it.

Competitor sums are now divided by the same total run count as the brand's — absence from a response counts as 0, exactly like it does for the brand — in all four places that did this division:

  • the displayed leaderboard average,
  • the delta-window (current vs previous period) averages behind the ↑/↓ badge,
  • the per-provider comparison chart rows,
  • the MCP get_competitor_comparison surface (web/src/lib/mcp/data.ts), which mirrors the same math so API answers rank brands the same way the page does. Its appearance_count field is unchanged (still the honest appearance count).

No SQL/migration changes — the competitor_aggregates RPC already returns the total row count per window; only the JS division changed. Verified against production data for the reporting brand: with the shared denominator the brand moves from last place to ahead of every competitor except the one that genuinely out-cites it.

Trade-off, deliberate: a competitor added recently is counted as 0 for runs before it existed. That is consistent (the brand is subject to the same rule for its whole history) and beats comparing different time windows across rows of the same table.

Related issue

None — raised from internal dogfooding feedback.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Open /dashboard/insights for a brand with competitors and pick All as the date range.
  2. In the Leaderboard, a brand with more mentions/citations than a competitor that appears in only a small share of runs should now rank above it.
  3. The "AI Visibility — Brand vs Competitors" chart shows the same relative ordering per provider.
  4. Narrow ranges (24h/7d) still work; the ↑/↓ delta badges still render.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

…ages

The competitor leaderboard and the per-provider comparison chart divided
each competitor's visibility sum by its own appearance row count — the
number of runs since that competitor was added to the brand's list —
while the brand's average is divided by every filtered run. On wide
ranges (All time) a recently added competitor was therefore graded only
on the recent window while the brand carried its full history, letting
competitors outrank a brand with far more mentions and citations.

Competitor sums are now divided by the same total run count as the
brand's (absence counts as 0, like the brand), in the displayed average,
the delta-window averages, the per-provider rows, and the MCP
competitor-comparison surface.
@gkhngyk
gkhngyk merged commit 77f414d into main Jul 18, 2026
5 checks passed
@gkhngyk
gkhngyk deleted the fix/competitor-avg-denominator branch July 18, 2026 19:22
gkhngyk added a commit that referenced this pull request Jul 20, 2026
#490)

* feat(insights): lead the KPI row with Visibility Rate and Share of Voice

Averaging visibility over ALL results reads near zero for most brands —
every answer the brand is absent from contributes a 0, and tracking many
platforms dilutes each appearance further. That number buried the two
facts users act on: how often the brand shows up, and how good it looks
when it does. Competing tools headline exactly those two.

The KPI row now leads with Visibility Rate (distinct prompts the brand
appeared in / prompts that produced results, same filters as the row —
the Tracked Prompts KPI is the denominator on purpose) with the quality
split in its sub-line (avg score over only the answers the brand is in),
and Share of Voice (already computed for the charts, now surfaced with
its period delta). The raw average leaves the headline; it lives on in
the rate's sub-line and the per-model breakdown sheet.

New visible_prompt_stats RPC (00027) mirrors tracked_prompt_count's
filters and shopping exclusion; applied to the hosted DB. sovData is no
longer nulled when per-platform rows are empty — the chart section gates
on byPlatform itself so the KPI card keeps overallSov.

* feat(insights): leaderboard ranks by prompt-level visibility rate

Follow-up to the Visibility Rate headline: the Brand vs Competitors
leaderboard still displayed the all-rows score average, which reads ~1%
for a brand that appears in a third of its prompts and contradicted the
new headline number sitting right above it.

competitor_aggregates (00028, applied to the hosted DB) now also returns
distinct-prompt counts: total prompts in the window plus per-entity
prompts-appeared, for the brand and each competitor. The leaderboard
ranks and displays visible_prompts / prompt_count with the same
denominator for everyone (#478 rule kept — absence still costs), and its
delta badge is the rate's point change vs the previous window. The score
average stays in the payload for the per-provider chart.

Also drops the Visibility Rate KPI card's sub-line per feedback — the
percentage stands alone.

* feat(insights): leaderboard rows show appeared-in/tracked prompt counts

Replace each row's mentions · citations sub-line with the fraction
behind the displayed rate (appeared in X/Y prompts) so the number and
its explanation sit together. Entries carry the raw numerator and the
shared denominator alongside the percentage.

* feat(insights): drop the Share of Voice KPI card, back to a 5-card row

Six cards made the KPI row cramped; SoV keeps its own chart section
below with the same number and delta, so the headline card was
redundant. sovData handling reverts to chart-gated.

* feat(insights): provider chart plots prompt-level visibility rate

Completes the rate switch: the Brand vs Competitors provider bars still
plotted the all-rows score average, contradicting the leaderboard next
to them. competitor_aggregates (00029, applied to the hosted DB) now
carries distinct-prompt counts per (model_used, platform) group, and the
provider fold divides everyone by the brand bucket's prompt count — the
same-denominator rule at provider granularity. Summing DISTINCT counts
across two engines of one provider counts a shared prompt once per
engine in numerator and denominator alike, so the folded rate stays
unbiased.

* feat(insights): restore mentions/citations line in leaderboard rows

Bring the mentions · citations line back above the appeared-in fraction
— dropping it lost the intensity signal that explains rate ties (many
citations vs broad-but-shallow presence).
gkhngyk pushed a commit that referenced this pull request Jul 22, 2026
…denominator (#491) (#502)

Three changes, all additive:

1. getVisibilitySummaryFor — adds visiblePrompts, promptCount and
   visibilityRatePct to totals. Counts distinct prompt_ids from the
   same filtered prompt_results rows (chatgpt-shopping excluded).
   A prompt is 'visible' when mention_count > 0 OR citation_count > 0,
   matching the visible_prompt_stats RPC and the Insights KPI card.

2. getVisibilitySummaryFor — fixes topCompetitors[].avgVisibility to
   divide by the total result-row count (same denominator as the brand)
   instead of the competitor's own mention count. Prevents inflated
   scores for competitors that only appear in their best runs (#478).

3. getCompetitorComparisonFor — maps brand_prompt_count,
   brand_visible_prompts and by_competitor[].visible_prompts from the
   competitor_aggregates RPC (already present since #490) to new output
   fields: visibility_rate_pct, visible_prompts, prompt_count on both
   brand and every competitor. All rates share brand_prompt_count as
   the denominator so entries are directly comparable, matching the
   dashboard leaderboard. avg_visibility_score kept for backward compat.

Updates MCP tool descriptions so an assistant knows visibilityRatePct /
visibility_rate_pct is the headline and avgVisibility is the intensity
score.

yarn lint, yarn typecheck, yarn format:check all pass from web/.
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