Skip to content

feat(content): show what the page earns in the editor's Impact tab - #1290

Merged
gkhngyk merged 72 commits into
ansvisor:mainfrom
cihan-gyk:feature/content-impact
Oct 21, 2026
Merged

gkhngyk merged 72 commits into
ansvisor:mainfrom
cihan-gyk:feature/content-impact

Conversation

@cihan-gyk

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

Copy link
Copy Markdown
Collaborator

Summary

Content Editor, PR 7a of the plan: the editor's side panel with its first tab, Impact (CE-016, spec v2 §6–§7). For the live page a draft optimizes, it shows what the page earns: the last 7, 30, 90 or 365 days against the period before.

Stacked on #1289 → #1288 → #1287 → #1286 → #1262 → #1261 → #1260 → #1259 → #1258 → #1256. Please merge them in order. This PR's own changes are a72d227 and 0f6f498.

Apply 00209 before the web. Without it the tab says it couldn't load the page's impact.

Side panel (_side-panel.tsx)

  • 400px beside the page, under the header. The spec's tabs (Impact · Chat · Brief · Sources · Rules) each join with the update that makes them work; Impact is first.
  • Opens by default on a window ≥ 1280px, closed below. The header's panel button opens and closes it, and the choice is remembered (aeo:content-editor-panel, read in try/catch).

Impact tab (_impact-tab.tsx)

  • Period buttons 7d · 30d · 90d · 1y (30d first).
  • This page, four cards with their change against the previous period:
    • Citations of the page on the brand's own domains;
    • AI visits;
    • Google clicks;
    • Prompts cited (absolute change, "+2" / "−1").
  • The % change follows the AI Traffic page's rule (visitChange): "Low sample" under 30 in either period, "New" from zero.
  • Prompts cited · N · M lost:
    • each prompt citing the page, with its platforms and "Cited N×";
    • a lost prompt (cited before, not now) in red, "Was cited on …".
  • See in Citations opens /dashboard/citations/url?u=<page>, because the Prompts page has no page filter.
  • Your other pages for these prompts: the brand's other pages cited now for the same prompts (up to 3), or "The only page cited for its N prompts · no overlap".
  • A note on the window and sources.
  • Search Console:
    • With no page data, the card reads "No Search Console data".
    • When a window starts before the first day of page data (190 days kept, so 1y always; 90d on a young connection), the change is left out and the note says so.
  • A new page (no live address yet) says there is nothing to measure.
  • Loading and error-with-retry states.

Migration 00209_content_page_impact.sql

  • content_page_impact(p_brand_id, p_page, p_days) returns jsonb.
  • Security definer with the organization check used by ai_traffic_page_citations (00176): null for a brand outside the caller's organization. citation_urls has no member select policy.
  • A page is one lower(normalize_page_path) key, so /x/ and /x?utm_source=… count as one page.
  • Windows are whole UTC days, today included. p_days must be 1–366.
  • Citations: own domains through citation_urls.domain, as ga_page_ai_visibility does. ChatGPT Shopping cards are left out, as on the Citations page.
  • AI visits: ai_traffic_merged's rule (00176). The snippet counts on days it was active for the brand, GA4 on other days, never both. GA4 rows without a landing page are left out, since normalize_page_path('') is / (the home page).
  • Search Console: clicks from gsc_page_stats by page_key, plus gscSince, the first day of page data.
  • Prompts: each prompt citing the page in either window, with its counts and platforms per window (most cited first, at most 100).
  • Overlap: the brand's other pages cited in this window for those prompts (at most 3).
  • Checked on the dev DB in a rolled-back transaction, as a member:
    • citations, including the ?utm_source copy of the page;
    • a GA4 row on a snippet day ignored and one on another day counted;
    • GSC clicks;
    • overlap found;
    • a 365-day window;
    • an outsider gets null.
  • Applied on the dev DB; schema.sql rebuilt.

Left for later (per the plan):

  • PR 7b: results per AI source (GA4 key events and revenue on the landing page, snippet visits by platform).
  • Journeys and goals (CE-017).
  • "Use lost prompts" (PR 8).

Related issue

Content Editor CE-016 (Impact tab), spec v2 §6–§7. Stacked on #1289 → … → #1256.

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

Apply 00204–00209. You need a draft that optimizes a live page: Start's "Optimize /… instead" (#1287), or the Page URL tab (#1288), on a page with citations.

  1. Open the draft in the editor on a wide window (≥ 1280px).
    • The side panel is open on the right with Impact.
    • On a narrower window it starts closed; the panel button at the right of the header opens it.
  2. This page shows Citations, AI visits, Google clicks and Prompts cited for the last 30 days, each with its change.
  3. Switch to 7d, 90d and 1y.
    • Every number follows the period.
    • On 1y the note says Search Console keeps 190 days of page data, and Google clicks show no change.
  4. Prompts cited lists the prompts with their platforms; a prompt cited only in the previous period is red, "Was cited on … · Lost".
  5. See in Citations opens the page's Citations view.
  6. A draft for a new page shows "This draft is a new page, not live yet…".
  7. Close the panel, reload: it stays closed.

Checked locally with temporary data on the demo brand (citations on four prompts across platforms, a lost prompt, an overlapping blog page, 77 AI visits, 70 days of Search Console data). The data is deleted. Checked in light and dark, and at 1024px.

Tests:

  • impact.test.ts: windows, Search Console coverage, changes, prompts cited, lost, platform names.
  • content-drafts-impact.test.ts: getContentImpact reads the period asked for; null outside the organization; errors; nothing read for a malformed brand, period or page.
  • _impact-tab.test.tsx: cards and changes, prompt rows with the lost one, overlap, period switch, Search Console missing and partial, no prompts, retry, new page.
  • page.test.tsx: the panel starts closed on a narrow window and opens from the header; opens on a wide window and remembers it closed.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/) (0 errors; the 6 warnings are in untouched files, as on main)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Web: yarn test (2,113 tests) and yarn build pass. No server change. bash supabase/build-schema.sh was run.

🤖 Generated with Claude Code

cihan-gyk and others added 24 commits October 20, 2026 17:09
Content Editor CE-001 and CE-002. Content becomes a group in the sidebar,
like Prompts: Opportunities keeps /dashboard/content, and the new Library
(/dashboard/content/library) lists every page a person or the Content Agent
writes or changes in Ansvisor, by status (Drafts, In review, Ready,
Published), with its page type, method, author and AEO score.

- Migration 00203: content_drafts with member RLS (select, insert, update;
  no delete yet). A guard trigger keeps author, created_by, brand and the
  scores server-side, so a member can't pass a draft off as the agent's.
- Sidebar: each submenu remembers its own open state (Prompts keeps its
  key), and only the longest matching entry is current, so Opportunities
  isn't marked on the Library.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Content Editor CE-004, stacked on the Library (ansvisor#1256). A draft opens at
/dashboard/content/editor/{id}: a header with its title, page type,
method and status, whether it's saved, and Mark ready; the toolbar (H1 H2
H3, bold, italic, link, lists, quote, table with add row / add column /
delete, image by address) with the length counter against the page
type's target; and the page as rich text (TipTap 3.31.4, pinned).

- Autosave after a pause in typing; saves never overlap, a failed one
  keeps the text and offers Retry, and leaving with unsaved text asks.
  The draft's title is its first H1.
- saveContentDraft validates the body (500k chars) and title and says
  when RLS matched no row; markContentDraftReady moves draft or in review
  to ready only.
- Links: http(s), mailto or a path on the brand's site; javascript:,
  //host and /\host are refused, in the dialog and when content loads.
- The Library gets New content (a blank draft, until the Start screen),
  and its entry stays current while a draft is open in the editor.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Content Editor CE-005 and CE-020, stacked on the editor core (ansvisor#1258).

- Paste: from Word, Google Docs, Notion or a web page, headings (H4-H6
  as H3), paragraphs, bold, italic, lists, links, tables and quotes stay;
  fonts, colors, classes, inline styles, scripts, iframes and utm_*,
  gclid, fbclid on links go. Google Docs' styled bold and italic become
  strong and em first. Pasted images wait for attachments. Cmd/Ctrl +
  Shift + V pastes plain text.
- </> HTML shows the page's HTML as a draft stores it, one block per
  line; Back to visual cleans it with the same allow-list and says how
  many unsafe tags it removed. Schema JSON-LD is kept: a block in the
  editor, written again from its JSON so it can't carry markup.
- The server checks every save against the allow-list again
  (sanitize-html), so a request made without the editor can't store a
  script, a handler or a javascript: link.
- dompurify 3.4.16 and sanitize-html 2.18.0, pinned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sanitize-html 2.18.0 needs Node 22.12 or later, and CI and the web
Docker image run Node 20, so yarn install refused it. 2.17.0 has no
engine limit and the same API; @types/sanitize-html 2.16.0 matches it
(both on htmlparser2 8).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Links in a draft looked like the text around them (dark, barely
underlined), so a link just added seemed not to be there. They're blue
and underlined now. With the cursor in a link, the toolbar shows its
address, which opens it in a new tab; Cmd/Ctrl + click on it in the page
does too. A click alone still puts the cursor in it to edit. A path on
the brand's own site is shown, not opened: it has no address here.

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

# Conflicts:
#	web/messages/en.json
#	web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
#	web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_toolbar.tsx
#	web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
On the user's request, as Webflow's custom code in rich text: as many
HTML blocks as a page needs, each carrying code as written (an embed, a
widget, a form), and one-click Copy HTML for a block or the whole page.

- An HTML block is added from the toolbar where the cursor is, and edited
  or removed from its own Edit. The editor shows its code as text; it
  never runs in Ansvisor. A draft stores it as UTF-8 base64 in an empty
  element, so no markup in it can act, and the server checks each one
  decodes and fits 50,000 characters.
- Copy HTML (header) puts the page on the clipboard as HTML to publish:
  each block as its own code, schema JSON-LD as its script. A block's
  own Copy HTML copies just its code. A refused Clipboard API falls back
  to the browser's copy command.
- The HTML view shows each block between <!-- html-block --> markers,
  kept as written; code typed between new markers becomes a block. A
  paste from elsewhere can't bring blocks in.
- The link, image and block dialogs focus their field when they open.

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

# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_toolbar.tsx
#	web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
Inserting an HTML block scrolls the cursor into view, and ProseMirror
measures a Range to do it. jsdom's ranges have no getClientRects, so on
CI's Node 20 the measuring threw on a timer after the test and failed the
run as an unhandled error. The editor test gives ranges an empty
measurement, as jsdom has no layout anyway.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…es to a draft

A paperclip tool and drag-and-drop on the editor (CE-006). Excel and CSV
open a preview (sheet picker, first 10 rows, header row) and insert as a
table, asking first above 200 rows. Word (.docx, via mammoth), Markdown,
HTML and plain text import as content through the same cleaning as a
paste. Images, old .xls/.doc and files over 10 MB get a clear message.
Imports land under a table when the cursor is inside one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Page types follow the kind of site (CE-023). Brand settings get a Site
type (B2B SaaS, E-commerce, B2C; guessed from Shopping mode and the
industry until picked), stored in brands.site_type (migration 00205). A
draft's page type is detected from its URL, JSON-LD schema and title,
and kept in step as the page changes, until the person picks one from
the editor header's page type menu (content_drafts.page_type_source).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ansvisor#1257 took 00203 (citation gap rollups) on main, so the content drafts
table moves to the next free number. The file is otherwise unchanged;
schema.sql is rebuilt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
New content (CE-003) starts a page from an open Content Opportunity, a
tracked prompt or a topic, or brings content to optimize by pasting
(cleaned like the editor's paste) or uploading a Word, Markdown, HTML or
text file. Start (CE-015) offers the site type's page types, the one the
title or content shows picked first, and opens the editor.

The draft keeps the opportunity it came from and the prompts it should
answer (migration 00206: opportunity_id, prompt_ids, and a trigger that
refuses another brand's opportunity or prompts). A new page starts with
its title as the H1; content to optimize is checked against the draft
allow-list. The editor's file readers move to lib/content/import-document
so both use them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Start (CE-015) now shows which of the brand's own pages AI already cites
for the prompts a new page would answer: prompts in common, citations in
the last 30 days and the Google position from Search Console, through a
new security-definer RPC, content_page_overlap (migration 00207). It
recommends one option: optimize a page that already answers half the
prompts or more, otherwise create the page and link the others to it,
or say it is safe to create.

Picking an existing page makes the draft that page's: optimize, its
title as H1, the page type read from its address, and its URL kept in
content_drafts.source_url, which the editor shows beside the page type.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Optimize content gets a Page URL option (CE-003), and Start's "Optimize
this page instead" now brings the page's live text. The API reads the
page through a new guarded fetch (server lib/safe-fetch.js):
- http(s) on the web ports, no credentials in the address;
- every address the host resolves to must be public, checked in the
  connection's own lookup, and an address written as an IP checked
  before it, since a literal skips the lookup;
- redirects followed by hand and checked the same way; 10 s, 3 MB,
  HTML only.
POST /api/content-pages/import reads only the brand's own domains,
before and after redirects, and returns the page's main content, title
and JSON-LD (lib/page-content.js). The web cleans it like a paste and
reads the page type from the address and the content.

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

Start's Who does it (CE-015): Me opens the editor; Agent waits for the
Content Agent; Team sends the draft to the Action Center as an action
for someone on the team to write.

The API gets a person-sent action kind, content_draft (from-content-draft.js,
POST /api/action-center/content-drafts): a new page's or an existing
page's library tasks, as an opportunity's page gets, prioritised and
measured on the draft's prompts and page. The draft records its action
(content_drafts.action_id, migration 00208, server-set: the drafts guard
is redefined to keep members from writing it). The Action Center names
it "Write: …" / "Optimize: …" and links back to the draft.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The editor gets its right side panel (Content Editor spec v2 §6), with the
Impact tab first (CE-016): for the live page a draft optimizes, the last
7, 30, 90 or 365 days against the period before.

- content_page_impact (00209): citations on the brand's own domains, AI
  visits by ai_traffic_merged's rule (snippet on its days, GA4 otherwise),
  Search Console clicks with the first day of page data, the prompts citing
  the page in either window with their platforms, and the brand's other
  pages cited for the same prompts. Security definer with an organization
  check; whole UTC days, today included.
- Impact tab: four cards with their change (low sample under 30, as the AI
  Traffic page), prompts cited with the lost ones marked, "See in
  Citations", the overlap line and where the numbers come from; a new page
  says there is nothing to measure yet.
- The panel opens beside the page on a wide window, closes from the header
  and remembers the choice.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Content Editor PR 7a is up: the editor's side panel with the Impact tab (CE-016). It's stacked on #1289, so please merge it after the stack below.

Two things worth a look:

  1. content_page_impact (00209) is security definer with the same organization check as ai_traffic_page_citations (00176). AI visits use ai_traffic_merged's rule: snippet days take the snippet, other days take GA4, never both. Could you run it once on the largest brand's busiest page with p_days = 365? It reads prompt_result_citations through the own-domain citation_urls, and I'd like to know the cost before it ships.
  2. The change % uses the AI Traffic page's low-sample rule (under 30 in either period). Is that right for citations too, or should citations get a lower threshold, since they come in smaller numbers?

Results per AI source (GA4 key events and revenue) follow as PR 7b.

@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 20, 2026
…the page's AI visits

normalize_page_path('') is '/', so a GA4 row with no landing page read
as the home page's visit.

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

Copy link
Copy Markdown
Collaborator Author

Pushed 0f6f498: content_page_impact (00209) now leaves out Analytics rows with an empty landing page. normalize_page_path('') is /, so they were counted as the home page's AI visits. It's re-applied on the dev DB, and schema.sql is rebuilt.

cihan-gyk and others added 6 commits October 21, 2026 15:51
# Conflicts:
#	supabase/schema.sql
#	web/src/components/content/new-content-dialog.tsx
#	web/src/lib/actions/content-drafts.ts
…and leave ChatGPT Shopping out

content_page_overlap took 8.9 s cold on the brand with the most citations,
mostly reading prompt_results' wide rows one citation at a time for
prompt_id. The window's answers to the prompts now come from
idx_prompt_results_brand_created_cover alone (brand_id, created_at, with
id and prompt_id included), materialized and hash-joined to the brand's
citations and its own pages. ChatGPT Shopping answers are left out, as
Impact does, so Start and Impact count the same citations. The function is
planned for each call's values (plan_cache_mode = force_custom_plan).

Checked against the old version on a fixture: same pages and prompts in
common; the only difference is the Shopping citations.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	web/src/components/content/new-content-dialog.tsx
…pts in SQL, end windows yesterday

- plan_cache_mode = force_custom_plan: after five calls plpgsql switched
  to a generic plan (7.8 s at 30 days, 83 s at 1 year on the biggest
  brand), so it would have started failing once pooled connections warmed.
- Both windows' answers come from idx_prompt_results_brand_created_cover
  alone (brand_id, created_at, with id, platform and prompt_id included),
  materialized and hash-joined, instead of a primary-key read of
  prompt_results per citation; the brand's other pages are found from the
  prompts citing this one outward, not from every own URL.
- promptCounts {cur, prev, lost} come from SQL over all prompts; the list
  stays capped at 100 for display, so the card, "N lost" and the overlap
  line no longer undercount.
- Windows are whole UTC days ending yesterday, so today's partial day isn't
  compared with whole ones.

Checked against the old version on a fixture (same windows): identical.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Review fixes for #1290 are in 23fb321 (merged up the stack):

  1. content_page_impact:
    • set plan_cache_mode to 'force_custom_plan'.
    • Both windows' answers come from idx_prompt_results_brand_created_cover alone (brand_id, created_at range, with id, platform, prompt_id included), materialized and hash-joined. Citations carry their answer's created_at (persistCitationRows), so the window on answers is the same as on citations.
    • other_pages goes outward from this window's answers to the citing prompts (materialized), then their citations (prc.prompt_result_id, bounded by brand and window), then own_all.
  2. Prompt counts: the function returns promptCounts {cur, prev, lost}, counted over all prompts. The card, its change, "N lost" and the overlap line read it. The list stays capped at 100 for display. The "Use the lost prompts" button (feat(content): plan the page in the editor's Brief tab and rebuild it from the sources kept #1293) adds the lost prompts it lists, and its label counts those.
  3. Shopping is filtered in cites, other_pages (through the answers) and 00207.
  4. Windows end yesterday (whole UTC days), as the overview asked. impactWindows and gscCoverage follow. 00210's Results use the same windows (in feat(content): show the results from AI visits per AI source in the Impact tab #1291, 047756f, with plan_cache_mode).

Checked against the old function on a fixture with the same windows (3 pages × 30d and 1y): identical JSON apart from the new promptCounts.

The EXPLAIN as authenticated after six calls needs prod-size data. Could you run it on the brand you measured, for 30d and 1y?

Checked on the stack's top (#1310): web 2,346 tests, tsc, lint, Prettier and yarn build; migrations re-applied on the dev DB.

cihan-gyk and others added 17 commits October 21, 2026 17:54
…İ before them keeps them

`toLowerCase()` turns 'İ' into two UTF-16 units, so every İ before a JSON-LD
script shifted the positions taken from the lower-cased copy: the schema was
dropped on save, with as many characters of the text after it. Only ASCII
letters are lower-cased now, which keeps the length and stays linear.

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

# Conflicts:
#	web/src/lib/content/sanitize-draft-html.test.ts
The test merged up from ansvisor#1259 used extractJsonLd, which this branch
replaces with extractKeptBlocks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
gkhngyk
gkhngyk previously approved these changes Oct 21, 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.

Fixes check out.

Timings for content_page_impact on prod (the brand with the most citations, its most-cited page, custom plan):

Window Time Notes
30d 0.31 s warm
1y 0.48 s, then 0.38 s 224k answers over the two windows
1y, another large brand 2.6 s cold, 41 ms warm
  • force_custom_plan is load-bearing. With a generic plan, the 30d and 1y runs together did not finish within 120 s. The generic plan does a BitmapAnd over the brand's whole citation window once for every citing answer in other_pages. Keep the setting.
  • promptCounts now counts over all prompts, and the web reads it.
  • Windows now end yesterday, in SQL and in impactWindows alike.

One thing left, outside this file. The cover-index scan still does 23.6k heap fetches (30d) and 29.5k (1y) on the biggest brand, because the visibility map of prompt_results is stale; see the review on #1287. On a cold cache that is on the order of 10 s. The autovacuum_vacuum_insert_scale_factor setting proposed there fixes it. Nothing more is needed in this PR.

The PRs below this one are squash-merged on main; this keeps only this branch's own change on top.

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.

The fixes check out. Merging with the stack.

@gkhngyk
gkhngyk merged commit 6c3b183 into ansvisor:main Oct 21, 2026
4 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