Skip to content

feat(audit): add pages closest to competitor-won prompts to Site Audit Opportunities - #1137

Merged
gkhngyk merged 7 commits into
ansvisor:mainfrom
cihan-gyk:feature/audit-opportunities-competitors
Oct 17, 2026
Merged

gkhngyk merged 7 commits into
ansvisor:mainfrom
cihan-gyk:feature/audit-opportunities-competitors

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Site Audit Opportunities now lists the brand pages closest to prompts a competitor wins, where a competitor's page is cited and the brand's never is. This is the citation source of SA-004 (spec §4.1, "Ansvisor citations").

Stacked on #1136 (the Opportunities tab). The diff includes its commits until it merges.

Migration 00166_audit_competitor_cited_prompts.sql

  • audit_competitor_cited_prompts(brand, since, own_domains[], competitor_domains[]): for each active prompt of the brand over the window it returns:
    • the answers;
    • the answers citing a competitor's domain;
    • the answers citing one of the brand's own;
    • the competitor domains cited.
  • Rows: only prompts with a competitor citation come back.
  • Source table: prompt_result_citations + citation_urls, the normalized tables the tracking writes. A domain also matches its subdomains.
  • Access: service role only, like the other server reads.

Server

  • competitorGapPages (lib/audit/opportunities.js):
    • Keeps the prompts the brand was never cited on in the window.
    • Maps each one to the prompt's target URL, else to the closest site page, using candidatePages, the same lexical matcher Content opportunities use; prompts no page matches are left out.
    • Gives one row per page, with the number of prompts and the competitor names.
  • GET /api/audits/opportunities:
    • Adds these pages with a competitor_cited reason over the last 30 days.
    • Hosts come from brand_domains and competitors.domain.
    • With no competitor domains, it asks nothing.
    • On a database without 00166 (PGRST202), the other sources still list.
  • Order until the priority score (SA-005): after GA's valuable pages without AI traffic, before pages never audited.

Web

The Opportunities table shows the reason as a chip: "Crema Club cited on 3 prompts", or "Crema Club +1 cited on 2 prompts" with more competitors.

Deploy

Apply 00166, after #1136's 00165. Both moved up one number when main took 00164 (#867).

Known limits

  • Closest page: a lexical match. When two pages tie, the first read wins; e.g. "best coffee subscription services" can land on a gifts page rather than the subscriptions guide. A prompt's target URL decides it.
  • Cost: the read matches every citation of the window against the domains on each request. That's fine at today's volumes; a rollup could replace it for very large brands.

Related issue

None. Site Audit v2 spec, task SA-004 (citation source).

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.
  2. Use a brand with competitor domains set, and tracked answers from the last 30 days where a competitor's page is cited on a prompt and the brand's isn't.
  3. Open Site Audit → Opportunities. The page closest to that prompt (its target URL, else the best-matching site page) shows "Competitor cited on N prompts".
  4. A prompt the brand is also cited on adds nothing.

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 (1526) pass.
    • lib/audit/opportunities.test.js:
      • closest pages for competitor-won prompts;
      • target URLs first;
      • prompts the brand is cited on, or that no page matches, left out;
      • competitor names;
      • the reason and its place in the order.
    • routes/audits-opportunities.test.js:
      • the reason in the inbox, and the RPC's hosts;
      • no call without competitor domains;
      • the other sources on a database without 00166.
  • web: yarn test (1330) and yarn build pass. _opportunities-table.test.tsx covers the chip with one and several competitors.
  • bash supabase/build-schema.sh: regenerated schema.sql.
  • Dev database:
    • A rolled-back dry run with sample citations returned the right rows: two prompts only a competitor was cited on, and one the brand was cited on too.
    • Then the migration was applied.
  • Browser: this branch's server and web, with sample competitor and brand citations on three demo prompts.
    • Checked: two pages showed "Crema Club cited on 1 prompt" and "Northline Roasters cited on 1 prompt"; the prompt the brand was also cited on added nothing.
    • Views: 1440px and 1024px, light and dark.
    • Afterwards: the samples were deleted.

🤖 Generated with Claude Code

cihan-gyk and others added 5 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]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk This is SA-004's citation source: Opportunities now lists the brand pages closest to prompts where a competitor's page is cited and ours never is. It's stacked on #1136 and adds migration 00166 (applied on the dev DB). Thanks!

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

@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 #1136. The opportunities and route tests, typecheck, lint and format pass, and CI is green.

I pushed one change to 00166, c383a6e (perf(audit): match a window's cited URLs once in audit_competitor_cited_prompts). Measured on production for 30 days of the largest brand (796k citations, 118k distinct cited URLs), the SQL version took 2.7 s warm and 13.5 s cold. That cost lands on every Opportunities open, and it came from matching every citation against every brand and competitor domain with LIKE.

The new version:

  • matches each distinct URL once, through its domain's suffixes;
  • joins only the matched URLs' citations to their answers;
  • runs as plpgsql with force_custom_plan.

It takes 1.4 s warm and returns the same 176 rows: an except both ways against the SQL version on production came back empty.

00166 is applied on production as that version. It is executable by service_role only. On the largest brand, 4 of the 176 prompts are ones the brand is never cited on.

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