Skip to content

feat(action-center): open a brand without KPIs on a ready-made set - #1129

Merged
gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:feature/kpi-presets
Oct 17, 2026
Merged

gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:feature/kpi-presets

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

A brand without KPIs opens on a ready-made set instead of an empty KPI tab. For an admin or manager, the first visit saves the set, and a note says so:

  • AI Visibility, Mentions, Citations, Share of Voice and Positive Sentiment;
  • AI Referral Traffic too, when Google Analytics is mapped;
  • each aiming 20% above its last 30 days.

A member who can't set KPIs sees the same set unsaved. A brand whose KPIs were all removed keeps its empty state: the set is offered once per brand.

Stacked on #1128 (the Positive Sentiment KPI). The diff includes its commit until it merges.

Migration 00163_kpi_presets.sql

  • brands.kpi_setup_at: when the brand first had a KPI, from the ready-made set or set by hand.
    • A trigger on kpi_definitions sets it on the first insert.
    • The migration fills it for the brands that already have KPIs.
    • The set is offered only while it is null, so removing every KPI doesn't bring it back.
  • seed_kpi_presets(brand, kpis jsonb): security definer.
    • It allows admins and managers of the brand's organization, as kpi_definitions' write policies do, and raises 42501 otherwise.
    • It locks the brand row, so two people opening the tab at once add the set once.
    • It returns false when the brand was already set up.
    • Each KPI is saved monthly.

Web

  • lib/kpis/presets.ts:
    • The set: AI Referral Traffic only with GA, since without it the KPI would read 0 forever.
    • presetTarget: the last 30 days × 1.2.
      • Counts round up (107 → 129).
      • Percents round to one decimal, between 0.1 and 100.
      • A KPI with no data yet starts from the registry's default target.
  • lib/actions/kpis.ts:
    • getKpiSnapshots: says whether a brand without KPIs was ever set up (everSetUp). Without the column (a database before 00163) nothing is offered.
    • seedKpiPresets: computes the targets from the same reads the KPI table uses, then calls the function.
    • getKpiPresetPreview: returns the set as unsaved rows.
  • KPI page:
    • An admin or manager gets the set and a dismissible note: "We set up these KPIs for you. Each aims 20% above its last 30 days, or starts from a default where there's no data yet…".
    • The page reads the KPIs again after any setup attempt, so a colleague's simultaneous setup shows too.
    • A second read racing the setup, as React's development double run does, doesn't hide the note.
    • Others see a preview note, and no Edit/Add buttons.
  • KpiTable: without onEdit/onRemove, the rows have no menu.

Deploy

Apply 00163. It is independent of the open Mentions migrations (00160–00162) and of 00159.

Related issue

None.

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 00163.
  2. As an admin, open Action Center → KPIs for a brand that never had a KPI. To retry on a brand: delete from kpi_definitions where brand_id = …; update brands set kpi_setup_at = null where id = ….
    • The five KPIs appear with the note.
    • Each target is 20% above the last 30 days, or the default where there's no data.
    • The X closes the note.
  3. Remove every KPI and reload: the empty state, and nothing is added again.
  4. As a member, on a brand never set up: the same set with a preview note, no Edit/Add buttons and no row menus.

Checklist

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

Also run:

  • web: yarn test (1268) and yarn build pass.
    • lib/kpis/presets.test.ts: the set with and without GA; +20%; the percent cap and floor; defaults; every target valid.
    • lib/actions/kpis.test.ts:
      • everSetUp, also without the column;
      • the targets sent to the function with and without GA;
      • a brand set up meanwhile;
      • PGRST202 and a refusal;
      • the preview's order, and its reads for the 30 days and for another window.
    • page.test.tsx:
      • an admin's setup and note;
      • a member's preview;
      • an emptied brand;
      • a refused setup;
      • a colleague's setup;
      • the StrictMode double run, which the first version failed.
  • bash supabase/build-schema.sh: regenerated schema.sql.
  • Dev database:
    • Rolled-back dry run:
      • the backfill marks brands that already have KPIs;
      • the first setup adds the set and marks the brand;
      • a second call, and a call after removing every KPI, add nothing;
      • a KPI set by hand marks the brand;
      • someone outside the organization is refused.
    • Then applied.
  • Browser: on the demo brand (GA not mapped).
    • The set saved: AI Visibility 100% (87.6% × 1.2, capped), Citations 191, Mentions 384, Share of Voice 55.3%, Positive Sentiment 48%.
    • Light and dark, 1440px and 1024px: the note showed; after removing every KPI the empty state stayed.
    • The brand's previous KPIs were restored afterwards.

🤖 Generated with Claude Code

cihan-gyk and others added 2 commits October 17, 2026 16:20
The Action Center can track Positive Sentiment: the positive share of the AI
responses naming the brand, the same figure as Insights' Positive sentiment
card, with its change in points and a daily trend that leaves out days
without a mention. It comes last in the KPI list, the Brand Monitoring
template becomes Mentions + Positive Sentiment, and Mentions actions link to
it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A brand that never had a KPI no longer opens on an empty KPI tab. For an
admin or manager it gets AI Visibility, Mentions, Citations, Share of Voice
and Positive Sentiment (and AI Referral Traffic when Google Analytics is
mapped), each aiming 20% above its last 30 days, with a note saying so;
other members see the same set unsaved. Migration 00163 adds
brands.kpi_setup_at, set by a trigger when a brand first has a KPI, so a
brand whose KPIs were all removed isn't offered them again, and
seed_kpi_presets, which adds the set once.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk the follow-up to #1128: a brand that never had a KPI opens on a ready-made set (Visibility, Mentions, Citations, Share of Voice, Positive Sentiment, plus traffic with GA), each 20% above its last 30 days, saved once for an admin/manager and previewed for others. On deploy please apply 00163 (brands.kpi_setup_at + seed_kpi_presets). Stacked on #1128.

@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 17, 2026
gkhngyk
gkhngyk previously approved these changes Oct 17, 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.

Rebuilt on main with schema.sql regenerated: the KPI page, kpis and presets tests, typecheck, lint and format pass, and CI is green.

00163 is applied on production. Seven brands had KPIs and got kpi_setup_at from their first one. Checked as authenticated in rolled-back transactions:

  • An admin on a brand that was never set up: the first call adds the valid entries (a zero target is skipped) and the trigger sets kpi_setup_at. A second call returns false.
  • An admin on a brand already set up: false.
  • A user from another organization: 42501.

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

Re-approving after rebuilding the merge on main after #1128 and #1133; CI is green again.

@gkhngyk
gkhngyk merged commit a3c9e1b into ansvisor:main Oct 17, 2026
5 checks passed
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