Skip to content

feat(content): show conversions from AI journeys through a page on the Live page tab (CE-017, part 4a) - #1530

Merged
gkhngyk merged 42 commits into
ansvisor:mainfrom
cihan-gyk:feature/ce-017-live-page-conversions
Oct 26, 2026
Merged

gkhngyk merged 42 commits into
ansvisor:mainfrom
cihan-gyk:feature/ce-017-live-page-conversions

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

CE-017, part 4a: the Live page tab. Stacked on #1529 (Settings), which is on #1528 (snippet) and #1527 (server). The PR's own changes are its commits after #1529's branch.

The editor's Live page tab gets Conversions from AI journeys (spec §7 points 26–28), from the AI Traffic snippet with conversions on. It sits under the existing Results from AI visits, which comes from Google Analytics; the two read side by side.

Migration 00287_content_page_conversions.sql: content_page_conversions(brand, page, days)

  • It's a security definer with an organization check, and uses the same whole-UTC-days window as content_page_results (00210).
  • A conversion counts for the page when its journey landed on it, went through it before the conversion, or ended on it.
  • A journey is its visitor id (the snippet makes a new id for each journey). It isn't keyed by its first visit's time: the server keeps a browser clock running ahead from dating that after the event, so the time can differ within one journey.
  • It returns:
    • per goal, the conversions and units;
    • the value of orders and bookings per currency (a value without a currency isn't summed);
    • per AI source of the first visit, the conversions;
    • the five most frequent journeys (source, pages in order without repeats in a row, goal, count), the latest first among as many;
    • the page's own key, so the tab finds This page as the database keys it;
    • whether the brand has conversions on.
  • Conversions are read through their own partial index (kind = 'conversion'): most events are pageviews.

The section:

  • Goals by the brand's kind of site (always shown, 0 when none):
    • B2B: Signups, Demo forms (its revenue comes with CE-024);
    • E-commerce: Orders, Revenue, Units sold;
    • B2C: Bookings, Revenue, Newsletter signups.
    • Any other goal it had gets a card of its own. A shop's newsletter form counts as a signup and shows as Signups.
  • By AI source, as the tab names sources elsewhere (ChatGPT's old hosts are ChatGPT; unknown keys go to Other, last).
  • Top journeys: the AI source chip, the pages with This page outlined, and the goal in green, with a count. For example: ChatGPT › / › This page › /shop/cold-brew-kit › /checkout › Purchase · 1.
  • A legend on what counts, and that visitors who refuse cookies and AI visits without a referrer aren't seen.
  • With conversions off: a note and a link to Brand settings › Tracking, with what they counted before still shown.
  • Journey chips show a shortened path's full path on hover, and each count reads as "N conversions" to a screen reader.
  • If the read fails, the rest of the tab still shows, without this section.

The Plan page's AI results strip comes in part 4b; the docs in part 5.

Related issue

Content Editor spec v2, CE-017 (§7 points 26–28, §7b).

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 00286 and 00287, turn conversions on for a brand, and have ai_traffic_events for one of its live pages. The snippet from feat(traffic): follow an AI visitor to conversions in the tracking snippet, consent first (CE-017, part 2) #1528 makes them, or insert a few.
  2. Open a draft that optimizes that page, then go to Live page.
  3. The cards follow the brand's site type, the source table counts conversions per first AI source, and the top journeys outline This page.
  4. Switch the period. The numbers follow it.
  5. Turn conversions off. The section says so and links to Brand settings.

I checked it in the browser at 1024 px, light and dark, on a demo e-commerce brand's published page with three test journeys:

  • Orders 2 · Revenue $117 · Units sold 3 · Other conversions 1;
  • ChatGPT 2, Perplexity 1;
  • three journeys with This page.

The test events and settings were removed after. I also ran the function on the dev database in rolled-back transactions:

  • a journey that never went through the page was left out;
  • a journey whose first-visit time differs between its events (a browser clock ahead) is still one journey, in order;
  • a demo form's value isn't revenue.

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 files this PR doesn't touch
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Migration added as a new file; bash supabase/build-schema.sh run and supabase/schema.sql included
  • Changes are focused — one concern per PR

Web: yarn test and yarn build. Tests:

  • cards per site type, units and other goals;
  • sources named and Other last;
  • journeys shortened past five pages;
  • the tab with B2B and e-commerce brands, conversions off, and a failed read.

A pre-push review ran; its fixes are the last commit:

  • journey key;
  • tie order;
  • the page's key from the database instead of a copy of its rules in the browser;
  • the partial index;
  • a card per goal;
  • revenue from orders and bookings only;
  • counted data kept on show while off;
  • chip title and screen-reader count.

The migration was applied on the dev database again after it, and lint, typecheck, yarn test and yarn build rerun.

🤖 Generated with Claude Code

cihan-gyk and others added 9 commits October 26, 2026 02:16
… Traffic snippet, for brands with conversions on (CE-017, part 1: server)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rder id sent first by anyone can't shut the real one out; keep no label or order ref that looks personal; check an event before reading the brand (pre-push review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ippet, with consent first: journey pages, forms, ansvisor('conversion'), Consent Mode (CE-017, part 2)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ent Mode default, read Consent Mode before keeping anything, take an id kept earlier as consent given, send only values the server takes, count a form once a page (pre-push review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e after t.js, pause on a denying default even beside regional ones, and take a queued "denied" before keeping or sending anything (consent review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…onsent call, forms, calls and the Shopify pixel to install (CE-017, part 3)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ify's cookie banner's answer to the snippet, keep the pixel's amount and units in the server's range, and say what the snippet really does (pre-push review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e Live page tab: goals by site type, by source, top journeys (CE-017, part 4a)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…time; count only orders' and bookings' value as revenue; give each goal its own card; mark this page from the database's key; keep what was counted on show while conversions are off (pre-push review)

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

@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 to fix before #1527's server is deployed.

What I checked:

  • 00287's security.
    • content_page_conversions is security definer with search_path = public, and every table it reads is schema-qualified.
    • It runs the same organization check as content_page_results (00210) before it reads anything, and returns null for a non-member.
    • Every read of ai_traffic_events is by the brand id that was checked, the pageview subqueries included, so it can't reach another brand's or organization's events.
    • It's revoked from public and anon, and granted to authenticated and service_role. It opens nothing beyond what the table's select policy (00286) already lets a member read.
  • Counting.
    • The filter is on conversions, so a conversion counts once even when its journey landed on the page, went through it and ended on it.
    • The window is whole UTC days to yesterday, as in 00210.
    • Revenue is orders' and bookings' value per currency, and a value without a currency is left out. The Units card reads orders' units.
    • Sources are first touch. Journeys are ranked by count, and the latest comes first among equal counts.
    • A journey's steps start with first_url. That's right, because the snippet sends no pageview for the first landing. Repeats in a row are dropped, and a pageview at the conversion's time comes before it.
  • The journey key. Keying by visitor_id rather than first_at is right. The server clamps first_at per event (traffic-events.js:109), so it can differ within one journey. The snippet makes a new id only when there's none, or when the old one is past 30 days.
  • Index. The partial index matches the kind = 'conversion' predicate. A plain create index on the table 00286 just made is quick. Pageview lookups use ai_traffic_events_journey.
  • The tab.
    • A failed read hides only this section.
    • Every key it builds (cards.*, goals.*) exists in en.json, and the goal keys match the table's check.
    • The settings link opens Tracking: TABS has it.
    • Tests cover the cards per site type, the sources, shortening, conversions off and a failed read.
  • Before the API deploy. The section reads Supabase, not the API, so nothing breaks. With no events yet:
    • a brand with conversions off (every brand today) sees the note and the link to Brand settings;
    • a brand that turns them on sees cards at 0 and "No conversions from AI journeys through this page in this period."
  • Merge. The stack merges on current main. Against the #1514–#1526 stack, only schema.sql conflicts.

Follow-ups (please fix before #1527's server is deployed):

  1. supabase/migrations/00287_content_page_conversions.sql:85-92 and :102-108: for each conversion, the function reads all the pageviews its visitor id has before it.

    • Those reads have no lower time bound and no cap, and nothing limits how many pageviews one visitor id can have.
    • through makes this read for every conversion of the brand in the period. 00288 caps it at 2,000; this function doesn't.
    • paths then reads the same pageviews again for each matching conversion, with normalize_page_path per row.
    • So a visitor id with a long history, and several conversions on it, can push the read past the 8 s statement timeout. The whole tab waits for this read (_impact-tab.tsx:77) before it shows without the section.

    In a follow-up migration (00288 needs the same, so one migration can replace both):

    • Read pageviews only from the start of the journey: p.occurred_at >= c.first_at - interval '1 day'. The day covers the server's 24-hour clock allowance, since first_at is clamped per event. It also keeps the read within about 31 days of the conversion.
    • Cap the steps per journey, in both the through check and paths: the latest N pageviews before the conversion (say 100).
    • Check the page once per visitor, not once per conversion: take the earliest time each visitor id saw the page, then join. Then several conversions on one id don't re-read the same pages.
    • On the dev database, time it on one visitor id with 50,000 pageviews and 1,000 conversions in the period. It should stay well under the timeout.
  2. web/src/lib/content/conversions.ts:95-97: a journey of more than five pages shows the first page, "…" and the last three. If the page itself is in the hidden middle, the journey shows without This page, though the docs (#1532) say it's marked. When you shorten a journey, keep the page in view, e.g. the first page, "…", This page, "…" and the last page. Add a test.

cihan-gyk and others added 17 commits October 26, 2026 09:36
…id and one tracking code can store, keep an order's id only as a hash, and drop an index nothing reads (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…itor takes back, run queued consent calls first, add no hooks for a visitor it doesn't follow, and never stop the site's own push or route change (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… "Don't wait" too; send no order ref without Shopify's order id, never the checkout token; say a privacy policy isn't consent (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… journey's reads compare keys instead of working each address out again (review)

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

# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
…once for the page, from a day before its first AI visit, by the stored keys; a journey's latest 100 steps; the latest 2,000 journeys), and keep this page in view when a journey is shortened (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…se that ran 00286 before them, and keep a handle such as /@acme in a path: only an email goes (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…'s part fails, and test the push wrapper with a send that fails (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…hows the consent call too (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…a journey's first page (review)

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

Copy link
Copy Markdown
Collaborator Author

Thanks, both are fixed here rather than in a follow-up migration (0303679). 00287 isn't merged yet, so it's edited in place, and re-applied on the dev database.

  1. Bounded reads. They use feat(traffic): take a journey's pageviews and conversions from the AI Traffic snippet (CE-017, part 1: server) #1527's stored keys (page_key, first_key), so no address is decoded per read.
    • The page check, once per visitor id: seen takes the earliest time each visitor id saw the page, from a day before its first AI visit to its last conversion in the period. Conversions then join to it (seen.at <= occurred_at and >= first_at - 1 day).
    • A journey's steps: the landing page, the latest 100 pageviews before the conversion (occurred_at >= first_at - interval '1 day', a limit 100 read through the journey index), and the converting page.
    • The journeys are built from the latest 2,000 matching conversions. The goals, revenue and sources still count all of them.
    • Timing on the dev database: one visitor id with 50,000 pageviews and 1,000 conversions in the period, inserted and rolled back. The 30-day read took 0.87 s and counted all 1,000 conversions; the 5 journeys have 102 steps at most. The table was vacuumed after.
  2. This page stays in view. journeyPages(pages, page) shows the first page, "…", This page, "…" and the last page when the page would fall in the hidden middle. Tests for the helper and the tab.

Also: when this page comes right after the first page, no "…" is shown between them.

Merge: I merged upstream/main in. Only page.test.tsx conflicted, and I kept both sides' mocks. Web 3,982 tests + build, server 2,792 tests.

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

What I checked:

  • Earlier items.
    1. Done (5f8b528).
      • The page check runs once per visitor id. seen (00287_content_page_conversions.sql:89-105) takes the earliest time each visitor id saw the page, by page_key, from a day before its first AI visit to its last conversion in the period. A conversion then counts through the page when that time is at or before the conversion and no more than a day before its own first visit (:113).
        • The snippet keeps one first visit per id. The server moves first_at only to the event's own time, and only when a browser clock runs ahead, by a day at most (traffic-events.js:131, :144). So the day's allowance misses no pageview of the journey.
        • seen isn't capped by a count, but #1527's per-visitor limits (300 pageviews in 30 days, traffic.js:249-259) keep each read to a few hundred rows.
      • A journey's steps are the landing page, the latest 100 pageviews from a day before its first visit (limit 100, read backwards through ai_traffic_events_journey), and the converting page (:125-140).
      • The journeys come from the latest 2,000 matching conversions (:145). Goals, revenue and sources still count all of them.
      • Pages are compared by the keys stored with each event (page_key, first_key), so no address is worked out again per row.
      • Timing. Your dev timing (0.87 s for one id with 50,000 pageviews and 1,000 conversions) covers a case the server's limits now rule out. The read is bounded at about 2,000 × 100 step rows, plus one short read per converting visitor id.
    2. Done (5f8b528, 0303679). journeyPages(pages, page) (conversions.ts:97-104) shows the first page, "…", This page, "…" and the last page when the page would fall in the gap. When the page comes right after the first, no "…" sits between them. Tests cover the helper and the tab.
  • 00287 again.
    • It's still security definer with search_path = public, and every table is schema-qualified.
    • The organization check runs before any read. Every read of ai_traffic_events, the new seen and step reads included, is by the brand id that was checked.
    • It's revoked from public and anon, and granted to authenticated and service_role.
    • Counting. seen has one row per visitor id, so the left join adds no rows, and each conversion counts once. conversions and through are each read more than once, so they're computed once.
  • Migrations. 00287 reads page_key and first_key. 00286 has them only at #1527's current head (fb41530), so apply that version of 00286 first. The partial index goes on a table that's still empty, so it's quick. schema.sql carries 00287 as the file is.
  • Merges.
    • f706346, db7d276 and ce2f3f4 change nothing beyond merging their parents.
    • db7d276 (main) had one conflict, page.test.tsx, and it keeps both sides' mocks.
    • On #1529 plus main, the PR's own change is 11 files: the migration, schema.sql, types, en.json, the action, the tab, its section and helper, and their tests.
    • On current main (with #1514), only schema.sql conflicts. CI passes.
  • Before the API deploy. The same as before: the section reads Supabase, so nothing breaks. Every brand has conversions off today, so each sees the note and the link to Brand settings.

@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Merged the PR below it forward (bcb4d85), which brings in main (00285 and the brief and editor work). No conflicts; schema.sql matches build-schema.sh.

Checks after the merge:

@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Merged #1527's holdsEmail fix forward (2b6e1fd). No change of its own.

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

What I checked:

  • Since round 31 (0303679), three merges bring in #1529's forward merges: bcb4d85, 3eb0144 and 2b6e1fd.
    • Re-merged with merge-tree: the trees match, with no conflicts.
    • The PR's own change on top of #1529 is the same as at round 31. I compared it against round 31's #1529 merged with that day's main.
  • 00287 hasn't changed since round 31: the file is the same at 0303679 and 2b6e1fd. It reads 00286's page_key and first_key, and 00286 hasn't changed either. So the order is still 00286, then 00287.
  • Merge state.
    • schema.sql matches the build.
    • The PR merges cleanly on 4efa150. On today's main, only schema.sql conflicts.
    • CI passes.
  • Before the API deploy. As before: the section reads Supabase, so nothing breaks once 00287 is applied. Every brand has conversions off, so each sees the note and the link to Brand settings.

@gkhngyk
gkhngyk merged commit 92a0534 into ansvisor:main Oct 26, 2026
4 of 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