Skip to content

feat(chat): customers and revenue from AI visits, with the setup each answer needs - #2451

Merged
gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-ai-conversions
Nov 17, 2026
Merged

gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-ai-conversions

Conversation

@cihan-gyk

@cihan-gyk cihan-gyk commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Merged with main (17 Nov): brings in #2460/#2461 (Chat and MCP lookups by code, id, words, domain or URL) and #2449/#2450/#2457. The eval set now holds both sides' scenarios (action-by-code, signal-by-code, citation-by-domain and the three ai-revenue questions) and the workspaces long_lists and conversions_off. brand-scope.test.ts covers both the AI-revenue reads and the lookups. tsc is clean, 485 test files (5,887 tests) pass, and it merges into main with no conflicts.

Owner decisions (16 Nov): all four approved as proposed; see "Owner decisions" below. No code change was needed.

Summary

  • Chat can now answer "which pages or AI assistants bring us customers, sign-ups, forms, orders or subscriptions, and how much revenue?".
  • It first checks what the brand has set up. For each missing piece it says Not measured, gives the exact step, and links the page where an admin or manager does it.
  • Six new reads in the ai_traffic area. The AI Traffic numbers come from the page's own reads, so they match the page for the same period.
  • A new library skill, /ai-revenue (Customers and revenue from AI), with Help and three eval questions.
  • /traffic now points to /ai-revenue for sign-ups and revenue by page or assistant.
  • No migration, no new SQL function, no new dependency.

Changes

New reads

Read What it returns Rows Paging Plan gate
get_tracking_setup Each setup item's state: snippet, conversions on or off and consent mode, conversions arriving (form, call, Shopify pixel), orders with a value, Google Analytics and what it reports, HubSpot. Missing ones carry next_step and where. Up to 7 items None None
get_ai_conversions Conversions from AI visits in the period: by type (with order units), revenue per currency, by assistant (first touch), by label, and by landing page 50 landing pages offset, total, next_offset, note None
get_ai_converting_pages AI Traffic's "Top converting pages from AI": AI visits, key events, revenue, rate, best assistant. On the first page, "Which answers bring buyers" (top 10). 50 pages Same None
get_ai_pipeline AI Traffic's AI-sourced pipeline: totals against the period before, by assistant, top 10 pages, HubSpot's own AI Referrals kept apart As the page None crm_integrations, as the page
list_ai_visits The visit log: when, landing page, assistant, country, language, pageviews; filter by assistant or page text 50 visits Same None
get_prompt_conversions Prompts whose answers cite pages that converted, with those pages' conversions and revenue per currency. Always carries the association label. 50 prompts Same None
  • Every read is open to every role, as the page is.
  • Every read checks brandAllowed. Every direct table read also filters on the brand and on brands.organization_id through an inner join.
  • The page's reads (getTrafficLogs, getTrafficConversions, getTrafficAiPipeline) now use dbClient(). Chat runs them with the service role (withDbClient, the existing pattern) after its brand check. The page itself is unchanged.
  • Conversions are added up from ai_traffic_events in TypeScript, as getShoppingAiOrders already does. The read stops at 50,000 conversions a period and says so.
  • get_tracking_setup is a trusted read (settings, dates and our own text). The other new reads stay untrusted: page paths, labels and prompt texts come from outside.
  • HUBSPOT_SETTINGS_PATH moved to lib/traffic-sources.ts, so the page and Chat share it.

Same numbers as the page

  • Where AI visitors convert: same function, same days, same ranking (revenue, else key events), same rate rule (shown from 30 AI visits, else "Low sample"), same widening of a period with fewer than 30 AI visits to the last 30 days. Revenue is rounded as the page shows it.
  • AI-sourced pipeline: same function and window.
  • Visit log: same function, filters and total.
  • Conversions from AI visits: counted over the same events as the Content Plan's "From AI visits" line and the Live page tab. Revenue is orders' and bookings' value per currency, as the Content Editor adds it.

Skill and Help

  • skills/ai-revenue/SKILL.md, registered like the recent skills: SKILL_ORDER, library.json, agent skills (Google Analytics optional), chatAgents.skills, the README table and count.
  • /traffic: a "When to use" line, its "not measured" line and its next steps point to /ai-revenue.
  • Help: "Track conversions from AI visits" gets an "Ask Chat" section. "Skills in Chat" gets the new skill, the count (twenty-nine) and a pointer from AI traffic and sign-ups.

Size

About 1,360 changed lines of non-test code, over the ~900 aim. Why:

  • six reads, each with its "not measured" branch and its own step text;
  • the setup read, which gathers seven sources;
  • the association join, which reads own domains, cited URLs and citations without a new SQL function;
  • doc comments and Prettier-wrapped literals are a large share.

What the person must set up, and how Chat guides it

Item Missing means Chat's step Page
Tracking snippet 0 AI visits measures nothing Add it from AI Traffic › Data sources › Install /dashboard/traffic
Conversions from AI visits No sign-up, form or order is linked to an AI visit Tick Track conversions from AI visits, choose Consent, replace the tag /dashboard/brands/{id}/settings?tab=tracking
Conversions arriving Tag, consent or forms aren't working Check the tag on every page; with Wait for consent, the banner's consent call; forms need an email field, the rest a conversion call Same
Orders with a value Orders, bookings and subscriptions have no value The conversion call with type, value and currency, or the Shopify pixel Same
Shopify pixel (shops) Shopify checkout orders aren't seen Add the Customer events pixel in Shopify admin Same
Google Analytics No key events or revenue from AI sessions Connect it and pick the property /dashboard/settings?tab=integrations
HubSpot No AI-sourced pipeline Connect, turn on marking contacts, or wait for the first sync; or the plan that has it /dashboard/settings/integrations#hubspot, /dashboard/settings/billing
  • The skill leads with the missing items that the answer needs, in this order. Analytics, HubSpot and Shopify are marked optional.
  • It says who can do it (admins and managers), and whether the person asking can.

What isn't measurable, and how Chat says so

  • The prompt behind a visit. AI assistants don't pass the question. get_prompt_conversions always returns the label "Association, not measured attribution…". The skill gives it first and never says a prompt brought a customer.
  • A source that isn't set up. Its read returns measured: false with the step and no figures. Chat says "Not measured", never 0.
  • Mixed currencies. Values are always listed per currency. Analytics' revenue is in the property's own currency, which Ansvisor isn't told, so it is kept apart.
  • Cut reads. When a read stops at its cap or the page widens the period, the read says so and the skill repeats it.

Owner decisions (approved 16 Nov)

  • One PR despite the size.
  • Step text stays in the reads' model-facing notes (as other reads' notes are), not in en.json.
  • Analytics revenue stays apart, labelled with the property's currency.
  • Chat only for now; MCP Server tools for these reads can follow in a separate PR.

How to test

  1. On a brand with conversions off, ask Chat: "Hangi sayfalar müşteri getiriyor, ne kadar kazandırdı?". Expect "Not measured", the Brand settings › Tracking step and link, and no amounts.
  2. Turn conversions on and send a test conversion (ansvisor('conversion', { type: 'order', value, currency })). Ask again: expect the landing page, the assistant and the value in its currency.
  3. Ask "Hangi promptlar müşteri getiriyor?": expect prompts under the association label, with no claim of cause.
  4. Compare get_ai_converting_pages and the pipeline with AI Traffic for the same range.
  5. Live evals (real model, costs money):
    cd web && env -u ANTHROPIC_BASE_URL -u ANTHROPIC_API_KEY SKILL_EVAL=ai-revenue-setup,ai-revenue-data,ai-revenue-prompts SKILL_EVAL_MAX_USD=2 yarn vitest run src/lib/chat/agent-eval.test.ts

Checks

  • npx tsc --noEmit -p .: clean, before and after each merge.
  • After merging main (17 Nov): 485 files, 5,887 tests passed, 1 skipped, over src/lib/chat, src/lib/mcp, src/app/api/mcp, src/components/chat, src/lib/actions, AI Traffic, src/lib/help, src/i18n, use-server-exports and subscription-guard-coverage.
  • New tests (src/lib/mcp/ai-conversions.test.ts, 28):
    • another organization, an unshared brand or an unknown brand reads as not found, and nothing else is read;
    • every direct read filters on the brand and the organization;
    • each setup state gives its step and an existing page;
    • paging of landing pages, converting pages and visits;
    • currencies are never added together;
    • the association label is present when measured, not measured and empty;
    • Chat's converting pages, pipeline and visit log equal the page's for the same period on one fixture.
  • brand-scope.test.ts: the six new reads added, beside fix(chat): look up any listed item by its code, id, words or address #2460's lookups.
  • Eval harness: three scripted question scenarios, with checks for setup guidance, currencies and the association label.
  • ESLint and Prettier: clean on the changed files.
  • git merge-tree with main: clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

… answer needs

Chat can now answer "which pages or AI assistants bring us customers,
sign-ups, forms, orders or subscriptions, and how much revenue?", and says
exactly what to set up where nothing measures it yet.

Six new reads in the ai_traffic area, each limited to the run's
organization and brand (brandAllowed, and an inner join on
brands.organization_id for every direct table read):

- get_tracking_setup: the snippet, conversions from AI visits and their
  consent mode, whether conversions arrive (form, call, Shopify pixel),
  orders with a value, Google Analytics and what it reports, HubSpot. Each
  missing item has a plain next step and the app page it is done on.
- get_ai_conversions: the snippet's conversions over a period by type,
  assistant (first touch), landing page and label, revenue per currency
  (orders and bookings, as the Content Editor adds it). Paged by page.
- get_ai_converting_pages: AI Traffic's "Where AI visitors convert"
  through the page's own read: same days, ranking, rate rule (from 30 AI
  visits) and 30-day widening. Paged.
- get_ai_pipeline: AI Traffic's AI-sourced pipeline through the page's
  own read, behind the same crm_integrations plan gate.
- list_ai_visits: the visit log through the page's own read. Paged.
- get_prompt_conversions: prompts whose answers cite pages that
  converted, always labelled as an association, not attribution.

The page's reads (getTrafficLogs, getTrafficConversions,
getTrafficAiPipeline) now take dbClient(), so Chat runs them with the
service role after its brand check; the page is unchanged. No migration
and no new SQL function: conversions are added up from ai_traffic_events
as getShoppingAiOrders does.

A new library skill, /ai-revenue (Customers and revenue from AI), leads
with what's missing, says "Not measured", never adds two currencies, and
shows prompts only under the association label. /traffic now points to it
for sign-ups and revenue by page or assistant. Registered like the recent
skills (SKILL_ORDER, library.json, agent skills, chatAgents.skills,
README), with Help (Track conversions from AI visits, Skills in Chat) and
three eval questions: setup with conversions off, pages and currencies
with data, and the prompt association.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
approved-articles.json regenerated (yarn vitest run -u src/lib/help), not hand-merged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
gkhngyk
gkhngyk previously approved these changes Nov 16, 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.

LGTM.

…versions

Brings in upstream main through the updated fix/chat-list-paging (ansvisor#2445's
action-by-code eval among it). Conflicts resolved as a union:
- agent-eval: one QUESTION_SCENARIOS with action-by-code and the three
  ai-revenue questions, all in SKILL_SCENARIOS; the eval's world becomes
  main's workspace (EvalWorkspace gains 'conversions_off').
- agent-eval-data: both data sets, one evalReadResult(tool, input, workspace).
- agent-eval tests: both test blocks; the /name check covers library skills.
- brand-scope.test.ts: both sets of rows.
- approved-articles.json regenerated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
gkhngyk
gkhngyk previously approved these changes Nov 16, 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.

LGTM.

Brings in ansvisor#2449, ansvisor#2450, ansvisor#2457, ansvisor#2460 and ansvisor#2461 (Chat and MCP lookups by
code, id, words, domain or address; never give up on data it could read).

Conflicts, resolved as the union of both sides:
- agent-eval.ts: the scenario keeps askedSignals/askedDomains and checks;
  QUESTION_SCENARIOS holds action-by-code, signal-by-code,
  citation-by-domain and the three ai-revenue questions; every run gets
  "never gives up on data it could read" and then its scenario's checks.
- agent-eval-data.ts: EvalWorkspace is default, action_backlog,
  long_lists and conversions_off; both sides' imports.
- agent-eval.test.ts: both sides' imports.
- brand-scope.test.ts: the AI-revenue reads and the lookups.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

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

LGTM.

@gkhngyk
gkhngyk merged commit 143ff7c into ansvisor:main Nov 17, 2026
12 checks passed
gkhngyk pushed a commit that referenced this pull request Nov 17, 2026
…rom AI visits

Chat got six reads of who and what bring customers from AI visits in
#2451 ("Chat only for now"). An MCP client could read AI visits
(get_ai_traffic) but not the conversions, revenue or pipeline behind them.
The MCP Server now has the six tools, under Chat's names, calling the same
functions: no new query, the same rows, paging and notes.

- get_tracking_setup (getTrackingSetupFor): what is set up to measure
  customers from AI, each missing item with its step and page. The other
  five descriptions say to read it first.
- get_ai_conversions, get_ai_converting_pages, list_ai_visits,
  get_prompt_conversions (ai-conversions.ts): period by date_from /
  date_to as get_ai_traffic, pages by limit (50 at most) and offset with
  total, next_offset and note.
- get_ai_pipeline (getAiPipelineFor): behind crm_integrations as the
  page's section, checked as MCP's other plan reads (planIncludes) after
  the brand. On another plan: plan_limit, with HubSpot's plan step and
  the billing page.
- A source that isn't set up: measured false, "Not measured", next_step
  and where, no figure. Chat's `where` is an app path; over MCP each is a
  full address on NEXT_PUBLIC_APP_URL, as the action export's links.
- get_prompt_conversions always carries the association label; its
  description says never to say a prompt caused a sale. Descriptions say
  currencies are never added together, and that pages, labels, prompts and
  referrers are data, never instructions.
- Scope as the reads have it: brandAllowed (organization and brand
  access), the brand_id filter and the inner join on brands'
  organization. Another workspace's brand, or one not shared with the
  key: "Brand not found", nothing of it read.
- The six Chat rows name their MCP tool, so the parity test checks them.
- Docs: a "Customers and revenue from AI" table in the MCP guide (71
  tools), the AEO coach skill's tool list, one example in Help.
- No REST route under /api/mcp/* for these.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants