Skip to content

feat(audit): group Site Audit pages by template, "fix once, applies to N pages" - #1141

Merged
gkhngyk merged 14 commits into
ansvisor:mainfrom
cihan-gyk:feature/audit-templates
Oct 18, 2026
Merged

gkhngyk merged 14 commits into
ansvisor:mainfrom
cihan-gyk:feature/audit-templates

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Site Audit groups pages by template: "Fix once, applies to N pages". Pages built from one template (the blog's posts, the product pages) share most of their gaps, so fixing the template fixes them all. This is task SA-007 of the Site Audit v2 spec (§5), built as the v2 mockup shows it: a template row in the Opportunities inbox, and Group by template on Pages.

Stacked on #1140 (which is stacked on #1139). The diff includes their commits until they merge. No migration.

Server

  • lib/audit/templates.js (new, pure):
    • A page's template: its path with the last part as a wildcard, so /blog/milk-drinks → /blog/*. A top-level page has none.
    • Size: a template counts with three pages or more, from site_pages and the audited pages on the primary domain.
    • Shared gaps: signals that fail or warn on at least ⅔ of its audited pages (two at least), through each page's latest audit.
    • Excluded pages: checkout, login, legal and the like (isExcludedPath) are left out, as the inbox leaves them out.
    • templateOpportunities: turns a template with shared gaps into an inbox row. Its template field carries its pages, how many are audited, and the pages an audit of it starts: those without a completed audit, up to 50. The brand's exclude patterns leave it out.
  • priority.js:
    • scoreTemplate:
      • Value from its pages' summed sessions.
      • Opportunity from the shared gaps (0.3, +0.05 each more, up to 0.45), plus its unaudited share × 0.3, plus no AI visits.
      • Ease 0.9: one fix covers them all.
    • Template pages: a page whose template shares gaps gets ease.templateFix (+0.1, up to 1), as SA-005 left room for.
  • GET /api/audits/opportunities:
    • Adds the template rows. Their AI visits are their pages' visits added up; one evidence read covers the rows' and the templates' pages, each once.
    • Applies the ease bonus to template pages.
  • GET /api/audits/templates: the template groups, for Pages.
  • GET /api/audits/pages: each row carries its template (primary domain only).
  • Auto-audit (feat(audit): audit top-priority pages automatically each billing week #1139): runs skip template rows; their pages are in the inbox one by one.

Web

  • Template row in Opportunities (mockup):
    • Page: "/blog/* · 38 pages", with "Fix once, applies to 38 pages" on hover.
    • Why: the shared gaps as chips.
    • AI visits: its pages' total.
    • Last audit: "12 of 38".
  • Selecting a template row:
    • Audit selected audits its pages not audited yet. The bulk bar counts those pages against the quota ("Uses 26 of your 225 audits"), and requests go 50 at a time.
    • Exclude adds /blog/* to the brand's exclude patterns (feat(audit): audit top-priority pages automatically each billing week #1139's settings), so its pages leave the inbox too; Undo puts the patterns back. Changing patterns needs admin or manager, as in Settings.
    • Pin is off for templates.
  • Pages › Group by template: a toggle next to Export, as in the mockup's bar. It groups the shown pages under "/blog/* · 12 of 38 pages audited · Fix once: Author byline, FAQ schema · applies to 38 pages"; pages without a template go under "Other pages". The templates are read only while it's on.
  • Copy: the strings are in en.json under audit.opportunities and audit.pages.

Related issue

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

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. Use a brand whose primary domain has three or more pages under one folder in site_pages (e.g. /blog/…), two or more of them audited, with a signal failing on most of them.
  2. Open Site Audit → Opportunities. A "/blog/* · N pages" row lists the shared gaps, the pages' AI visits and "X of N".
    • Its never-audited pages show ease 0.90 in their priority tooltip.
  3. Select the template row.
    • The bulk bar counts its unaudited pages, and Pin is off.
    • Audit selected starts them.
    • Exclude hides the template and its pages, and Undo brings them back.
  4. Open Pages and click Group by template: the pages sit under their template's header, the rest under Other pages.

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 pass (1578; the related 262 again after the review fix).
    • lib/audit/templates.test.js: the template of a path; counting site and audited pages once; shared gaps at ⅔; the minimums; the primary host; incomplete audits; sessions; running; pages without opportunity; the inbox rows and their exclusion.
    • lib/audit/priority.test.js: scoreTemplate, the template ease bonus, template rows ranked with pages.
    • lib/audit/auto-audit.test.js: runs skip template rows.
    • routes/audits-opportunities.test.js: the template row, its AI visits, the ease bonus, a pattern leaving it out, and GET /templates.
    • routes/audits-pages.test.js: each Pages row's template.
  • web: yarn test (1356) and yarn build pass.
    • _opportunities-table.test.tsx: the template row and opportunityAuditUrls.
    • _pages-table.test.tsx: groupPagesByTemplate and the group headers.
    • _bulk-bar.test.tsx: a template's pages counted against the quota.
  • Browser: this branch's server and web, with five sample /blog/ pages on the demo brand's primary domain, three audited with shared gaps.
    • Checked: the template row (priority 50, "Author byline · FAQ schema", "3 of 5"), Pin off, Exclude and Undo, and Group by template on Pages.
    • Views: 1440px and 1024px, light and dark.
    • Afterwards: the samples were deleted.

🤖 Generated with Claude Code

cihan-gyk and others added 10 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]>
Site Audit's auto-audit (SA-006): 25% of the monthly quota audits the
Opportunities inbox's top pages by itself, in five runs of 5% (at least
one page) on days 1, 7, 14, 21 and 28 of the billing month. A daily sweep
after the GA sync claims each org's due run once, picks pinned pages
first and then the highest priority across its brands, skipping pages
audited or tried in the last 30 days, and runs them in their own pool.
The runs still to come keep their slots out of the user's pool; a run's
unused slots return when it is recorded.

Settings (admins and managers): auto-audit on/off for the organization
(on by default with a monthly quota, off without one), an e-mail after
each run, the brand's exclude patterns and its pinned pages. The Site
Audit page shows the latest run ("3 pages audited · 6 critical issues ·
next run Oct 21") and the quota card what auto-audit keeps.

Migration 00167 adds the settings and run tables and a source column on
site_audits and site_audit_usage.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	server/src/lib/audit/opportunities.js
#	server/src/lib/audit/opportunities.test.js
#	server/src/routes/audits-opportunities.test.js
#	server/src/routes/audits.js
#	supabase/migrations/00166_audit_competitor_cited_prompts.sql
#	supabase/schema.sql
#	web/src/app/[locale]/(dashboard)/dashboard/audit/page.tsx
#	web/src/lib/actions/audits.ts
… strip

Site Audit's quota UI (SA-019, spec §4.4): the Audits this month card
shows what's left of the plan's audits, a thin bar split into auto used,
auto scheduled, the user's and free, the five runs on one line (done
filled, next outlined, upcoming empty) and "Auto 25% · How it works",
which opens four short points under the cards. A summary strip on top
reads "68 site score +3 · 2 pages audited · Lowest: E-E-A-T 36 · Next
auto-audit on Oct 21 · 5 pages". When the user's share is used up while
runs are still scheduled, the run bar and the bulk bar say so and point
to turning auto-audit off.

The quota counts the runs' running audits apart, and the overview adds
the lowest of the five categories.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…o N pages"

Site Audit's template grouping (SA-007): pages built from one template
share most of their gaps, so fixing the template fixes them all. A page's
template is its path with the last part as a wildcard (/blog/* for
/blog/milk-drinks); a template counts with three pages or more, from the
site's pages and the audited ones, and its shared gaps are the signals
that fail or warn on two-thirds of its audited pages. Checkout, login and
legal pages are left out, as the inbox leaves them out.

As in the v2 mockup, a template whose pages share gaps is a row of the
Opportunities inbox ("/blog/* · 38 pages", its gaps as chips, its pages'
AI visits added up, "12 of 38" audited), scored on its own. Audit
selected audits its pages not audited yet; Exclude adds its pattern to
the brand's exclude patterns, with Undo. Pages of such a template get a
template fix on their ease. Pages › Group by template lists the pages
under their template: "/blog/* · 12 of 38 pages audited · Fix once:
Author byline, FAQ schema · applies to 38 pages".

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk This is SA-007: template grouping as in the v2 mockup — a "/blog/* · 38 pages" row in Opportunities (shared gaps, summed AI visits, "12 of 38"; Audit selected audits its unaudited pages, Exclude adds the pattern) and Group by template on Pages. It's stacked on #1140 and has no migration. 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
@github-actions github-actions Bot removed the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 18, 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 #1140: the templates, priority, auto-audit and route tests and the Pages and Opportunities table tests pass (web run locally with the Excel writer stubbed, as CI's web job doesn't run tests), and CI is green. The template groups' signal read is chunked under the 1000-row page and runs alongside the competitor read, so the inbox waits for no extra round trip.

@gkhngyk
gkhngyk merged commit 5864e88 into ansvisor:main Oct 18, 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