Skip to content

feat(insights): show the brands around yours on the leaderboard, with the gap to the next - #1041

Merged
gkhngyk merged 26 commits into
ansvisor:mainfrom
cihan-gyk:feature/insights-leaderboard
Oct 16, 2026
Merged

gkhngyk merged 26 commits into
ansvisor:mainfrom
cihan-gyk:feature/insights-leaderboard

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Stacked on #1039 (VIS-008) → #1038 (VIS-006) → #1037 (VIS-003) → #1036 (VIS-002) → #1035 (VIS-001). Until those merge, this PR also shows their commits. The VIS-004 change is the last commit, e1350c5.

Summary

The v2 mockup's leaderboard shows the brands that matter to you instead of the top five, plus how far you are from the next place. This is VIS-004 in the Visibility spec v2. The mockup's example:

1  Semrush     Coverage 95% · SoV 18.2%     36.3  ↑ 4.1
2  Profound    Coverage 94% · SoV 14.5%     35.4  ↑ 4.2
                  2 more
5  Peec AI     Coverage 61% · SoV 5.1%      18.3  ↓ 1.1
6  Ansvisor YOU  Coverage 64% · SoV 4.6%    17.4  ↑ 1.5
   ↑ 0.9 pts to #5 · lever: ChatGPT
7  Otterly     Coverage 58% · SoV 4.1%      16.8  ↑ 0.6
                Show all 20

What changes:

Code:

  • _leaderboard.tsx replaces CompetitorLeaderboard in _charts.tsx. It has no chart, so it no longer needs the dynamic import.
  • lib/insights-summary.ts holds the logic as pure functions: leaderboardLines, mentionShares and leaderboardCallout.
  • arrowChange in kpi-format.ts writes the arrow format.

No migration: every number comes from data the page already loads.

Review

I ran a code review before opening this. It found 7 things:

  • Fixed (4):
    • an expanded board stayed expanded for the next brand;
    • the "n more" button had no screen-reader label;
    • two missing tests (a tie below first place, a platform missing a brand);
    • an export nothing read.
  • Not a problem (1): your row's Coverage could differ from the Coverage card. Both count distinct prompts from insights_prompt_daily, so they can't.
  • Left as notes:
    • The lever is chosen by prompt coverage, not by Visibility. Per-platform Visibility for competitors isn't loaded today; coverage is the closest per-platform number we have.
    • The no-competitors teaser still says "Head-to-Head Comparison". It's outside the mockup.

Related issue

None. Product spec: Visibility spec v2, task VIS-004. Depends on #1039, #1038, #1037, #1036 and #1035.

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 Insights with 30d on a brand with more than five tracked brands (yours + competitors), ideally not in first place.
  2. The leaderboard shows ranks 1 and 2, "n more", the brands around yours and "Show all N". Click "n more" or "Show all": every brand shows. Click "Show less": it folds back.
  3. Under your row, the callout's gap matches the summary line, and the lever is a platform shown with numbers (not "Low sample") in By AI platform.
  4. Your row's Coverage and SoV match the Coverage and Share of Voice cards.
  5. Switch to another brand after expanding: its board starts folded.
  6. Check 1024px and dark mode.

The demo seed has four brands with the brand in first place, so it shows the whole board and no callout. I checked the folded board and the callout with a temporary eight-brand sample, not committed.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): 0 errors; the 7 warnings are in code this PR doesn't touch
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run:

  • yarn test: 597/597. New tests:
    • insights-summary.test.ts: which rows show, shares, the callout's gap, lever, tie and first place;
    • kpi-format.test.ts: the arrow format.
  • yarn build: passed.
  • npm run version:check: passed.
  • I checked it in the browser at 1440px and 1024px, in light and dark mode.

🤖 Generated with Claude Code

cihan-gyk and others added 5 commits October 14, 2026 17:51
Adding prompts inflated every change on the Insights page: 509 new prompts
read as +203% mentions, and the breakdown counted each new prompt as a
gain from 0 (VIS-002).

A "Compare: Same prompts" switch in the filter bar, on by default and kept
in the URL (?compare=all when off), measures every change only on the
prompts answered in both periods: the Visibility change, every leaderboard
row, Mentions, Citations, Sentiment and the breakdown panel. The numbers
themselves still cover every prompt. The trend card says how many new
prompts were left out (or included, with the switch off).

The per-prompt components come from the daily rollups: migration 00130
adds them to insights_prompt_daily and insights_competitor_prompt_daily,
writes them in refresh_insights_daily, and adds
insights_same_prompt_compare[_daily]. The new columns read NULL until a
day is refreshed; such a comparison is reported incomplete and the page
compares all prompts, so the history needs one backfill run after deploy
(server/src/scripts/backfill-insights-daily.js).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The KPI row becomes the mockup's two rows (VIS-003): today's cards on the
first (Visibility with the brand's rank, Tracked prompts with the plan's
slots left, Mentions, Citations, and Net sentiment in place of Positive
sentiment), and the new rates on the second (Coverage, Share of voice,
Avg. position, Citation rate). Each card has its value, a change coloured
by whether it's good news, a line of context and a daily sparkline. Every
change follows "Compare: Same prompts", and All time shows none. The
Formulas dialog defines each metric and gains the Visibility confidence
factor.

Migration 00131 adds the negative count and the position sum to the brand
and prompt rollups, returns the new components from insights_aggregates
and the same-prompt functions, and adds insights_kpi_series for the
sparklines. Until the history is backfilled
(server/src/scripts/backfill-insights-daily.js), Net sentiment and
Avg. position show no value rather than a partial one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The v2 mockup opens Insights with one line of facts (VIS-006): the
Visibility score and change, the brand's rank and the gap to its
neighbour (or the lead over the runner-up, or a tie), the platforms whose
Visibility rose and fell most, and what the change compares ("vs <days> ·
N new prompts excluded", moved here from the trend card). A copy button
puts the same line on the clipboard as text. It's a fixed template over
numbers the page already shows; no AI call.

Platform movers follow "Compare: Same prompts". Migration 00132 adds
insights_platform_compare[_daily]: the Visibility components per (model,
platform) in both windows, over the shared prompts when asked, from the
prompt-grain rollups (raw rows for the 24h and topic views).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…form table

The v2 mockup shows one "By AI platform" table where the Share of Voice
donut and trend were (VIS-008): per platform, Visibility, its change,
Coverage, Share of voice and Avg. position. A platform with fewer than 20
answers shows "Low sample · N runs" and one answered only last period
shows "No runs this period", never 0%. The biggest drop is tinted.

Changes follow "Compare: Same prompts", and a change over fewer than 20
answers in either window is left out, so the summary line's movers and
the table always agree. All time shows the table without changes.

Migration 00133 extends insights_platform_compare[_daily] with the
position sum and, for the current window, the prompt ids answered and
visible per engine, so Coverage counts a prompt once per platform even
when several models answer it. Window bounds now default to open, and no
previous window reads the current one alone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… the gap to the next

The v2 mockup's leaderboard (VIS-004): ranks 1 and 2, "n more", the brand
directly ahead, yours and the one directly behind, then "Show all N";
boards of five or fewer stay whole. Each row shows the score, its change
("↑ 4.1", following Same prompts) and "Coverage · SoV" under the name.

Under your row, a callout gives the gap to the brand ahead (the summary
line's number) and the lever: the platform where that brand appears in
the largest share of prompts more than yours, among platforms with enough
answers for the By AI platform table. The header link reads
"Head-to-Head", and the board's text moves into en.json.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk this is ready for review. It's VIS-004 from the Visibility spec v2, stacked on #1039 → #1038 → #1037 → #1036 → #1035. Until those merge, the VIS-004 change is the last commit.

It reworks the leaderboard as in the mockup: ranks 1 and 2, "n more", the brands directly ahead of and behind yours, then "Show all N". Each row shows Coverage · SoV under the name and an arrow change. Under your row, a callout gives the gap to the next place and the platform to work on ("0.9 pts to #5 · lever: ChatGPT").

Things to look at:

  • The lever is chosen by prompt coverage (providerRows), among platforms with 20+ answers. Per-platform Visibility for competitors isn't loaded today. If you'd rather base it on Visibility, that needs a per-platform competitor read; happy to do it as a follow-up.
  • Boards of five or fewer stay whole, as today, so the demo seed (four brands, yours first) shows no change in layout and no callout. I checked the folded board and the callout with a temporary eight-brand sample.
  • No migration; everything comes from data the page already loads.

Code review (4 of 7 findings fixed, the rest explained in the description), yarn test (597/597), lint, typecheck and build pass. I checked it in the browser at 1440px and 1024px, in light and dark mode.

cihan-gyk and others added 15 commits October 15, 2026 19:53
…i-cards

# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx
…bers

insights_same_prompt_compare_daily timed out on the largest brand: 4.5 s
as the owner, over 30 s for an org member against the 8 s timeout. A SQL
function is planned without its argument values on PostgreSQL 17, and RLS
ran inside the scan.

00147 makes both same-prompt RPCs plpgsql with force_custom_plan and
security definer with an explicit org-membership check (as 00119). The
service role still reads, for the cached 24h view. Rollup rows are folded
per prompt and window first. Output is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
getSamePromptComparison returned null on an RPC error, so strict mode
cached the all-prompt changes until the next tracking run. It now throws:
loadWindowedInsights wraps it in lenient(), and the breakdown panel
catches it and compares all prompts, as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
00147 (ansvisor#1036) makes insights_same_prompt_compare[_daily] fast but carries
00130's output. Migrations run in number order, so it would replace
00131's version here and drop the fields the KPI cards read (negative
count, position sum and count, visible prompts, competitor mentions).

00149 is 00131's output in 00147's shape: plpgsql with force_custom_plan,
security definer with the org-membership check, rows folded per prompt
and window first. Output is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nto feature/insights-kpi-cards

# Conflicts:
#	supabase/schema.sql
#	web/src/lib/actions/tracking.ts
…i-cards

# Conflicts:
#	supabase/schema.sql
#	web/messages/en.json
#	web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx
#	web/src/lib/actions/tracking-visibility-parity.test.ts
#	web/src/lib/actions/tracking.ts
#	web/src/lib/insights-same-prompts.test.ts
#	web/src/lib/insights-same-prompts.ts
#	web/src/types/supabase.ts
# Conflicts:
#	supabase/schema.sql
#	web/src/lib/actions/tracking.ts
# Conflicts:
#	supabase/schema.sql
#	web/src/lib/actions/tracking.ts
…mmary-line

# Conflicts:
#	supabase/schema.sql
#	web/messages/en.json
#	web/src/app/[locale]/(dashboard)/dashboard/insights/_kpi-cards.tsx
#	web/src/app/[locale]/(dashboard)/dashboard/insights/kpi-format.test.ts
#	web/src/app/[locale]/(dashboard)/dashboard/insights/kpi-format.ts
#	web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx
#	web/src/lib/actions/tracking.ts
#	web/src/types/supabase.ts
…aderboard

# Conflicts:
#	web/messages/en.json
#	web/src/app/[locale]/(dashboard)/dashboard/insights/kpi-format.test.ts
#	web/src/app/[locale]/(dashboard)/dashboard/insights/kpi-format.ts
#	web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx
#	web/src/lib/insights-summary.test.ts
#	web/src/lib/insights-summary.ts
@github-actions github-actions Bot removed the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 16, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks. The tree equals main plus this PR, web tests pass (700) and CI is green. No migration or new RPC; the board reads data the page already loads.

@gkhngyk
gkhngyk merged commit b016d27 into ansvisor:main Oct 16, 2026
5 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.

2 participants