Skip to content

feat(content): weigh a page's conversions from AI journeys in the Content Plan's Priority Score (00291) - #1557

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:feature/plan-priority-conversions
Oct 27, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:feature/plan-priority-conversions

Conversation

@cihan-gyk

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

Copy link
Copy Markdown
Collaborator

Summary

The Content Plan's Priority Score (#1356) weighs each page's value, 20 of its 100 points, by its AI visits. The approved plan also counts conversions there once per-page data exists. CE-017 (00286–00288) added conversions from AI journeys, so this brings them in.

Migration 00291: content_plan_pages with each page's conversions.

  • Each listed page gets conversions:
    • What counts: conversions from AI journeys in the window that landed on the page, went through it, or converted on it. This is the editor's Live page tab's count (00287), once per page.
    • It's null for a brand with conversions off: unknown, never zero.
    • Past 2,000 conversions in the window, the count is of the latest 2,000, as 00287 and 00288 bound their journeys.
  • Bounded reads:
    • the window's latest 2,000 conversions, through 00287's partial index;
    • then 00287's seen, for every listed page at once: when each visitor id first saw each listed page, from a day before its first AI visit to its last conversion, on the journey index (00286).
    • That's one row per visitor and page, so a sender's many pageviews can't multiply the rows. A conversion counts for a page when that first sighting falls in its journey, exactly as 00287 counts it.
  • With conversions off nothing is read: the CTE is filtered by the brand's flag.
  • Everything else is 00238's, with the same signature, so its grants carry over. The function's comment is brought up to date.

Web

  • plan.ts:
    • PlanPage.conversions (number | null).
    • Page value is pageValueOf. Conversions get a share of it that grows with the data (conversionsShareOf): 2% per conversion counted on the listed pages, up to 40% at 20. From there it's 60% AI visits and 40% conversions. One conversion moves a row by 0.4 points at most. Conversions count on a log scale, 20 in the period counting in full (conversionsValue).
    • With conversions off, or on with none yet in the window, it's the visits alone, as before. Turning conversions on doesn't reorder the Plan before any data arrives.
    • A page with conversions counts as traffic data, as one with AI visits does.
  • content-plan.ts: maps the field; null when absent, from a database before 00291.
  • Tooltip: the score's breakdown reads "42 AI visits and 3 conversions this period" (en.json: plan.score.pageValue.visitsConversions).

Checked on the dev DB (00291 is applied there):

  • In a rolled-back transaction, with the demo brand given two cited own pages:
    • with conversions off, the new read equals 00238's apart from each page's conversions: null;
    • with conversions on and four journeys, the 7- and 30-day counts are right: a landing, a pass-through, a conversion on the page, one outside the window. They equal content_page_conversions for the same pages at 30 days.
  • The temporary copy of the old function was dropped, and no test row is left.

Deploy: a migration, then the web.

  • The web reads the new field only when it's there, so either order works.
  • Before the migration runs, the score reads as before.

Related issue

Content Plan Priority Score, step 2 follow-up (#1356; no issue).

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. Run the tests:
    cd web && npx vitest run src/lib/content/plan.test.ts src/lib/actions/content-plan.test.ts "src/app/[locale]/(dashboard)/dashboard/content/plan/page.test.tsx"
  2. With 00291 applied, on a brand with conversions on (Settings › Tracking › Conversions) and AI journeys:
    • open Content › Plan and hover a page's Priority Score;
    • the page value line reads "N AI visits and M conversions this period";
    • the count matches the page's Live page tab for the same period.
  3. On a brand with conversions off, the scores are unchanged.

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 were already on main
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run:

  • yarn test (427 files, 4,070 tests) and yarn build;
  • bash supabase/build-schema.sh: schema.sql is in the PR.

Tests:

  • plan.test.ts:
    • full visits and 20 conversions give 20 points;
    • the same visits with none converting give 12;
    • few visits with many conversions give 12.2;
    • pageValueOf without conversions is the visits alone;
    • turned on with none yet, the visits alone;
    • conversions with no AI visit on any page read as data;
    • the existing breakdowns carry conversions: null.
  • content-plan.test.ts: maps a count, and reads null, absent and 0 as null, null and 0.
  • page.test.tsx: the tooltip names the conversions.

This needs the needs-migration label (I can't set labels on this repo).

Pre-push review

A review agent read the change. It checked:

  • 00291 against 00238 line by line: only the new parts differ;
  • the same window as 00287;
  • the definer check, unchanged;
  • no change for brands with conversions off;
  • schema.sql byte-identical to a fresh build.

Fixed from it:

  • the journey read now uses 00287's seen, one row per visitor and page. Before, it joined every pageview to every conversion, which a crafted sender could inflate;
  • conversions weigh only once some page has any;
  • a page with conversions counts as traffic data;
  • the unread conversionsOn key is gone;
  • the function's comment is updated;
  • one test asserts the exact value.

🤖 Generated with Claude Code

…tent Plan's Priority Score, with content_plan_pages counting them as the Live page tab does

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 26, 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.

Needs changes: one fix before merge, a bound on the conversions read. The rest are follow-ups.

What I checked:

  • Same results otherwise. I compared 00291's function with 00238's line by line. 00238 is the latest on main, after 00220, 00221 and 00226. Every change is an addition:

    • the v_conversions_on variable and its read;
    • four CTEs: conversions, seen, touched and page_conversions;
    • the conversions key and its left join.

    Nothing else changes: the columns, pages' order and its limit of 100, the filters, the jsonb_agg order and the nulls. page_conversions is grouped by page_key, so the left join can't repeat a page.

  • Security definer. The signature, security definer, search_path, plan_cache_mode and the organization check are unchanged. create or replace keeps the grants from 00220/00221: authenticated and service_role, not anon. The flag is read after the check, and both reads of ai_traffic_events filter on brand_id = p_brand_id. Members can already read those rows under 00286's policy, so nothing new is exposed. The function's comment is now current; 00238 had left 00221's text, overlaps clause and all.

  • The count is the Live page tab's.

    • The window matches 00287's: N whole UTC days, ending yesterday.
    • seen is 00287's, per visitor and page. It's the first sighting between a day before the visitor's first AI visit and its last conversion.
    • touched counts a conversion for a page when it landed there, converted there, or the first sighting falls in [first_at − 1 day, occurred_at], as 00287's through does. The union counts a page reached two ways once.
    • Both sides key pages with the same lower(normalize_page_path(...)).
  • Reads.

    • Conversions off: the conversions CTE's condition is a constant false in the call's own plan (force_custom_plan), so nothing is read. The added cost is one primary-key read on brands.
    • Conversions on: one pass per brand, not one per page. First the window's conversions, through 00287's partial index. Then each converting visitor's pageviews in range, read once on the journey index and filtered to the 100 listed pages. That costs about as much as one Live page tab count for the same window, plus the union (item 2).
  • Null and zero.

    • Conversions off gives null on every page; on with none in the period gives 0.
    • In both cases conversionsKnown stays false in plan.ts. The page value is visitsValue as today, and trafficKnown doesn't change.
    • I ran 5,000 random plans (pages, opportunities, marks) through main's buildPlan and this branch's, with conversions set to null and to 0. All 10,000 runs match: rows, order, scores, score parts (apart from the new conversions: null), hidden rows and counts.
  • The score. Once a listed page has a conversion, page value is 60% visits and 40% conversions. conversionsValue is on a log scale, with 20 conversions counting in full. The new expectations (20, 12 and 12.2 points) hold on this branch and fail on main's plan.ts.

  • Web.

    • The Plan calls content_plan_pages from the web with the member's session, so the API isn't involved.
    • getContentPlan maps each field by name. Against a database without 00291, conversions reads null and the score is today's. The old web ignores the new key. Either deploy order is safe.
    • The tooltip mentions conversions ("42 AI visits and 3 conversions this period") only when they're weighed.
  • Tests. These fail without the change: the content-plan.test.ts case ([null, null, 0]), the tooltip case and the score cases.

  • Migration and merge.

    • 00291 is the only open migration with that number.
    • schema.sql is byte-identical to a fresh build-schema.sh run.
    • The branch is one commit on today's main (307670b). It merges to its own tree, and CI passes.
  • Before the API deploy. Nothing here depends on the API. Once the migration runs and this merges:

    • A brand with conversions off, or on with none in the period, sees the same Plan.
    • A brand with conversions on and some on a listed page sees new scores, and the conversions in the tooltip.
  • Dry run on prod (00291 applied in a rolled-back transaction, as the service role): on the largest, a mid-size and a small brand at 7, 30 and 90 days, every output equals today's once each page's conversions key is taken out, and that key is null on every page. With conversions turned on for the largest brand (also rolled back) every page reads 0, and the 90-day read took about 0.55 s. No brand has conversions on yet.

Before merge:

  1. Bound the conversions read (supabase/migrations/00291_content_plan_pages_conversions.sql:175-221).
    • The whole Plan now waits on this read: getContentPlan throws on this call's error. The strip's own read (brand_ai_conversions) falls back to nothing instead.
    • The read grows with the period's conversions, with no cap. touched holds a row for each conversion and each listed page its visitor saw, up to 100 per conversion.
    • Read the latest 2,000, as 00287 and 00288 bound their journeys. The partial index is (brand_id, occurred_at desc), so the scan stops there:
        conversions as materialized (
          select c.id, c.visitor_id, c.first_key, c.page_key, c.first_at, c.occurred_at
          from public.ai_traffic_events c
          where v_conversions_on
            and c.brand_id = p_brand_id
            and c.kind = 'conversion'
            and c.occurred_at >= v_cur
            and c.occurred_at < v_until
          order by c.occurred_at desc
          limit 2000
        ),
    • The scores don't change, since 20 conversions already count in full. Past 2,000 in a period, the tooltip's count covers the latest 2,000, as the strip's Top journey does.

Follow-ups:
2. One conversion re-weighs every page (web/src/lib/content/plan.ts:350, :196).

  • The split switches from 100/0 to 60/40 when the first conversion lands on any listed page. Each page with no conversion then loses up to 8 points:
    • 1,000 AI visits drop from 20 points to 12;
    • 300 AI visits drop from 16.5 to 9.9, below a new page's neutral 10.
  • In random plans I generated, that one conversion reordered 9 plans in 10 and changed the badge on one row in 5.
  • Let the conversions' share grow with the data instead, reaching 40% once the listed pages have 20 conversions between them:
    // buildPlan
    const conversionsSeen = pages.reduce((n, p) => n + (p.conversions ?? 0), 0);
    const conversionsShare =
      PAGE_VALUE_SPLIT.conversions * Math.min(1, conversionsSeen / FULL_CONVERSIONS);
    
    export function pageValueOf(
      visits: number,
      conversions: number | null,
      share: number = PAGE_VALUE_SPLIT.conversions,
    ): number {
      return conversions === null
        ? visitsValue(visits)
        : (1 - share) * visitsValue(visits) + share * conversionsValue(conversions);
    }
    conversionsKnown becomes conversionsSeen > 0. One conversion then moves a row by 0.4 points at most, and from 20 on the split is 60/40, as in the PR.
  1. Docs (docs/guides/content-editor.mdx:53). "Page value" still says AI visits only. Add that with conversions on, once the listed pages have some, page value also weighs the conversions from AI journeys through the page.

…their share of page value grow with the data (ansvisor#1557 review)

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

mintlify Bot commented Oct 26, 2026

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 9fd7609.

Approve preview

@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Thanks! All three are in 9fd7609:

  1. Bound the conversions read. The conversions CTE now reads the latest 2,000 (order by c.occurred_at desc limit 2000), as 00287 and 00288 bound their journeys, through 00287's partial index. The migration's note and the function's comment say a page's count is of the latest 2,000.
    • On dev, in a rolled-back transaction: the earlier checks read as before (conversions off: same output as 00238 with conversions null; on: pricing 2 / pour-over 1 at 7 days, 2 / 2 at 30, matching the Live page tab). Then 2,000 newer conversions on an unlisted page: both listed pages read 0, so the older ones fall out. That read took about 21 ms.
    • schema.sql regenerated.
  2. One conversion re-weighs every page. As you suggested: conversionsShareOf(pages) is 0.4 × min(1, conversions on the listed pages / 20), and pageValueOf(visits, conversions, share) uses it. Conversions are weighed (non-null in the breakdown) once the share is above 0. PAGE_VALUE_SPLIT became CONVERSIONS_SHARE, since the visits half is 1 − share.
    • New test: two pages with 1,000 AI visits, one with a conversion: 19.6 and 19.7 points (it was 12 and 13.8), plus conversionsShareOf at 0, 1, 10 and 40 conversions. It fails with the old all-at-once share. The 20 / 12 / 12.2 case still holds, since its pages have 40 between them.
  3. Docs. "Page value" now says that with conversions on, once the listed pages have some, it also weighs the conversions from AI journeys through the page, more as they come in.

I also reworded a few comments: a conversion is counted on each listed page its journey touched, so the share counts conversions "counted on the listed pages", and 00291's header says "of the latest 2,000". The PR description is updated.

Checks: web format, lint (0 errors, the 6 old warnings), typecheck, test (427 files, 4,071 tests), build: all pass. docs/ isn't in the format script; that file's Prettier warning is on main too.

@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.

Looks good, with follow-ups.

Round 35's items:

  1. Bound the conversions read: Done.
    • 00291 at 9fd7609 differs from 99c425f only by order by c.occurred_at desc limit 2000 in the conversions CTE, plus comments: the header, the CTE's note and the function's comment now say "of the latest 2,000".
    • The function still differs from 00238 only by additions: v_conversions_on and its read, the four CTEs and the conversions key with its left join. The signature, security definer, search_path, plan_cache_mode, the organization check and the grants are unchanged.
    • The read uses 00287's partial index. The CTE filters on brand_id =, on the index predicate kind = 'conversion' and on an occurred_at range, and it sorts in the index's order (occurred_at desc). So the scan starts at the newest conversion and stops at 2,000, with no sort step.
    • Everything after it is bounded too. seen reads the journeys of at most 2,000 visitors. touched has at most 2,000 rows per listed page (one per conversion), plus each conversion's landing page and the page it converted on. With conversions off, nothing is read, as before.
  2. One conversion re-weighs every page: Done.
    • conversionsShareOf(pages) is 0.4 × min(1, the listed pages' conversions / 20). pageValueOf(visits, conversions, share) gives (1 − share) × visits + share × conversions. The breakdown sets conversions only once the share is above 0.
    • At share 0 (conversions null or 0 everywhere), the value is visitsValue and the breakdown reads "N AI visits this period", as on main. I re-ran round 35's replay: 5,000 random plans through main's buildPlan and this head's, with conversions null and with 0. All 10,000 runs match: rows, order, scores, score parts (apart from conversions: null), hidden rows and counts. No page-value part carries a count.
    • From 20 conversions on, this head equals 99c425f exactly: 5,000 random plans with 20 to 200 conversions spread over their pages.
    • One conversion on one page (plans with AI visits):
      • no row moves by more than 0.4 points (8 at 99c425f);
      • 1 row in 100 changes badge (1 in 5 before);
      • 3 plans in 10 reorder, only among rows less than a point apart (9 in 10 before).
    • 1 to 19 conversions (plans with AI visits): no page value moves by more than the share.
    • A conversion counts on each listed page its journey touched, so a journey through three listed pages adds 6%. The code comment's "on one page, … 0.4 points at most" is exact. The PR description's "One conversion moves a row by 0.4 points at most" holds for a journey through one listed page.
    • Tooltip: unchanged. It reads "1,000 AI visits and 0 conversions this period" on a page with no conversions once the share is above 0, and "N AI visits this period" at 0.
  3. Docs: Done. The "Page value" line in docs/guides/content-editor.mdx now says that with conversions on, once the listed pages have some, page value also weighs conversions, more as they come in.

Checked:

  • Tests. This head's plan.test.ts passes on its plan.ts (26 tests).
    • On 99c425f's plan.ts, only the new case fails ("weighs conversions more as the listed pages have more", at 19.6 points).
    • On main's, the four conversion cases fail.
    • The 20 / 12 / 12.2 case still holds, since its pages have 40 conversions between them.
  • Schema. schema.sql's changes in this commit match the migration's, hunk for hunk. A fresh build-schema.sh run gives the same file, byte for byte.
  • Migration. 00291 is still the only open migration with that number.
  • Merge state. The branch is two commits on 307670b, and main is now 3b650fb. It merges cleanly. The merged tree differs from main only by this PR's 10 files. Main has added no migration since, so schema.sql carries over as it is. CI passes.
  • Before the API deploy. Unchanged from round 35: nothing here goes through the API.

Follow-ups:
4. One conversion still re-weighs every page when no listed page has an AI visit (web/src/lib/content/plan.ts:362, :198).

  • This came in with 99c425f's trafficKnown and isn't new here. It's the case item 2 is about, though.
  • What happens. With no AI visit on any listed page in the period, each page's value is neutral, 10 points. One conversion counted on a listed page turns trafficKnown on, with a 2% share. Each page's value then comes 98% from its zero visits: every page row drops by about 10 points, and the converting page drops too (to 0.1 points).
  • Example: a plan with three pages and one new page. A page losing citations goes from High (56.3) to Medium (46.3) and falls below the new page.
  • How it happens. A conversion counts on its journey's landing page, and on the listed pages the visitor saw. AI visits count only where AI visitors landed in the period. So a journey that landed on an unlisted page, or before the period, can produce this.
  • Fix: while no listed page has an AI visit, use the neutral value for the visits half:
    const visitsKnown = pages.some((p) => p.aiVisits > 0);
    const trafficKnown = visitsKnown || conversionsShare > 0;
    // page value, with conversions weighed:
    (1 - share) * (visitsKnown ? visitsValue(visits) : NEUTRAL_PAGE_VALUE) +
      share * conversionsValue(conversions)
  • Result: one conversion then moves a row by 0.2 points at most. At 20 conversions, a page with 20 of them reads 14 points and a page with none reads 6.
  • The breakdown line for that case needs its own string, saying the AI visits are unknown. The smaller fix is to keep trafficKnown on AI visits alone, as on main, so conversions are weighed only alongside visits.

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