Skip to content

feat(shopping): list the catalog's products with their AI shelf, visits, orders and readiness (Product Visibility, part 4) - #1639

Merged
gkhngyk merged 24 commits into
ansvisor:mainfrom
cihan-gyk:feature/product-visibility-products
Oct 28, 2026
Merged

gkhngyk merged 24 commits into
ansvisor:mainfrom
cihan-gyk:feature/product-visibility-products

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Product Visibility, part 4 of 14: the Products table (ECOM-007). Every product of the brand's catalog, with its AI shelf, its AI visits and orders, and its readiness.

Stacked on #1638. This PR's own change is its commits after #1638's: f7f5cf9, 0cfe141, c7d66ae.

Where you see it: Product Visibility › Products (?tab=products), for a brand with a catalog.

Each row:

Readiness adds up what the catalog can tell:

  • GTIN 20, photo 15, in stock 15, price 10;
  • a description of 25 words or more 15;
  • what the product is, in its first 40 words, 10 (ECOM-024);
  • category 10, brand 5.

Views: All, Never shown, Shown not clicked, Clicked not bought, Top sellers from AI, each with its count.

Without a catalog, or when it can't be read: the products AI showed, by title, as before, with main's CatalogNote and its link to Settings.

Migration 00361, catalog_product_ai_stats:

  • AI visits per product page, from ai_traffic_logs.
  • Orders and revenue in the main currency, from ai_traffic_events.
  • No source carries order line items yet (Shopify orders, feat(integrations): Shopify orders, with AI-referred revenue per product #1267). So an order is credited to the product whose page the AI visitor landed on first (first_url, CE-017).
  • A product's page is its link, else its first variant's, matched by path.
  • Members of the brand's organization only: security definer with an organization check.

Prices now format in one place, lib/shopping/price.ts.

Fixed in the pre-push review:

  • B2 (0cfe141): the function returned a row for every live product, and PostgREST cut it at 1,000.
    • It now returns only the products with an AI visit or an order.
    • It takes p_product_id for one product's row.
    • The table reads it page by page (readAllRpcRows, by product_id).
  • B6 (0cfe141): the table read the first 2,000 products by id, with every variant of the catalog.
    • Past 2,000, it lists the products with orders, shelves or visits first, then the rest by id.
    • Their variants, photos and categories are read for those ids, 100 at a time.
    • All and Never shown count the whole catalog (the server's count). A note says which products the table holds.
  • B5 (c7d66ae): each filter change read the whole table, and the Shelf waited for it.
    • The table now reads only while its tab is open (useKeyedRead, an older answer dropped).
    • The Shelf reads on its own.

Related issue

No issue: ECOM-007 in Drive › E-commerce › ECOM-spec.md. The screens' tasks were only in epic #1190, closed as not planned. Per-product orders from order line items wait on #1267.

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. cd web && yarn vitest run src/lib/shopping/products.test.ts src/lib/actions/shopping-products.test.ts "src/app/[locale]/(dashboard)/dashboard/shopping/_products-table.test.tsx" "src/app/[locale]/(dashboard)/dashboard/shopping/use_keyed_read.test.tsx"
  2. With 00360 and 00361 applied and a brand with a catalog:
    • open Products, switch the views, hover a readiness score;
    • change the date on Shelf: Products isn't read until you open it.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Checks:

  • At the top of the chain (part 14): server/ npm test (3,383 tests), web/ vitest (4,451 tests) and yarn build.
  • At this branch's tip: yarn typecheck, and the tests, eslint and prettier of the files it changes.
  • bash supabase/build-schema.sh leaves schema.sql as committed.

For you, @gkhngyk:

🤖 Generated with Claude Code

cihan-gyk and others added 12 commits October 27, 2026 18:33
…popup and the Shelf and Products tabs (product visibility, part 1)

The first step to the Products screens in Drive › E-commerce (ECOM-spec,
ECOM-mockup): the page says what it's for, "Which of your products AI
recommends, why, and what it sells.", with "Why Ansvisor Product
Visibility?" beside the title. That opens the four steps from the AI
shelf to the sale, and offers Connect Google Analytics while GA isn't
connected (ECOM-010).

Under the title, the data sources (ECOM-009), as on AI Traffic:
- the product catalog (its products and last sync, Manage, or Connect);
- Google Analytics and Search Console, each with what it adds here;
- a Marketplaces & feeds row: Merchant Center connects as a catalog
  source, and the marketplaces show Soon until they have a connector.

The tabs read Shelf, Prompts, Products and Competitors, the one open in
the URL (?tab=products). The sidebar item is new. AI Traffic's Google
source card is shared, with an optional line for while it isn't
connected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…oduct it shows (product visibility, part 2)

Product Visibility's Shelf, Products and Categories (ECOM-005…008)
count a brand's products on the AI shelf, so each product card that is
the brand's own now records which catalog product it shows
(prompt_result_shopping_cards.catalog_product_id, and catalog_match for
how it was found; 00360).

lib/catalog/card-match.js matches in order:
- by the marketplace listing a card links (ASIN, eBay item id), to a
  variant's marketplace_item_id;
- by a GTIN the card carries, without its padding;
- by its link, to a product's or variant's page, with or without its
  query;
- else by its title: at least 0.6 of the words in common (Dice) and
  0.1 clear of the next product, the brand's name and filler words
  aside.

A key two products share matches neither.

Cards are matched as they're written, against the brand's catalog read
at most every 10 minutes. If the catalog can't be read, the cards go in
without matches. If the API runs before 00360, they go in without the
new columns rather than not at all.

After the daily catalog sweep, each brand whose catalog synced in the
last day has its own cards of the last 90 days matched again. Only the
matches that changed are written, 500 a call, through
set_shopping_card_matches (service role only).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e brand's place and its sales from AI (product visibility, part 3)

The Shelf tab (ECOM-006, Drive › E-commerce › ECOM-mockup) is
Product Visibility's overview now.

Four numbers:
- AI shelf share: the brand's share of the product slots in AI
  answers;
- Products on the shelf: its catalog products shown (00360), "41 of
  320", else distinct titles;
- AI revenue · 30d: orders from AI visits and their value against the
  30 days before, from the tracking snippet and the Shopify pixel
  (ai_traffic_events). Without a source, "Connect to see sales" links
  to AI Traffic;
- Top AI channel: by AI orders, else by the brand's slots.

Then where AI sends shoppers and resellers cheaper than the brand, as
before. Then one row per shopping question and channel: its latest
shelf in order, with photos, prices, ratings and merchant icons. The
brand's products are ringed ("2 · You", "via Amazon"), and each row
says "You: #2 · 1 of 2 shelves this week" or "You're not on this
shelf". The shelves the brand isn't on come first.

Filters:
- channel chips, which set the page's platform filter;
- a price band, from the shelf's middle price.

The platform card rate and trend charts stay below, under Trends.

The old KPI row and the Card-eligible prompts tab give way to the
Shelf. The Shelf reads on its own, so a failure there leaves the other
tabs working and says so. ChatGPT Shopping gets its name in the
platform labels.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ts, orders and readiness (product visibility, part 4)

The Products tab (ECOM-007) lists the brand's catalog when it has one.
Each product shows:
- its photo, category and price;
- the AI shelves it was on, its win rate (#1 or #2) and average place
  over the filter window, from its matched cards (00360);
- the AI visits its page got;
- the orders those visits led to, and their revenue;
- its readiness out of 100.

Visits and orders come from catalog_product_ai_stats (00361). No source
carries order line items yet (Shopify order sync, ansvisor#1267), so an order is
credited to the product whose page the AI visitor landed on first
(ai_traffic_events.first_url). A product's page is its link, else its
first variant's, matched by path. Members of the brand's organization
only.

Readiness adds up what the catalog can tell:
- a GTIN (20);
- a photo (15);
- in stock (15);
- a price (10);
- a description of 25 words or more (15);
- what the product is, in its first 40 words (10, ECOM-024);
- a category (10);
- a brand (5).
Its tip says what's missing and what each costs.

Views: All, Never shown, Shown not clicked, Clicked not bought, and Top
sellers from AI, each with its count. The table reads up to 2,000
products and says so past that. A brand with no catalog, or one that
can't be read, keeps the products AI showed by title, with a line on
connecting the catalog. Prices format in one place
(lib/shopping/price.ts).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…a product's delete (00360)

`on delete set null` looks up a deleted catalog product's cards by the
product alone, which the (brand_id, catalog_product_id) index can't serve:
each delete would scan the cards. A partial index on catalog_product_id
covers it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…loop, and read every GTIN key in the sweep

A 50,000-product catalog took about 90 s to match 20,000 cards, in one
synchronous run. Now about 1 s, the longest hold under 70 ms:

- A word in more than 5% of the titles (and more than 500) finds no
  products to compare; it still counts in a title's score. A title of
  common words only is found by its rarest one. A product not compared
  can't come within the margin of the best: when one could (it would share
  only the card's common words), no title match is made. So every title
  match is one the full comparison makes too; a few it made are not.
  A catalog of 500 titles or fewer is matched exactly as before.
- matchCards matches 500 cards at a time, with a turn of the event loop
  between (the bot-log parser's nextTurn).
- The kept catalogs are capped at 200,000 products in all, not 50 brands;
  the brand read least recently goes first, and a read keeps a brand.
- The daily sweep reads a card's GTIN under gtin, gtin13, gtin12, gtin14,
  ean and upc, as matching at write time does, not under gtin alone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…'s active products first (00361)

catalog_product_ai_stats returned a row for every live product, so a
catalog past 1000 products lost the rest to PostgREST's max_rows, and the
Products table read at most the first 2000 products by id, with every
variant of the catalog.

- 00361 returns only the products with an AI visit or an order, and takes
  p_product_id for one product's row. The table reads it a page at a time
  (readAllRpcRows, by product_id).
- Past 2000 products, the table lists those on a shelf or visited from AI
  first (orders, then shelves, then visits), then the rest by id. Their
  rows, variants, photos and categories are read for those ids, 100 at a
  time; a catalog of 2000 or fewer is read by the brand, as before.
- All and Never shown count the whole catalog (the server's count), not
  the rows read; the note says which products the table holds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…f apart from the rest

Each filter change read the whole Products table with the page's other
reads, and the Shelf's rows waited for all of them.

- useKeyedRead: a read made once per brand and filters while it's wanted,
  an older answer dropped. The Shelf reads with it on its own; the
  Products table only while its tab is open, as Categories does.
- The no-catalog view says what main's CatalogNote says (with its link to
  Settings), not a second line of its own: products.noCatalog goes.

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

Copy link
Copy Markdown
Collaborator Author

Merged main forward (8356ba3) through the stack; supabase/schema.sql rebuilt. Typecheck and lint pass on the chain tip.

…ing cards to the catalog

A card's link has its trailing slashes cut by a walk back rather than an
end-anchored regex, and a title match reads a card title's first 40 words
(MAX_CARD_TITLE_WORDS): each word more was compared with every product it
found.

@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. Nothing in this part's own diff blocks. The branch merges in part 2's fix, so the stack carries it; nothing else changes here.

What I checked:

  • Own change. 3579d87..c7d66ae: f7f5cf9, the merge of part 3, 0cfe141 (every product's AI visits; a big catalog's active products first) and c7d66ae (the Products table only reads on its tab).
  • Merge on today's main. It needs a merge of main, with the same conflicts as part 3 (platform-labels.ts, dashboard-navigation.test.ts, server/src/server.js, supabase/schema.sql). It adds nothing new.
  • 00361 catalog_product_ai_stats.
    • security definer, search_path set to public, and every name is qualified.
    • It returns nothing unless the caller is the service role or a member of the brand's organization (profiles.organization_id = brands.organization_id).
    • Execute is revoked from public and anon and granted to authenticated and service_role.
    • It's stable and read-only, with no table or index changes.
    • It reads the brand's live products, AI visits (ai_traffic_logs, first views from an AI source, served by (brand_id, created_at)) and order conversions (ai_traffic_events.first_key). Pages are matched with normalize_page_path as elsewhere, and only products with a visit or an order come back.
    • The number clashes with nothing on main. Nothing outside this stack uses 00361.
  • Products table.
    • Up to 2,000 products. Past that, products with orders come first, then most shelves, then most visits, then the rest by id, and total counts them all. The union always reaches 2,000.
    • Variants, images and categories are read for those ids, 100 ids a read and 6 at a time.
    • All reads use the user's client, so RLS applies.
    • Readiness is a fixed weighted checklist. Its word splits are linear, and a description is at most 20,000 characters (00278).
    • useKeyedRead drops an answer for an older brand or filter key.
  • Tests. On the PR's tree, tsc is clean, and the shopping tests (20 files, 121 tests) pass, including products.test.ts, shopping-products.test.ts, _products-table.test.tsx and use_keyed_read.test.tsx. Prettier and eslint are clean.
  • Deploy. The Products tab calls the RPC, so apply 00361 (and 00360) before merging. Without them the tab shows its "Couldn't read" note, and the Shelf and the other tabs still work.

Follow-ups (none block):

  1. Two live products with the same page (a product without a link whose first variant links another product's page, or duplicates in a feed) both get that page's visits and orders, so totals across products count them twice. Credit a page to one product, for example the one whose own url it is, else the lowest id.
  2. The visits CTE runs normalize_page_path on every AI first-view in the window, for the whole brand, on each call. That's fine at today's sizes. If it shows up in timings, the stored-key approach 00286 uses for page_key would make it a plain group-by.

@gkhngyk
gkhngyk merged commit ce92aab into ansvisor:main Oct 28, 2026
1 of 2 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