Skip to content

feat(audit): rank Site Audit opportunities by a priority score - #1138

Merged
gkhngyk merged 9 commits into
ansvisor:mainfrom
cihan-gyk:feature/audit-priority
Oct 17, 2026
Merged

gkhngyk merged 9 commits into
ansvisor:mainfrom
cihan-gyk:feature/audit-priority

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Site Audit Opportunities is now ordered by a priority score. Each page gets a 0–100 score: its value to the business × the opportunity an audit can win × how easy it is to fix. This is task SA-005 of the Site Audit v2 spec.

Stacked on #1137 (which is stacked on #1136). The diff includes their commits until they merge. This PR has no migration.

Server: lib/audit/priority.js (new, pure)

Each part is 0–1. The score is round(100 × cbrt(value × opportunity × ease)): it keeps the product's order, on a scale people read. The spec's example, 0.96 × 0.9 × 0.8, scores 88. All weights live in PRIORITY_WEIGHTS, so they can be tuned in one place.

  • Value: what the page is worth.
    • GA's value percentile, weighted by its signal: revenue and transactions 1, key events 0.9, engagement 0.75.
    • Without GA, the page's 30-day sessions ranked within the site's pages, up to 0.6. Ties share a rank, as in scorePages.
    • The floor is 0.15.
  • Opportunity: what an audit can win. The parts add up, capped at 1, with a floor of 0.1.
    • No AI traffic (GA): 0.45.
    • Targeted, not cited: +0.15.
    • A competitor cited: 0.3, +0.05 per extra prompt, up to 0.45.
    • Never audited: 0.3. This is read from the page's audits, whichever source listed the page.
    • Last audit too old: 0.2.
    • Zero AI visits: 0.1, only when GA's reason doesn't already say so.
  • Ease: how close the page is to passing.
    • Last score ≥ 60: 1. From 40: 0.75. Below that: 0.55.
    • Never audited: 0.8.
    • The template grouping (SA-007) will add to this later.
  • rankOpportunities: scores every row and orders the inbox: pinned pages first, then by priority, then by path. Each row gains priority and priorityParts.

Server: route

  • GET /api/audits/opportunities ranks the rows once each has its AI visits.
  • buildOpportunities no longer orders by source; it returns rows pinned first, then by path.
  • Every row now takes its title, sessions and citations from site_pages, whatever source listed it.

Web

  • Priority column: a new first column in the Opportunities table, with an ⓘ tip that explains the score.
  • Score pill: dark from 80, as in the mockup; muted below.
  • Hover: shows the parts, e.g. "Value 0.96 × opportunity 0.90 × ease 0.80".
  • Copy: the strings are in en.json under audit.opportunities.

Related issue

None. Site Audit v2 spec, task SA-005.

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 00165 (feat(audit): list pages worth auditing in a Site Audit Opportunities tab #1136) and 00166 (feat(audit): add pages closest to competitor-won prompts to Site Audit Opportunities #1137) if they aren't applied yet.
  2. Open Site Audit → Opportunities for a brand with GA page opportunities, site pages, or competitor-cited prompts.
  3. The first column shows each page's priority, and the rows are ordered by it, highest first. A score of 80 or more is a dark pill.
  4. Hover a score to see its value × opportunity × ease. Hover the ⓘ in the header for the explanation.
  5. Pin a page with a low score: it moves to the top. Unpin it: it goes back to its place.

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:

  • server: npm run format, npm run lint and npm test (1532) pass.
    • lib/audit/priority.test.js (new):
      • the spec's example at 88;
      • the signal weights;
      • the sessions rank, with ties and pages above every site page;
      • the opportunity's sum and caps;
      • never audited counted once;
      • ease by the last score;
      • the inbox order.
    • lib/audit/opportunities.test.js: rows in path order, with their title and sessions from site_pages.
    • routes/audits-opportunities.test.js: the route returns priority and priorityParts.
  • web: yarn test (1331) and yarn build pass. _opportunities-table.test.tsx covers the column, the dark pill from 80 and the parts on hover.
  • Browser: this branch's server and web, with sample GA opportunities, competitor citations and two old audits on the demo brand.
    • Checked: the order went 88 (a revenue page never audited and targeted, not cited) → 76 → 58 → … → 34 (audited 120 days ago) → 29 (last score 32).
    • Views: 1440px and 1024px, light and dark.
    • Afterwards: the samples were deleted.

🤖 Generated with Claude Code

cihan-gyk and others added 6 commits October 17, 2026 20:47
SA-004 of the Site Audit v2 spec, first sources: audits were only of the
URLs someone typed, with nothing suggesting which pages to audit (F5, F7).

- GET /api/audits/opportunities builds the inbox (lib/audit/opportunities.js):
  Google Analytics' open "no AI traffic" pages (page_opportunities: valuable
  pages AI engines send no visitors to) and the site's pages (site_pages)
  never audited or audited more than auditMaxAgeDays (90) ago. One row per
  page across spellings, with why (chips), its last audit and its AI visits
  over 30 days (Analytics where it has the page, else the snippet). Pinned
  pages come first; excluded ones are listed apart; checkout-like paths stay
  out. Until the priority score (SA-005) rows follow GA value, then never
  audited, then citations and sessions.
- Migration 00164 adds site_audit_page_prefs (pinned / excluded per page),
  written through PUT /api/audits/opportunities/prefs.
- The hub gets Opportunities and Pages tabs; Opportunities opens when it
  lists pages. Select pages to Audit selected (the bulk audit, within the
  quota), Pin / Unpin or Exclude (with Undo); excluded pages can be
  restored from the list under the table. The bulk bar takes each table's
  own audit label and actions.

Competitor-cited pages and Search Console sources come in later PRs.

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

SA-004's citation source: prompts where a competitor's page is cited and
the brand's never is point to the brand page closest to them, as a page
worth auditing.

- Migration 00165 adds audit_competitor_cited_prompts (service role only):
  per active prompt over a window, answers citing a competitor's domain,
  answers citing the brand's own, and the competitor domains cited, read
  from prompt_result_citations (domains match with their subdomains).
- competitorGapPages (lib/audit/opportunities.js) keeps the prompts the
  brand was never cited on and maps each to its target URL, else the best
  site_pages match (candidatePages, as Content opportunities use), one row
  per page with its prompts and competitor names.
- GET /api/audits/opportunities adds them as a competitor_cited reason over
  the last 30 days; without competitor domains it asks nothing, and on a
  database without 00165 (PGRST202) the other sources still list. Until
  the priority score (SA-005) these pages rank after GA's valuable pages and
  before pages never audited.
- The Opportunities table shows them as "Crema Club cited on 3 prompts"
  ("+N" for more competitors).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
main took 00164 (insights topic rollups), so this branch's migration moves
to 00165_site_audit_page_prefs.sql; schema.sql rebuilt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
main took 00164 (insights topic rollups) and the Opportunities prefs
migration moved to 00165, so this one follows as 00166; schema.sql rebuilt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each page in the Opportunities inbox gets a 0-100 priority: its value to
the business x the opportunity an audit can win x how easy it is to fix,
each 0-1, shown as the cube root of the product (SA-005). Pinned pages
stay on top; the rest are ordered by priority. The table shows the score
in a pill (dark from 80) with its parts on hover.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 17, 2026
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk This is SA-005: the Opportunities inbox is now ordered by a 0–100 priority (value × opportunity × ease, weights in lib/audit/priority.js), with pinned pages still on top. It's stacked on #1137 and has no migration. Thanks!

…ed_prompts

The read matched every citation of the window against every brand and
competitor domain with LIKE: 2.7 s warm, 13.5 s cold, for 30 days of the
largest brand (796k citations, 118k distinct URLs), on each Opportunities
open. It now matches each distinct URL once, through its domain's suffixes,
and joins only the matched URLs' citations to their answers: 1.4 s warm,
the same 176 rows (checked against the SQL version on production). plpgsql
with force_custom_plan, planned with the brand and window in hand.

Claude-Session: https://claude.ai/code/session_0121amRjX7UFrgmGELAtkc7A
@github-actions github-actions Bot removed the needs-migration Adds or edits a migration — apply it to the hosted database label 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 after #1137. The priority, opportunities and route tests, the Opportunities table tests, typecheck, lint and format pass, and CI is green. The score is pure and computed per request over at most a few hundred rows, so it adds no database reads; sessionsRanker's linear scan per row stays well under a millisecond at the 500-page cap site_pages has on production.

@gkhngyk
gkhngyk merged commit d696ed9 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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants