Repository navigation
feat(content): weigh a page's conversions from AI journeys in the Content Plan's Priority Score (00291) - #1557
Conversation
…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]>
gkhngyk
left a comment
There was a problem hiding this comment.
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_onvariable and its read; - four CTEs:
conversions,seen,touchedandpage_conversions; - the
conversionskey and its left join.
Nothing else changes: the columns,
pages' order and its limit of 100, the filters, thejsonb_aggorder and the nulls.page_conversionsis grouped bypage_key, so the left join can't repeat a page. - the
-
Security definer. The signature,
security definer,search_path,plan_cache_modeand the organization check are unchanged.create or replacekeeps the grants from 00220/00221:authenticatedandservice_role, notanon. The flag is read after the check, and both reads ofai_traffic_eventsfilter onbrand_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.
seenis 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.touchedcounts 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'sthroughdoes. Theunioncounts a page reached two ways once.- Both sides key pages with the same
lower(normalize_page_path(...)).
-
Reads.
- Conversions off: the
conversionsCTE'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 onbrands. - 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).
- Conversions off: the
-
Null and zero.
- Conversions off gives
nullon every page; on with none in the period gives0. - In both cases
conversionsKnownstays false inplan.ts. The page value isvisitsValueas today, andtrafficKnowndoesn't change. - I ran 5,000 random plans (pages, opportunities, marks) through main's
buildPlanand this branch's, withconversionsset to null and to 0. All 10,000 runs match: rows, order, scores, score parts (apart from the newconversions: null), hidden rows and counts.
- Conversions off gives
-
The score. Once a listed page has a conversion, page value is 60% visits and 40% conversions.
conversionsValueis 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'splan.ts. -
Web.
- The Plan calls
content_plan_pagesfrom the web with the member's session, so the API isn't involved. getContentPlanmaps each field by name. Against a database without 00291,conversionsreadsnulland 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.
- The Plan calls
-
Tests. These fail without the change: the
content-plan.test.tscase ([null, null, 0]), the tooltip case and the score cases. -
Migration and merge.
- 00291 is the only open migration with that number.
schema.sqlis byte-identical to a freshbuild-schema.shrun.- 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
conversionskey 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:
- Bound the conversions read (
supabase/migrations/00291_content_plan_pages_conversions.sql:175-221).- The whole Plan now waits on this read:
getContentPlanthrows 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.
touchedholds 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.
- The whole Plan now waits on this read:
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); }
conversionsKnownbecomesconversionsSeen > 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.
- 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]>
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
|
Thanks! All three are in 9fd7609:
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. |
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good, with follow-ups.
Round 35's items:
- Bound the conversions read: Done.
- 00291 at 9fd7609 differs from 99c425f only by
order by c.occurred_at desc limit 2000in theconversionsCTE, 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_onand its read, the four CTEs and theconversionskey 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 predicatekind = 'conversion'and on anoccurred_atrange, 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.
seenreads the journeys of at most 2,000 visitors.touchedhas 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.
- 00291 at 9fd7609 differs from 99c425f only by
- 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 setsconversionsonly once the share is above 0.- At share 0 (conversions null or 0 everywhere), the value is
visitsValueand the breakdown reads "N AI visits this period", as on main. I re-ran round 35's replay: 5,000 random plans through main'sbuildPlanand this head's, withconversionsnull and with 0. All 10,000 runs match: rows, order, scores, score parts (apart fromconversions: 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.
- Docs: Done. The "Page value" line in
docs/guides/content-editor.mdxnow 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.tspasses on itsplan.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.
- On 99c425f's
- Schema.
schema.sql's changes in this commit match the migration's, hunk for hunk. A freshbuild-schema.shrun 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.sqlcarries 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
trafficKnownand 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
trafficKnownon, 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
trafficKnownon AI visits alone, as on main, so conversions are weighed only alongside visits.
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_pageswith each page's conversions.conversions: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).Web
plan.ts:PlanPage.conversions(number | null).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).content-plan.ts: maps the field; null when absent, from a database before 00291.en.json:plan.score.pageValue.visitsConversions).Checked on the dev DB (00291 is applied there):
conversions: null;content_page_conversionsfor the same pages at 30 days.Deploy: a migration, then the web.
Related issue
Content Plan Priority Score, step 2 follow-up (#1356; no issue).
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/): 0 errors; the 6 warnings were already on mainyarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)Also run:
yarn test(427 files, 4,070 tests) andyarn build;bash supabase/build-schema.sh:schema.sqlis in the PR.Tests:
plan.test.ts:pageValueOfwithout conversions is the visits alone;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-migrationlabel (I can't set labels on this repo).Pre-push review
A review agent read the change. It checked:
schema.sqlbyte-identical to a fresh build.Fixed from it:
seen, one row per visitor and page. Before, it joined every pageview to every conversion, which a crafted sender could inflate;conversionsOnkey is gone;🤖 Generated with Claude Code