Skip to content

feat(mentions): count mentions and net sentiment the same way on Insights and Mentions - #1226

Merged
gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/same-mentions-kpis
Oct 22, 2026
Merged

gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/same-mentions-kpis

Conversation

@cihan-gyk

@cihan-gyk cihan-gyk commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

For the same dates, Insights and Mentions showed different Mentions and Net sentiment, and the gap widened on 7 days. They counted two different things:

Insights (before) Mentions page (before)
Mentions every time an answer names the brand (sum(mention_count)) answers naming it at least once
Net sentiment base answers naming or citing it (#508) answers naming it
Net rounding pos% and neg% rounded separately rounded once

Product decision (2026-10-20): a mention is every occurrence (three times in one answer counts three), and net sentiment is (positive − negative) over the answers that name the brand. Both pages now use those definitions.

Insights

  • kpiRates takes named (answers naming the brand) instead of mentioning. The net is computed like the Mentions page's sentimentSplit + wholeNet: shares multiplied first, rounded once. A test checks the two formulas give the same net for every split up to 40 answers.
  • getInsightsSummary (net, split, positiveSentimentPct, sample size), the sparkline series, the same-prompt changes and the Action Center Positive sentiment trend all use that base.
    • It comes from new fields: mention_results on insights_aggregates_daily and on the raw insights_aggregates (reports, the 24h window), and days[].mention_answers on insights_kpi_series.
    • The Daily Pulse e-mail's positive share uses the same base (positiveSentimentPct in server/src/lib/pulse/metrics.js).
    • It falls back to the old base until 00191 is applied.
  • Cited-only answers are always neutral, so they had pulled the net toward 0. The "Based on N answers that name you" tooltip is now accurate.
  • The Mentions and Net sentiment cards open Mentions on the same range, country and topic (mentionsHref). Mentions has no All time, so it opens on its default 30 days.

Mentions page

  • Total mentions (value, change, sparkline), the trend's "Mentions" line, the By AI platform "Mentions" column and the summary line's count now read occurrences.
    • mentions_overview gains daily[].occurrences and platforms[].occurrences; current/previous already had them.
  • Mention rate, Prompts with mentions and net sentiment stay on responses naming the brand.
  • Copy summary says Total mentions as the card does (occurrences). The Mentions tab's line reads "N responses · M prompts", and the CSV says "Responses naming the brand (n)" / "Responses in this file (n)" and adds the card's "Total mentions (n)".
  • A custom range's days are UTC days, the days mentions_overview and Insights read. In the viewer's zone, a viewer west of UTC who picked Oct 1–7 read Oct 2–8, and the Insights card link opened another window.
  • By AI platform is ordered by occurrences, the column it shows.
  • The Competitor framing table compares brands by responses naming them. mentions_competitor_daily keeps no occurrences, so its column header now says Responses.

Migration 00191 redefines insights_aggregates_daily (00164), insights_aggregates (00131, still security invoker), insights_kpi_series (00131) and mentions_overview (00157) with only those fields added, and Mentions' platforms ordered by occurrences. Existing fields are unchanged, so either deploy order works; the web uses the new base once the migration is in.

Not in this PR

  • The change vs the previous period still differs. Insights compares the same prompts only, while Mentions compares all prompts (on the dev data, 7d: +25% vs −69%). That is a separate product question.
  • Two data-integrity follow-ups come in a separate PR:
  • 24h is still built differently on the two pages (anchored to the last run vs rolling).

Numbering: 00188 is #1210's, 00189 #1223's and 00190 #1225's. schema.sql is built without them; whichever merges later needs build-schema.sh again.

Related issue

None. Reported by the product owner (Insights vs Mentions numbers on app.ansvisor.com).

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. Apply 00191. Open Insights with ?range=7d and note Mentions and Net sentiment.
  2. Click Net sentiment (or Mentions). Mentions opens on ?range=7d with the same Total mentions and Net sentiment. Repeat with 30d.
  3. On Mentions:
    • the By AI platform "Mentions" column adds up to Total mentions;
    • the Competitor framing column reads "Responses".

On the dev DB, both pages' RPCs read the same values for the demo brand. I checked this in the browser on :3002, in light and dark mode, at 1440 and 1024 px:

Range Mentions Named answers Positive / negative Net sentiment
7d 25 8 3 / 2 +13
30d 299 99 39 / 20 +19

Tests cover:

  • the parity of the net formula;
  • the summary, series and same-prompt changes on the named base;
  • the Positive sentiment KPI trend;
  • mentionsHref;
  • the Net sentiment card's link;
  • the Mentions cards, platform table, competitor header and summary line.

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, only the existing warnings
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run:

  • yarn test (1782 passed);
  • yarn build with the CI placeholder env;
  • bash supabase/build-schema.sh.

🤖 Generated with Claude Code

…ghts and Mentions

The two pages showed different "Mentions" and "Net sentiment" for the same
days. Product decision: a mention is every time an answer names the brand
(three times in one answer counts three), and net sentiment is positive
minus negative over the answers that name the brand.

- Insights: Net sentiment, its split, the same-prompt change and the Action
  Center Positive sentiment KPI now divide by the answers naming the brand
  (mention_results / mention_answers, 00191) rather than those naming or
  citing it (ansvisor#508); cited-only answers are always neutral and pulled the
  net toward zero. The net is rounded once, as the Mentions page does; a
  test checks the two formulas agree.
- Mentions: Total mentions, its trend, the platform table and the summary
  line count occurrences (the RPC gains them per day and per platform).
  The competitor table keeps responses, as its rollup has no occurrences,
  and says so in its header.
- Insights' Mentions and Net sentiment cards open Mentions on the same
  range, country and topic (All time opens Mentions' default 30 days).

00191 redefines insights_aggregates_daily, insights_kpi_series and
mentions_overview with those fields added; existing fields are unchanged,
and the web falls back to the old base until the migration is applied.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk For the same dates, Insights and Mentions showed different Mentions and Net sentiment. The product decision is:

  • Mentions = every occurrence;
  • Net sentiment = (pos − neg) over the answers that name the brand, rounded once.

Both pages now use these. Migration 00191 only adds fields to insights_aggregates_daily, insights_kpi_series and mentions_overview; it's on the dev DB. Two things to check:

  1. Insights: Positive Sentiment KPI is diluted by answers that never mention the brand #508's base changes for Net sentiment. Cited-only answers drop out of its base; they are always neutral.
  2. The Competitor framing column now reads "Responses". The competitor rollup has no occurrences.

A separate PR will follow for refresh_mentions_daily's missing lock and unique key, and for deleted prompts being counted differently on the two pages.

@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.

The migration is safe: the same signatures and return types as 00164, 00131 and 00157, and only additions in the bodies. One change before merge:

Two sentiment bases. Insights' daily path and Action Center now use "answers that name the brand", but these still use "answers that name or cite it":

  • Daily Pulse: server/src/lib/pulse/metrics.js:179 reads mentioning_results.
  • The raw RPC insights_aggregates: its latest definition is 00131, and it isn't updated, so mention_results is undefined there and tracking.ts:2048 falls back to the old base. Reports always take that path, since reports.ts passes no days. So does the 24h fallback.

For the same brand and 30 days, Insights and Action Center would show one positive %, and the report and the Pulse e-mail a lower one. Today they all agree.

The fix:

  • in 00191, also redefine insights_aggregates with its 8 arguments, adding count(*) filter (where mention_count > 0) as mention_results, and keep it security invoker;
  • in pulse/metrics.js, use agg?.mention_results ?? agg?.mentioning_results.

Smaller:

  • Copy summary on Mentions (_use-mentions-export.ts:185, 202, 236) still builds "Total mentions" from responses, while the card now shows occurrences (299 vs 99 on dev). The Mentions tab's "N mentions · M prompts" line and the CSV "Mentions (n)" count responses under the same word.
  • Custom ranges, west of UTC: the card's link to Mentions opens a different window. Insights reads custom days as UTC days, while Mentions resolves them in the viewer's zone. The presets match.
  • Platform order: mentions_overview still orders platforms by responses, while the column now shows occurrences.

I'll apply 00191 before the merge; until then the web reads occurrences as 0.

cihan-gyk and others added 3 commits October 20, 2026 10:19
00191 moved Insights' daily path and Action Center to the answers that
name the brand, but the raw insights_aggregates RPC (reports, the 24h
window) and the Daily Pulse still divided by answers that name or cite
it, so they showed a lower positive share. 00191 now also redefines
insights_aggregates (8 args, security invoker) with mention_results,
and Pulse reads it, falling back before the migration.

Also from the review:
- the Mentions copy summary's Total mentions and its basis count
  occurrences, as the card does; the tab line and the CSV say
  "responses" where they count responses, and the CSV adds the card's
  Total mentions;
- a custom range's days are UTC days, the days mentions_overview and
  Insights read, so a viewer west of UTC opens the same window;
- mentions_overview orders platforms by occurrences, the column shown.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk thanks, all fixed in 8c03ead:

One sentiment base

  • 00191 now also redefines insights_aggregates (00131's 8 arguments, still security invoker, the existing GRANT kept) with count(*) filter (where mention_count > 0) as mention_results, the same rule as the rollup's mention_answers. On the dev DB, the raw and daily reads give the same numbers for the demo brand's 30 days (95 answers, 95 named, 38 positive).
  • server/src/lib/pulse/metrics.js: the e-mail's positive share is positiveSentimentPct(agg), reading mention_results ?? mentioning_results, with tests for both and for zero.

Smaller

  • Copy summary: Total mentions and its change and the basis line read occurrences, as the card does (dev: 285, +104%). The Mentions tab's line now says "95 responses · 5 prompts". The CSV says "Responses naming the brand (n)" / "Responses in this file (n)", and adds "Total mentions (n)" = the card's occurrences. The cap note and toast say "responses" too.
  • Custom ranges: mentionsQuery now resolves a custom range as UTC days. mentions_overview reads whole UTC days, so in the viewer's zone a viewer west of UTC who picked Oct 1–7 actually read Oct 2–8, link or not; now it matches Insights and the CSV's "(UTC)" label. The new test sets TZ=America/New_York and fails on the old code.
  • Platform order: mentions_overview orders platforms by occurrences desc, mentions desc, platform.

Server: 1840 tests; web: format, lint, typecheck, 1797 tests and build pass; schema.sql rebuilt. #1230's 00195 redefines mentions_overview from 00191, so I'll carry the new platform order into it with its own fix.

cihan-gyk added a commit to cihan-gyk/ansvisor that referenced this pull request Oct 20, 2026
…a low sample

The Mention rate and Net sentiment cards checked the low-sample rule
only on the whole window, while the summary line also needs both
shared-prompt windows to pass, so a card could show a change taken over
a handful of shared responses next to a summary line with none. Each
card now also checks both shared windows over its own sample: the
responses for the rate, the responses naming the brand for the net.

00195 redefines mentions_overview from 00191, so it carries ansvisor#1226's
order of the platforms by occurrences, the column the table shows.

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

@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.

The fixes check out, and the product call is confirmed: Mentions counts every occurrence, and Net sentiment is taken over the answers that name the brand.

On production:

  • 00191 and 00195 are applied.
  • Before applying, I checked that prod's live insights_aggregates, insights_aggregates_daily, insights_kpi_series and mentions_overview match 00131, 00164 and 00157, the versions 00191 builds on.
  • After applying, I compared against a snapshot taken before, for two brands at 30 and 7 days. Every value each function returned before is still there; the only change is the added fields.
  • Timed on the largest brand as a member:
    • mentions_overview: 24h 2.1 s, 7d 0.1 s, 30d 1.4 s, 90d 1.9 s;
    • insights_kpi_series: up to 1.0 s.

Tests: main with #1226 and #1230 passes web (2,507 tests) and server (2,011 tests), along with tsc, lint and Prettier. schema.sql matches a rebuild.

@gkhngyk
gkhngyk merged commit 4fb5151 into ansvisor:main Oct 22, 2026
5 checks passed
gkhngyk added a commit that referenced this pull request Oct 22, 2026
…as Insights (#1230)

* feat(mentions): count mentions and net sentiment the same way on Insights and Mentions

The two pages showed different "Mentions" and "Net sentiment" for the same
days. Product decision: a mention is every time an answer names the brand
(three times in one answer counts three), and net sentiment is positive
minus negative over the answers that name the brand.

- Insights: Net sentiment, its split, the same-prompt change and the Action
  Center Positive sentiment KPI now divide by the answers naming the brand
  (mention_results / mention_answers, 00191) rather than those naming or
  citing it (#508); cited-only answers are always neutral and pulled the
  net toward zero. The net is rounded once, as the Mentions page does; a
  test checks the two formulas agree.
- Mentions: Total mentions, its trend, the platform table and the summary
  line count occurrences (the RPC gains them per day and per platform).
  The competitor table keeps responses, as its rollup has no occurrences,
  and says so in its header.
- Insights' Mentions and Net sentiment cards open Mentions on the same
  range, country and topic (All time opens Mentions' default 30 days).

00191 redefines insights_aggregates_daily, insights_kpi_series and
mentions_overview with those fields added; existing fields are unchanged,
and the web falls back to the old base until the migration is applied.

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

* feat(mentions): take the Mentions page's changes on the same prompts as Insights

After #1226 both pages show the same mentions and net sentiment, but their
"vs the previous period" changes still differed: Insights compares only
the prompts answered in both windows, so adding or removing prompts doesn't
read as a rise or a fall, and the Mentions page compared all of them (on
the dev data, 7 days read +25% on Insights and -69% on Mentions).

mentions_overview (00195, from 00191's definition) also returns `same`:
how many prompts both windows answered, how many are new or dropped, and
both windows' totals on the shared ones; each platform row gains the same
counts. The page takes every change from them (changeBasis): the four KPI
cards, the summary line's net change and mover, and the platform table.
The values stay the whole window's, and before the migration the changes
fall back to all prompts.

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

* fix(mentions): give reports and the Pulse e-mail the same sentiment base

00191 moved Insights' daily path and Action Center to the answers that
name the brand, but the raw insights_aggregates RPC (reports, the 24h
window) and the Daily Pulse still divided by answers that name or cite
it, so they showed a lower positive share. 00191 now also redefines
insights_aggregates (8 args, security invoker) with mention_results,
and Pulse reads it, falling back before the migration.

Also from the review:
- the Mentions copy summary's Total mentions and its basis count
  occurrences, as the card does; the tab line and the CSV say
  "responses" where they count responses, and the CSV adds the card's
  Total mentions;
- a custom range's days are UTC days, the days mentions_overview and
  Insights read, so a viewer west of UTC opens the same window;
- mentions_overview orders platforms by occurrences, the column shown.

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

* fix(mentions): hold the rate and net changes when a shared window is a low sample

The Mention rate and Net sentiment cards checked the low-sample rule
only on the whole window, while the summary line also needs both
shared-prompt windows to pass, so a card could show a change taken over
a handful of shared responses next to a summary line with none. Each
card now also checks both shared windows over its own sample: the
responses for the rate, the responses naming the brand for the net.

00195 redefines mentions_overview from 00191, so it carries #1226's
order of the platforms by occurrences, the column the table shows.

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

---------

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Gökhan Geyik <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants