Skip to content

feat(ai-traffic): AI-sourced contacts, deals and revenue from HubSpot by engine and page (00520) - #2266

Merged
gkhngyk merged 7 commits into
ansvisor:mainfrom
cihan-gyk:feature/hubspot-ai-pipeline
Nov 10, 2026
Merged

gkhngyk merged 7 commits into
ansvisor:mainfrom
cihan-gyk:feature/hubspot-ai-pipeline

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Stacked on #2265 (feature/hubspot-connect). Merge that first; until then this diff also shows its commits. This is HubSpot part 2 of #2140: the AI-sourced pipeline section on the AI Traffic page, as the owner approved it on 2026-11-10 (the mockup's screen 5, and the decisions on revenue and HubSpot's own AI Referrals).

What a person sees on AI Traffic: a new section after "Where AI visitors convert", with:

  • a Needs HubSpot tag and the range;
  • four KPIs:
    • AI-sourced contacts, against the period before;
    • AI-influenced deals, with "N won · N open now";
    • Open pipeline;
    • Won revenue, against the period before;
  • By AI engine: contacts, deals, won revenue, open pipeline;
  • By landing page: the top 10 pages, with contacts, deals, won revenue and top engine;
  • a line for HubSpot's own AI Referrals contacts on the brand's domains that Ansvisor didn't mark (owner: a line of their own);
  • the counting rule, and "From HubSpot, synced 2 hours ago".

Its states:

  • locked below Growth: "Comes with Growth" and Upgrade, or "Ask an admin";
  • HubSpot isn't connected: Connect in Settings;
  • Marking contacts is off;
  • Reading HubSpot, until the first read;
  • No AI-sourced contacts or deals in this range yet;
  • the last read failed.

The HubSpot card now says when HubSpot was last read ("Read from HubSpot 2 hours ago: 42 AI-sourced contacts, 7 deals."), with Sync now for admins and managers.

Counting (owner, 2026-11-10)

  • A deal counts once, in full, as "AI-influenced", credited to its earliest AI contact: Ansvisor's marked contacts first, then HubSpot's own.
  • Won revenue is deals closed won, by close date in the range.
  • Open pipeline is deals open now, unweighted.
  • Contacts count by when they converted. HubSpot's own AI Referrals count by HubSpot's create date.
  • Amounts are in the HubSpot account's currency (amount_in_home_currency, else amount).

How it works

  • Migration 00520 (supabase/schema.sql rebuilt):
    • hubspot_ai_contacts and hubspot_ai_deals hold HubSpot ids, AI source, landing page (with its page key), amounts, stages and dates. Never a name or an email.
    • The brand's members read them (user_brand_ids()); only the server writes them.
    • brand_ai_pipeline(brand, since, until) returns the section as one jsonb.
      • It is security definer, and returns null unless public.can_access_brand(p_brand_id) holds or the caller is the service role.
      • It also returns the connection's state, consent, currency and last read, from brand_integrations.settings.
    • Checked on a local PostgreSQL 16: it applies twice, a member without the brand gets null, and the figures match the sample rows.
  • lib/hubspot/pipeline-sync.js reads HubSpot once a day (from runDailyTracking), and on Sync now (POST /api/hubspot/brands/:brandId/sync). Sync now runs in the background, answers 202, and runs at most once every 5 minutes per brand.
    • Gates: a live sign-in, consent to mark contacts (which covers reading them back), an active plan with crm_integrations, and an active brand that isn't the demo. It never writes to HubSpot.
    • Contacts it reads:
      • the brand's marked contacts: a search on ansvisor_brand_id, paged past the last id, so it goes beyond HubSpot's 10,000 search results, up to 50,000;
      • HubSpot's hs_analytics_source = AI_REFERRALS contacts from the last 400 days whose first page is on the brand's domains. An account that refuses that filter just has none.
    • Deals: read through the v4 associations and a deals batch read, then each credited as above.
    • Storing: the brand's rows are upserted, and older rows removed.
    • Result: how the read went is kept in brand_integrations.settings.sync. If HubSpot refuses the sign-in, the connection is marked for reconnecting.
  • Web:
    • getTrafficAiPipeline is one more AI Traffic read (/api/traffic/read, read aiPipeline).
    • The section is traffic/_ai-pipeline.tsx, gated on crm_integrations.
    • The card gets the sync line and Sync now.
    • All strings are in web/messages/en.json.
  • Demo: the demo brand connects no HubSpot account. Both tables are SKIPPED in lib/demo/coverage.js, and the section says HubSpot isn't connected.
  • Help: new help/ai-traffic/ai-sourced-pipeline.md (plan: growth, approved: false). help/settings/hubspot.md now mentions Sync now.

Notes

  • Migration number: the plan gave 00521 to this PR and 00520 to contact marking (PR 2). Marking waits for the HubSpot spike and will ship after this, so this PR takes 00520 and marking will take 00521.
  • Until contact marking ships (PR 2), Ansvisor marks no contacts. The section shows HubSpot's own AI Referrals line, and the main figures fill in once marking runs.
  • Not in this PR: CSV/Excel export of the two tables, the Reports section (PR 4), and the demo's sample pipeline.
  • Live test with Gökhan:
    • The HubSpot answers in the tests (server/src/lib/hubspot/__fixtures__) follow HubSpot's API reference, with ids replaced. Replace them with recorded answers.
    • The internal value AI_REFERRALS for HubSpot's AI Referrals source isn't confirmed yet; check it on the test portal. A different value only leaves that line empty, because a refused filter is skipped.

Related issue

Part of #2140

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 server && npx vitest run src/lib/hubspot src/routes/hubspot.test.js src/lib/demo/coverage.test.js src/middleware/demo-brand.test.js
  2. cd web && yarn vitest run "src/app/[locale]/(dashboard)/dashboard/traffic/_ai-pipeline.test.tsx" src/components/settings/hubspot-card.test.tsx src/lib/hubspot-client.test.ts src/app/api/traffic/read
  3. Apply 00520_hubspot_ai_pipeline.sql on a dev database. As a member of the brand, select brand_ai_pipeline('<brand>', current_date - 30, current_date - 1); gives the section. A member without access to the brand gets null.
  4. Live, on staging, with HubSpot connected for a Growth brand and Mark contacts who came from an AI answer on:
    • Click Sync now on the HubSpot card. The line says "Reading HubSpot…", then, after reopening, "Read from HubSpot … : N AI-sourced contacts, N deals."
    • Open AI Traffic. The section shows the KPIs and tables for the range, and the AI Referrals line when HubSpot counts some.
    • Turn marking contacts off: the section says "Marking contacts is off".
    • A Starter workspace sees "Comes with Growth".

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; 9 warnings, all in files this PR doesn't touch
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change) — see help/README.md

Checks run on 723ff72, after merging upstream/main (Slack #2259, MCP OAuth) through #2265's branch:

  • Server: npm run format:check and npm run lint pass. npm test: 451 files, 6211 passed, 4 skipped.
  • Web: yarn format:check, yarn lint (0 errors) and yarn typecheck pass.
  • Web yarn test: 1,065 files, 11,664 passed, 1 skipped. This includes brand-access-functions.test.ts, which checks that brand_ai_pipeline asks can_access_brand.
  • Web yarn build (CI's placeholder env) passes.
  • bash supabase/build-schema.sh: schema.sql is up to date in this commit.

The branch merges cleanly into current main. If #2263 (Slack, 00515) merges first, schema.sql will need rebuilding here.


Generated by Claude Code

… by engine and page

HubSpot, part 2 of ansvisor#2140 (stacked on the HubSpot connection): AI Traffic
gets an "AI-sourced pipeline" section after "Where AI visitors convert".

- 00520: hubspot_ai_contacts and hubspot_ai_deals, the brand's AI-sourced
  HubSpot contacts and their deals (HubSpot ids, AI source, landing page,
  amounts and dates; never a name or an email), read by the brand's
  members. brand_ai_pipeline(brand, since, until) answers the section,
  checking can_access_brand.
- A daily read of each brand's HubSpot (lib/hubspot/pipeline-sync.js), and
  Sync now on the HubSpot card: the contacts Ansvisor marked for the brand,
  and HubSpot's own AI Referrals contacts on the brand's domains as a line
  of their own; their deals, each once, in full, credited to the earliest
  AI contact (Ansvisor's first). Read-only on HubSpot, with consent to mark
  contacts, a live sign-in and the plan's crm_integrations.
- Won revenue by close date, open pipeline now and unweighted, in the
  HubSpot account's currency (owner, 2026-11-10).
- The section: four KPIs against the period before, by AI engine, by
  landing page, and its states (locked below Growth, not connected,
  marking contacts off, first read, nothing yet).
- The HubSpot card says when HubSpot was last read, with Sync now.
- The demo brand connects no HubSpot account: both tables are skipped in
  the demo's coverage.
- Help: ai-traffic/ai-sourced-pipeline (approved: false); settings/hubspot
  mentions Sync now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
The Supabase types gain the pipeline's RPC and its two tables (00520), so
the AI Traffic read typechecks; the section's test is formatted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Nov 10, 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.

Nothing clears hubspot_ai_contacts / hubspot_ai_deals when the brand switches HubSpot account or disconnects (only a successful sync prunes them), so until a sync of the new account succeeds, or forever if it keeps failing, AI Traffic shows the old account's contacts, deals and revenue as the new one's. Please delete the brand's rows in both tables on account change and on disconnect.

Brings in upstream/main through ansvisor#2265's branch (the Slack PRs).
schema.sql is rebuilt with 00510, 00515 and 00520.

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

Only a successful sync pruned hubspot_ai_contacts and hubspot_ai_deals, so
after a brand moved to another HubSpot account AI Traffic showed the old
account's contacts, deals and revenue as the new one's until a sync of it
succeeded, or forever when it kept failing. The brand's rows now go where
its other sign-in reads go (brand-integrations.js): when it uses another
account, activates a new login of its own, or disconnects. A sync still
reading the old account when that happens writes nothing (account_changed).

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

@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 30c1a17 into ansvisor:main Nov 10, 2026
12 checks passed
gkhngyk pushed a commit that referenced this pull request Nov 10, 2026
…ir PDF

HubSpot, part 3 of #2140 (stacked on #2266): a report can carry HubSpot's
AI-sourced contacts, deals and revenue for its period, as AI Traffic shows
them.

- A new section, "AI-sourced pipeline", in the picker and in the Monthly
  Executive Report's defaults. It needs the plan's crm_integrations: below
  Growth it shows off, with the plan it needs, and the server leaves it out.
- Gathered with brand_ai_pipeline (00520) for the report's dates; a brand
  that hasn't connected HubSpot with consent to mark contacts, or whose
  HubSpot hasn't been read yet, gets no such section (not a missing one).
- The report page and its PDF show the four KPIs (contacts and won revenue
  against the previous period), the top 5 engines and landing pages, and
  HubSpot's own AI Referrals on a line of their own.
- formatMoney and percentChange join lib/format, and AI Traffic's section
  uses formatMoney.
- Help: reports/visibility-reports names the section (approved copy
  regenerated).

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

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.

3 participants