Repository navigation
feat(audit): add pages closest to competitor-won prompts to Site Audit Opportunities - #1137
Conversation
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]>
…nities-competitors
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]>
…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
left a comment
There was a problem hiding this comment.
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.
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").
Migration
00166_audit_competitor_cited_prompts.sqlaudit_competitor_cited_prompts(brand, since, own_domains[], competitor_domains[]): for each active prompt of the brand over the window it returns:prompt_result_citations+citation_urls, the normalized tables the tracking writes. A domain also matches its subdomains.Server
competitorGapPages(lib/audit/opportunities.js):candidatePages, the same lexical matcher Content opportunities use; prompts no page matches are left out.GET /api/audits/opportunities:competitor_citedreason over the last 30 days.brand_domainsandcompetitors.domain.PGRST202), the other sources still list.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
Related issue
None. Site Audit v2 spec, task SA-004 (citation source).
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/): no errors; the 7 warnings are in files this PR does not touchyarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)Also run:
server:npm run format,npm run lintandnpm test(1526) pass.lib/audit/opportunities.test.js:routes/audits-opportunities.test.js:web:yarn test(1330) andyarn buildpass._opportunities-table.test.tsxcovers the chip with one and several competitors.bash supabase/build-schema.sh: regeneratedschema.sql.🤖 Generated with Claude Code