Skip to content

feat(audit): list pages worth auditing in a Site Audit Opportunities tab - #1136

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

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

Conversation

@cihan-gyk

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

Copy link
Copy Markdown
Collaborator

Summary

Site Audit gets an Opportunities tab: the brand's pages worth auditing, each with why, ready to audit in bulk, pin or exclude. This is SA-004 of the Site Audit v2 spec, with its first sources. Until now audits covered only the URLs someone typed, and nothing suggested which pages to audit (F5, F7).

Sources in this PR

  • Google Analytics: the open "no AI traffic" pages of page_opportunities. These are valuable pages (revenue, transactions, key events or engagement in the site's top 30%) that AI engines send no visitors to. The rows are already computed nightly with the GA sync.
  • Site pages: pages of site_pages (sitemap, GA and cited pages; up to 500) that were never audited, or whose last audit is older than auditMaxAgeDays (90, the Action Center's threshold).

Not in this PR:

  • Competitor-cited pages (spec source 5): the next PR.
  • Search Console sources (3 and 4) and the AI impressions column: the GSC sync stores queries only, with no page or AI Overviews data, so they need a new sync first.

Server

  • GET /api/audits/opportunities?brandId=… (pure logic in lib/audit/opportunities.js):
    • One row per page: pages are keyed like the Pages list (host without www., path without a trailing slash), lowercased as GA paths are. Both sources merge into one row.
    • Each row carries:
      • its reasons (no_ai_traffic with signal, percentile and citation state; never_audited; stale_audit with days);
      • pinned, running, and the last completed audit;
      • AI visits over 30 days, through ae_page_traffic_evidence: Analytics where it has the page, else the snippet, never both (as mergeTrafficPages). The value is null when the brand has neither.
    • Left out:
      • checkout, login, legal and similar paths (isExcludedPath), unless pinned;
      • pages audited within the window.
    • Order until the priority score (SA-005): pinned, GA value percentile, never audited before stale, then citations and sessions.
    • Reads: the GA rows are read past PostgREST's 1000-row page; a large site has up to ~1,500 of them.
  • Migration 00165_site_audit_page_prefs.sql:
    • One pinned / excluded row per brand and page (unique (brand_id, page_key)).
    • RLS: members can read it; the server writes it.
    • Also used by the auto-audit runs in SA-006.
  • PUT /api/audits/opportunities/prefs ({ brandId, urls, state }, Zod): pins, excludes, or clears (null) pages, once per page key.

Web

  • Tabs: the hub's list becomes Opportunities · N | Pages · N, as in the mockup. Opportunities opens when it lists pages, else Pages.
  • Opportunities table (audit/_opportunities-table.tsx):
    • Columns: Page (opens the live page), Why (chips such as "No AI traffic · Top 4% by revenue · Targeted, not cited", "Never audited", "Audited 120 days ago", "Pinned"), AI visits, Last audit (opens the report; "never"; "Running").
    • Paging: 50 rows at a time.
  • Bulk bar: the existing bar (SA-003) now takes each table's own audit label and actions.
    • On Opportunities:
      • "N selected · Uses N of your X audits";
      • Audit selected (the bulk audit, within the quota);
      • Pin / Unpin;
      • Exclude, with Undo in its toast.
    • Restoring: excluded pages are listed under the table ("N excluded pages · Show") to Restore.

Deploy

Apply 00165. It was 00164 until main took that number for the insights topic rollups (#867).

Known limits

  • Undo after a pin: undoing an exclusion clears the page's row, so a page pinned before it was excluded comes back unpinned.
  • Load time: the hub now loads five reads (pages, quota, trend, overview, opportunities). Next.js runs a page's server actions one at a time, so the first load takes a few seconds in development.

Related issue

None. Site Audit v2 spec, task SA-004 (first sources).

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.
  2. Open Site Audit for a brand with Google Analytics connected, or with site pages from its sitemap.
    • Opportunities · N opens first.
    • GA's valuable pages without AI traffic lead the list with their chips; then pages never audited; then pages audited more than 90 days ago.
    • A page audited recently isn't listed.
  3. Select pages.
    • Pin moves them to the top with a "Pinned" chip.
    • Exclude removes them, with Undo.
    • "N excluded pages · Show" → Restore brings one back.
  4. Audit selected starts the audits within the monthly quota. The rows read Running, then show their last audit.
  5. Pages still lists the audited pages, with Export and Re-audit.

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 (1519) pass.
    • lib/audit/opportunities.test.js:
      • page keys;
      • GA pages with their last audit;
      • never-audited and stale site pages, with fresh and failed audits;
      • merged sources and excluded paths;
      • pinned first and excluded apart;
      • running pages, and no primary host;
      • the order;
      • AI visits from GA over the snippet.
    • routes/audits-opportunities.test.js:
      • the inbox with AI visits and excluded pages;
      • more than 1,000 GA rows (this test fails without the paged read);
      • unknown visits without Analytics or the snippet;
      • pin / exclude / clear per page key;
      • validation.
  • web: yarn test (1327) and yarn build pass.
    • _opportunities-table.test.tsx: chips, links, selection (running pages left out), Show more, excluded pages and Restore, the empty state.
    • _bulk-bar.test.tsx: the table's own label and actions, and no Export without one.
  • bash supabase/build-schema.sh: regenerated schema.sql.
  • Dev database:
    • A rolled-back dry run created the table, took a row and refused an unknown state.
    • Then the migration was applied.
  • Browser: this branch's server and web, with two sample GA opportunities and two sample audits on the demo brand.
    • Checked: the order and chips; Pin, Unpin, Exclude with Undo, and Restore; Audit selected.
    • Views: 1440px and 1024px, light and dark.
    • Afterwards: the sample rows were deleted.

🤖 Generated with Claude Code

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]>
@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-004 from the Site Audit v2 spec (first sources): an Opportunities tab listing GA's valuable pages without AI traffic and site pages never audited or audited over 90 days ago, with Audit selected / Pin / Exclude. It adds migration 00164 (site_audit_page_prefs; applied on the dev DB). Competitor-cited pages come next in their own PR; the Search Console sources need a page-level GSC sync first. Thanks!

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]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Heads-up: main took 00164 (#867), so this PR's migration is now 00165_site_audit_page_prefs.sql (main merged in, schema.sql rebuilt), and the stacked #1137 uses 00166. The PR description is updated.

@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 branch already merges current main; I reran its checks on that tree. The opportunities, audits-opportunities route, bulk bar and Opportunities table tests, typecheck, lint and format pass, and CI is green.

00165 (site_audit_page_prefs) is applied on production. RLS is on with the member select policy only, so pins and exclusions are written by the server alone. site_pages holds at most 500 rows per brand on production, under the 1000-row page the route reads it in.

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