Skip to content

feat(traffic): list the Google queries no prompt tracks, with Track and Track all - #1178

Merged
gkhngyk merged 12 commits into
ansvisor:mainfrom
cihan-gyk:feature/traffic-untracked-queries
Oct 19, 2026
Merged

gkhngyk merged 12 commits into
ansvisor:mainfrom
cihan-gyk:feature/traffic-untracked-queries

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

AI Traffic Analytics, spec AT-009 (§3.6): the "Untracked Google queries" card beside "Is AI taking Google clicks?". It lists the top Search Console queries by clicks that no tracked prompt covers. Track adds a query as a prompt; Track all adds the listed ones within the plan's prompt slots (PROMPT-012). The summary line now states their number while Search Console is connected.

Stacked on #1177 (→ #1176). This PR's own change is the last commit, d103e8fe. Please merge #1176 and #1177 first.

Why

  • Spec §3.6: "Google queries you don't track yet: top GSC queries (by clicks) that match no tracked prompt. Track / Track all buttons, which respect plan slots (PROMPT-012)."
  • Spec §3.1: the summary line's example includes "number of untracked queries" (AT-007 left it for this task).
  • §3.8b rule 5: only from a connected source.

What changes

  • Migration 00182_ai_traffic_google_queries.sql: ai_traffic_google_queries(brand, since, until, latest, limit = 5000) returns the queries with clicks between two days, most clicks first, plus capped when there were more.
    • With latest, the window ends on Google's latest reported day and keeps its length (§3.01), like the Google clicks card (00174).
    • It runs as security invoker.
  • Server: GET /api/prompts/untracked-queries/:brandId?since&until&limit checks brand access, then lib/untracked-queries.js drops the queries an active prompt covers.
    • "Covers" uses the rule Prompt Suggestions already applies to Google queries (isCoveredByPrompts), so the two pages agree.
    • gsc-suggestions.js now exposes the same rule over pre-tokenized prompts (promptTokenSets / isCoveredByTokenSets), so checking 5,000 queries doesn't re-split every prompt per query. isCoveredByPrompts keeps its behaviour.
    • The prompts are read past the 1,000-row page (readAllRpcRows).
  • Web, UntrackedQueriesCard (_untracked-queries.tsx):
    • A table with Query | Clicks | Impr. | Track, and the days read in the header.
    • "N of M queries · the export has them all"; M reads "5,000+" past the scan.
    • Track all (k), where k is the rows the slots allow. The legend states the slots left.
    • Tracking uses the existing trackPagePrompts (one addPromptToSet each, plan-checked; the query text as the prompt, no topic, no target URL). It then reads the list again, so the tracked queries leave it.
    • Without slots, or for members who can't add prompts, the buttons are disabled and the tooltip says why.
    • The empty states cover: none untracked, no Search Console queries yet, and a failed read.
  • Section: the Google Search Console section is a two-column grid from 1,280px, as in the mockup (one column below).
  • Summary line: "… 12 Google queries with clicks aren't tracked as prompts."
    • Only with Search Console connected; the line waits for this count like the others.
    • After tracking from the card it counts again (queriesRevision), so the line and the card agree.
  • Export: a new "Untracked Google queries" sheet (Query, Clicks, Impressions, Days) in the whole-page Excel/CSV and on the card. It notes when only the 5,000 most clicked queries were checked. The "Needed for" list names the card.

Deploy

Apply 00182, then deploy the server (new route) and the web together.

Related issue

None. Spec AT-009 (AI Traffic Analytics plan, approved by product).

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 00182. Open AI Traffic Analytics on 30d for a brand with Search Console data.
  2. Untracked Google queries lists queries by clicks, without the ones a tracked prompt covers.
  3. Track one: a toast confirms it, the query leaves the list and the summary line's count drops by one.
  4. Track all adds the listed queries up to the slots left. With no slots, or as a member, the buttons are disabled with the reason.
  5. Excel/CSV on the card downloads every untracked query with the days read.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): no errors; 6 warnings, all in files this PR does not touch
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • npm run lint passes (run from server/)
  • npm test passes (run from server/): 1703
  • Changes are focused — one concern per PR

Also run:

  • web: yarn test (1670) and yarn build pass.
  • supabase: bash supabase/build-schema.sh; schema.sql updated.
  • Tests:
    • untracked-queries.test.js: covered queries dropped; stopword-only queries dropped; the prompt read past 1,000 rows; capped; errors.
    • prompts-untracked-queries.test.js: the days and limit; invalid input; another organization's brand.
    • _untracked-queries.test.tsx: the rows, days and slots; Track and the reload; Track all within the slots; the plan limit toast; disabled states; empty reasons; the "+" floor.
    • traffic-untracked-queries.test.ts: the API call and mapping.
    • _summary-line.test.tsx: the count only with Search Console; the floor; counting again after tracking; waiting while the state loads.
    • Export tests: the sheet and its notes.
  • Browser:
    • Setup: temporary Search Console queries on the demo brand, 15 with clicks, 3 of them covered by its prompts. The card listed 12 and the line said 12.
    • Tracking one removed it from both, and tracking another made them 10.
    • The CSV export matched the card.
    • Light and dark. At 1440 px the cards sit side by side, at 1024 they stack, with no overflow.
    • The temporary data and the two test prompts were removed afterwards.

🤖 Generated with Claude Code

cihan-gyk and others added 3 commits October 19, 2026 11:35
…l Trend card

AT-008 part A (spec §3.3): the trend card gets a "vs Google clicks" tab.
It draws AI visits and Google organic clicks, each indexed to 100 on its
first day with data, with markers on the days actions entered the Action
Center or prompts were added.

- 00180 ai_traffic_google_trend: Search Console clicks per UTC day, the
  first and latest day it has rows for the brand, and the markers.
  Security invoker; RLS decides the brand.
- The Google line is blank (not 0) before Search Console's history and
  after Google's latest reported day, which the view names.
- A range that starts mid-day leaves out its partial first day so it
  doesn't set a misleading 100.
- The 24-hour range offers "Show last 7 days" (Google reports by day).
- Without Search Console the view keeps the AI line and says what the
  Google line needs (§3.8b). The view shows the "Needs Google Search
  Console" tag.

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

AT-008 part B (spec §3.6): a Google Search Console section with "Is AI
taking Google clicks?" — pages whose impressions rose 15% or more while
their CTR fell 20% or more against the period before, most clicks lost
first. Each row's next step is the Positions panel of a tracked prompt
about the page, or Add prompts when there is none.

- 00181: gsc_page_stats (Search Console by page and day, member select
  policy) and ai_traffic_google_pages. The window keeps the range's
  length and ends on Google's latest reported day (§3.01). Pages need 100
  impressions in the period before. When the page history doesn't reach
  back to that period, it says so rather than reading every page as
  rising. Security definer with an organization check (citations).
- Server: the daily GSC sync also pulls pages. A brand's first page sync
  reads 190 days in 28-day steps, later ones the last 28, and the prune
  keeps 190 days. A failed page step leaves the query rows synced.
- Web: the section with its connection states (§3.8b), the "Needed for"
  list, and the export sheet "Is AI taking Google clicks".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nd Track all

AT-009 (spec §3.6): beside "Is AI taking Google clicks?", a card with the
top Search Console queries by clicks that no tracked prompt covers. Track
adds a query as a prompt; Track all adds the listed ones within the plan's
prompt slots (PROMPT-012). The summary line states their number while
Search Console is connected (§3.1, §3.8b rule 5).

- 00182 ai_traffic_google_queries: queries with clicks between two days,
  most clicks first, ending on Google's latest reported day (§3.01).
- Server: GET /api/prompts/untracked-queries/:brandId drops the queries
  an active prompt covers, by the rule Prompt Suggestions uses for Google
  queries, so the two pages agree. Prompts are tokenized once and read
  past the 1,000-row page.
- Web: UntrackedQueriesCard, the summary line's count (counted again after
  tracking), the export sheet "Untracked Google queries" and the "Needed
  for" list.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk AT-009 is stacked on #1177 → #1176, so please merge those first. This PR's own change is the last commit, d103e8fe.

Deploy: apply 00182 (one security-invoker function, no table change), then deploy the server (new route GET /api/prompts/untracked-queries/:brandId) and the web together.

One product note: Track adds the Google query as a prompt word for word, with no topic and no target URL. If you'd rather turn queries into question-style prompts first (as Prompt Suggestions does with a model), that could be a follow-up.

cihan-gyk and others added 2 commits October 19, 2026 16:13
main now has ansvisor#1176 (squash a98c35c, identical to this branch's 27f2ad2)
and ansvisor#1193. The conflicts are the files ansvisor#1177 extends after ansvisor#1176: this
branch's side kept; schema.sql rebuilt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…raffic-untracked-queries

main now has ansvisor#1176 (squash, identical to the stack's first commit) and ansvisor#1193;
merged through feature/traffic-google-pages, which carries it.

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.

No blockers here; it waits on #1177. 00182 on production (rolled back), as a member of the largest Search Console brand: 125 ms for 90 days and 110 ms for all time. A user from another organization gets no rows.

Two notes:

  • web/src/app/api/traffic/export/route.ts now calls the API through getUntrackedGoogleQueries without a .catch, so an API 5xx fails the whole export. .catch(() => null), as getTrafficSources has, keeps the rest of the export.
  • The page calls /untracked-queries twice per load, once for the card and once for the summary line. Each call matches up to 5,000 queries against every active prompt on the event loop. One fetch at page level would halve that.

cihan-gyk and others added 2 commits October 19, 2026 18:49
…backfill

Review of ansvisor#1177:
- ai_traffic_google_pages spent its time on normalize_page_path, twice a
  row (4 s warm, 18 s cold on 110k rows). gsc_page_stats now stores
  page_key as a generated column and the function groups by it; only the
  ranked rows work out their display path. Plus an index on date for the
  table-wide prune.
- A first page sync cut short left 28 days of history for months. The sync
  now reads the 190 days until one gets through them all for the property
  (brands.gsc_pages_backfilled_property), and drops the old property's
  pages when it changes.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Thanks. This one only has #1177's fix merged forward (3e8e82b). Both of your notes go into one small follow-up after the stack, so it doesn't grow further:

  • .catch(() => null) on getUntrackedGoogleQueries in the export;
  • one /untracked-queries fetch per page load.

Say if you'd rather have them here.

cihan-gyk and others added 2 commits October 19, 2026 20:55
The fallback prompt for a page in "Is AI taking Google clicks?" counted every citation of the page over 90 days, which read every answer that cited it (35,798 on the largest brand, 15 s cold, past the 8 s timeout). It now reads the page's latest citation and that one answer, at most one each per ranked page.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
cihan-gyk and others added 3 commits October 19, 2026 22:48
Ordering a page's citations by created_at still read every one of them from the heap, since the (url_id, brand_id) index has no created_at: 35,700 rows and 7.5 s cold on the largest brand's 50 pages. The lateral now stops at the first citation in 90 days whose prompt is active (2 s there, gkhngyk's measure), which also skips a paused prompt instead of leaving the page without one.

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.

00182 is applied on prod. On the largest brand a member reads the Google queries in 0.6 s, and a stranger gets no rows. Tests pass on main with the whole stack. I pushed a merge commit (main after #1177's squash), so the tree is unchanged apart from main.

@gkhngyk
gkhngyk merged commit 98c6cc4 into ansvisor:main Oct 19, 2026
5 checks passed
gkhngyk pushed a commit that referenced this pull request Oct 20, 2026
…d match the AI bots sheets to the report (#1244)

* fix(traffic): keep the export going without the untracked queries, and match the AI bots sheets to the report

Follow-ups from gkhngyk's AI Traffic reviews:

- #1178: the export read the untracked Google queries without a .catch,
  so an API 5xx failed the whole file. It now leaves that sheet with its
  note and exports the rest, as the AI bot report already does.
- #1182: the AI bots sheet had one row per bot while the By bot type card
  has one row per type; it now has one per type, with its bots. The cited
  pages no search bot read in 30 days get their own sheet, as the report
  lists them (the most cited, then how many more), with a note while the
  bot data is under 30 days or not whole.

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

* fix(traffic): say an export section couldn't be read instead of that it's empty

When the untracked-queries read or the AI bot report failed, the export
left it out so the rest still exported, but its sheets then said "No
query data from Google Search Console yet" or "No AI-bot requests in
this range", which wasn't true. The route now notes those sections as
unread, and their sheets say "Couldn't read this section; export again
to include it". The uncrawled-pages sheet also tells a paused alert
(stale) from one still waiting for 30 days of data, as the report does.

Review on #1244.

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

---------

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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