Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

feat(traffic): list the pages that gain Google impressions while losing clicks (#1177)

Cihan80d7116e0702
diff --git a/server/src/lib/gsc-sync.js b/server/src/lib/gsc-sync.js
index b6e999ffb..6647f5020 100644
--- a/server/src/lib/gsc-sync.js
+++ b/server/src/lib/gsc-sync.js
@@ -3,9 +3,13 @@
*
* For every active brand mapped to a GSC property (whose org connection is
* live), pulls the last SYNC_WINDOW_DAYS of per-query daily stats through
- * Composio and upserts them into gsc_query_stats. Parameters are fully
- * deterministic — no LLM anywhere in this path. Per-brand failures are
- * logged and skipped; one broken property never aborts the run.
+ * Composio and upserts them into gsc_query_stats, and the same per page into
+ * gsc_page_stats (AT-008: which pages lose clicks while impressions rise). A
+ * brand's page sync reads PAGE_BACKFILL_DAYS until one gets through them all
+ * for its property, so a 90-day range has a period before it (pagesSyncPlan).
+ * Parameters are fully deterministic — no LLM anywhere in
+ * this path. Per-brand failures are logged and skipped; one broken property
+ * never aborts the run.
*/
import supabaseAdmin from '../config/supabase.js';
@@ -19,6 +23,10 @@ const ROW_LIMIT = 5000;
// — hitting it is logged, never silent.
const MAX_PAGES = 5;
const UPSERT_CHUNK = 500;
+// Page stats (AT-008): the first sync reads 190 days — a 90-day range and
+// the 90 days before it, plus Google's delay — in SYNC_WINDOW_DAYS steps so
+// each step keeps the row cap; the daily prune keeps as many.
+export const PAGE_BACKFILL_DAYS = 190;
/** YYYY-MM-DD in UTC, `daysAgo` days before now. */
function utcDateString(daysAgo = 0) {
@@ -45,16 +53,39 @@ export function mapAnalyticsRows(brandId, rows) {
return mapped;
}
-async function syncBrand(brand) {
+/** Map raw GSC rows ({keys:[page,date],...}) to gsc_page_stats rows (AT-008). */
+export function mapPageRows(brandId, rows) {
+ return mapAnalyticsRows(brandId, rows).map(({ query, ...row }) => ({ ...row, page: query }));
+}
+
+/**
+ * The windows a brand's page sync reads, as days ago: the last
+ * SYNC_WINDOW_DAYS once its PAGE_BACKFILL_DAYS have been read, else those in
+ * SYNC_WINDOW_DAYS steps.
+ */
+export function pageSyncWindows(backfilled) {
+ if (backfilled) return [{ from: SYNC_WINDOW_DAYS, to: 0 }];
+ const windows = [];
+ for (let to = 0; to < PAGE_BACKFILL_DAYS; to += SYNC_WINDOW_DAYS + 1) {
+ windows.push({ from: Math.min(to + SYNC_WINDOW_DAYS, PAGE_BACKFILL_DAYS), to });
+ }
+ return windows;
+}
+
+/**
+ * Pull one window of daily rows by `dimension` and upsert them, paging
+ * through the result set up to the row cap. Returns the rows written.
+ */
+async function pullWindow(brand, { dimension, table, onConflict, map }, { from, to }) {
const entityId = `org_${brand.organization_id}`;
let total = 0;
for (let page = 0; page < MAX_PAGES; page++) {
const rows = await queryGscSearchAnalytics(entityId, {
site_url: brand.gsc_property,
- start_date: utcDateString(SYNC_WINDOW_DAYS),
- end_date: utcDateString(0),
- dimensions: ['query', 'date'],
+ start_date: utcDateString(from),
+ end_date: utcDateString(to),
+ dimensions: [dimension, 'date'],
search_type: 'web',
row_limit: ROW_LIMIT,
start_row: page * ROW_LIMIT,
@@ -63,12 +94,10 @@ async function syncBrand(brand) {
dimension_filter_groups: [],
});
- const mapped = mapAnalyticsRows(brand.id, rows);
+ const mapped = map(brand.id, rows);
for (let i = 0; i < mapped.length; i += UPSERT_CHUNK) {
const chunk = mapped.slice(i, i + UPSERT_CHUNK);
- const { error } = await supabaseAdmin
- .from('gsc_query_stats')
- .upsert(chunk, { onConflict: 'brand_id,query,date' });
+ const { error } = await supabaseAdmin.from(table).upsert(chunk, { onConflict });
if (error) throw new Error(`Upsert failed: ${error.message}`);
}
total += mapped.length;
@@ -78,12 +107,68 @@ async function syncBrand(brand) {
}
logger.warn(
- { brandId: brand.id, total, maxRows: MAX_PAGES * ROW_LIMIT },
+ { brandId: brand.id, dimension, total, maxRows: MAX_PAGES * ROW_LIMIT },
'[gsc-sync] page cap reached — result set truncated',
);
return total;
}
+const QUERIES = {
+ dimension: 'query',
+ table: 'gsc_query_stats',
+ onConflict: 'brand_id,query,date',
+ map: mapAnalyticsRows,
+};
+const PAGES = {
+ dimension: 'page',
+ table: 'gsc_page_stats',
+ onConflict: 'brand_id,page,date',
+ map: mapPageRows,
+};
+
+async function syncBrand(brand) {
+ return pullWindow(brand, QUERIES, { from: SYNC_WINDOW_DAYS, to: 0 });
+}
+
+/**
+ * What a brand's page sync does (AT-008). Its PAGE_BACKFILL_DAYS are read
+ * once per Search Console property: until a sync gets through them all
+ * (gsc_pages_backfilled_property names the property), every sync reads them
+ * again, so one cut short resumes the next day rather than leaving the brand
+ * 28 days of history, and its 90-day range `incomplete`, for months. A brand
+ * mapped to another property drops the old one's pages first. Then the last
+ * SYNC_WINDOW_DAYS.
+ */
+export function pagesSyncPlan(brand) {
+ const backfilled = brand.gsc_pages_backfilled_property === brand.gsc_property;
+ return {
+ windows: pageSyncWindows(backfilled),
+ dropOldPages: !backfilled && brand.gsc_pages_backfilled_property != null,
+ markBackfilled: !backfilled,
+ };
+}
+
+/** The brand's page stats (AT-008), as pagesSyncPlan says. */
+async function syncBrandPages(brand) {
+ const plan = pagesSyncPlan(brand);
+ if (plan.dropOldPages) {
+ const { error } = await supabaseAdmin.from('gsc_page_stats').delete().eq('brand_id', brand.id);
+ if (error) throw new Error(error.message);
+ }
+ let total = 0;
+ for (const window of plan.windows) {
+ total += await pullWindow(brand, PAGES, window);
+ }
+ if (plan.markBackfilled) {
+ const { error } = await supabaseAdmin
+ .from('brands')
+ .update({ gsc_pages_backfilled_property: brand.gsc_property })
+ .eq('id', brand.id);
+ if (error) throw new Error(error.message);
+ }
+ return total;
+}
+
/**
* Sync every eligible brand; optionally scoped to one organization (the
* manual trigger) or to one brand (right after its property is mapped).
@@ -107,7 +192,7 @@ export async function runGscSync({ organizationId, brandId } = {}) {
// …and their active brands with a mapped property.
let brandQuery = supabaseAdmin
.from('brands')
- .select('id, organization_id, gsc_property')
+ .select('id, organization_id, gsc_property, gsc_pages_backfilled_property')
.eq('is_active', true)
.not('gsc_property', 'is', null)
.in('organization_id', connectedOrgIds);
@@ -121,7 +206,15 @@ export async function runGscSync({ organizationId, brandId } = {}) {
for (const brand of brands || []) {
try {
const count = await syncBrand(brand);
- results.push({ brandId: brand.id, rows: count });
+ // The page stats are a step of their own: failing there leaves the
+ // query rows synced.
+ let pages = null;
+ try {
+ pages = await syncBrandPages(brand);
+ } catch (err) {
+ logger.error({ err, brandId: brand.id }, '[gsc-sync] page sync failed');
+ }
+ results.push({ brandId: brand.id, rows: count, pages });
synced++;
} catch (err) {
logger.error({ err, brandId: brand.id }, '[gsc-sync] brand sync failed');
@@ -141,6 +234,13 @@ export async function runGscSync({ organizationId, brandId } = {}) {
if (pruneError) {
logger.warn({ err: pruneError }, '[gsc-sync] retention prune failed');
}
+ const { error: pagePruneError } = await supabaseAdmin
+ .from('gsc_page_stats')
+ .delete()
+ .lt('date', utcDateString(PAGE_BACKFILL_DAYS));
+ if (pagePruneError) {
+ logger.warn({ err: pagePruneError }, '[gsc-sync] page retention prune failed');
+ }
}
logger.info({ synced, skipped, total: (brands || []).length }, '[gsc-sync] completed');
diff --git a/server/src/lib/gsc-sync.test.js b/server/src/lib/gsc-sync.test.js
index 213c68024..1a08b7a89 100644
--- a/server/src/lib/gsc-sync.test.js
+++ b/server/src/lib/gsc-sync.test.js
@@ -4,7 +4,13 @@ import { describe, expect, it, vi } from 'vitest';
// when SUPABASE_* env is absent (as in CI) — stub it before the import chain.
vi.mock('../config/supabase.js', () => ({ default: {} }));
-import { mapAnalyticsRows } from './gsc-sync.js';
+import {
+ mapAnalyticsRows,
+ mapPageRows,
+ PAGE_BACKFILL_DAYS,
+ pageSyncWindows,
+ pagesSyncPlan,
+} from './gsc-sync.js';
describe('mapAnalyticsRows', () => {
it('maps GSC rows keyed by [query, date] into table rows', () => {
@@ -73,3 +79,75 @@ describe('mapAnalyticsRows', () => {
expect(mapAnalyticsRows('b', undefined)).toEqual([]);
});
});
+
+describe('mapPageRows', () => {
+ it('maps GSC rows keyed by [page, date] into gsc_page_stats rows (AT-008)', () => {
+ expect(
+ mapPageRows('brand-1', [
+ {
+ keys: ['https://acme.example/blog/x', '2026-08-01'],
+ clicks: 4.4,
+ impressions: 300,
+ ctr: 0.015,
+ position: 5.2,
+ },
+ { keys: ['https://acme.example/'] },
+ ]),
+ ).toEqual([
+ {
+ brand_id: 'brand-1',
+ page: 'https://acme.example/blog/x',
+ date: '2026-08-01',
+ clicks: 4,
+ impressions: 300,
+ ctr: 0.015,
+ position: 5.2,
+ },
+ ]);
+ });
+});
+
+describe('pageSyncWindows', () => {
+ it('reads the last 28 days once the 190 have been read', () => {
+ expect(pageSyncWindows(true)).toEqual([{ from: 28, to: 0 }]);
+ });
+
+ it('reads 190 days in 28-day steps until then, with no gap', () => {
+ const windows = pageSyncWindows(false);
+ expect(windows[0]).toEqual({ from: 28, to: 0 });
+ expect(windows.at(-1).from).toBe(PAGE_BACKFILL_DAYS);
+ for (let i = 1; i < windows.length; i++) {
+ expect(windows[i].to).toBe(windows[i - 1].from + 1);
+ expect(windows[i].from - windows[i].to).toBeLessThanOrEqual(28);
+ }
+ });
+});
+
+describe('pagesSyncPlan', () => {
+ const property = 'sc-domain:acme.com';
+
+ it('reads the 190 days, and marks them read, until a sync gets through them for the property', () => {
+ // A first sync, or one cut short after its first window: no marker yet.
+ const plan = pagesSyncPlan({ gsc_property: property, gsc_pages_backfilled_property: null });
+ expect(plan).toEqual({
+ windows: pageSyncWindows(false),
+ dropOldPages: false,
+ markBackfilled: true,
+ });
+ });
+
+ it('reads the last 28 days once the property has been read in full', () => {
+ expect(
+ pagesSyncPlan({ gsc_property: property, gsc_pages_backfilled_property: property }),
+ ).toEqual({ windows: [{ from: 28, to: 0 }], dropOldPages: false, markBackfilled: false });
+ });
+
+ it("drops the old property's pages before reading a new one's 190 days", () => {
+ expect(
+ pagesSyncPlan({
+ gsc_property: property,
+ gsc_pages_backfilled_property: 'https://old.example/',
+ }),
+ ).toEqual({ windows: pageSyncWindows(false), dropOldPages: true, markBackfilled: true });
+ });
+});
diff --git a/supabase/migrations/00181_gsc_page_stats.sql b/supabase/migrations/00181_gsc_page_stats.sql
new file mode 100644
index 000000000..bfe3dbec2
--- /dev/null
+++ b/supabase/migrations/00181_gsc_page_stats.sql
@@ -0,0 +1,272 @@
+-- ============================================================================
+-- 00181: Search Console by page, and "Is AI taking Google clicks?" (AT-008, part B)
+-- ============================================================================
+-- Spec §3.6: pages whose Google impressions rose at least 15% while their
+-- click-through rate fell at least 20% against the period before — the
+-- pattern of an AI Overview answering in place of the click. The next step
+-- for each page is the Positions panel of a tracked prompt about it.
+--
+-- gsc_query_stats (00047) holds Search Console by query, which can't say
+-- which page lost clicks, and drops anonymised queries. gsc_page_stats holds
+-- it by page and day, synced alongside (server/src/lib/gsc-sync.js): a
+-- brand's sync reads 190 days so a 90-day range has a whole period before it,
+-- Google's delay included, until one gets through them all for its property
+-- (brands.gsc_pages_backfilled_property); later ones read the last 28, and
+-- rows older than 190 days are pruned. The sync (service role) upserts on
+-- (brand_id, page, date); org members read.
+--
+-- page_key is the page as the Pages section keys it (00178), stored: worked
+-- out per row at read time it took 4 s warm, 18 s cold, on 110k rows (the
+-- largest brand's 600 pages × 183 days); grouped by the column, 46 ms.
+
+create table public.gsc_page_stats (
+ id uuid primary key default gen_random_uuid(),
+ brand_id uuid not null references public.brands(id) on delete cascade,
+ page text not null,
+ page_key text generated always as (lower(public.normalize_page_path(page))) stored,
+ date date not null,
+ clicks integer not null default 0,
+ impressions integer not null default 0,
+ ctr double precision not null default 0,
+ position double precision not null default 0,
+ created_at timestamptz not null default now(),
+ unique (brand_id, page, date)
+);
+
+create index gsc_page_stats_brand_date_idx
+ on public.gsc_page_stats (brand_id, date desc);
+
+-- The sync's table-wide retention prune (date < 190 days ago).
+create index gsc_page_stats_date_idx
+ on public.gsc_page_stats (date);
+
+-- The property whose 190 days of pages the sync has read in full. Until it
+-- matches gsc_property, every sync reads them again, so a first sync cut
+-- short resumes; when it names another property, that one's page rows are
+-- dropped first.
+alter table public.brands
+ add column if not exists gsc_pages_backfilled_property text;
+
+alter table public.gsc_page_stats enable row level security;
+
+create policy "gsc_page_stats: member select" on public.gsc_page_stats
+ for select using (
+ brand_id in (
+ select b.id
+ from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid()
+ )
+ );
+
+grant select on public.gsc_page_stats to authenticated;
+
+-- ─── Is AI taking Google clicks? ────────────────────────────────────────────
+--
+-- Pages are keyed by lower(normalize_page_path(...)), as the Pages section's
+-- are (00178). A page qualifies when the period before had at least
+-- p_min_impressions impressions (fewer would make the percentages noise),
+-- impressions rose by 15% or more and the CTR fell by 20% or more. Rows come
+-- most clicks lost first: the previous CTR's clicks on this period's
+-- impressions, minus the clicks there were.
+--
+-- Google reports about two days late: with p_latest the window ends on the
+-- latest day it has reported, keeping its length (§3.01), as the Google
+-- clicks card's does (00174). All time (p_since null) has no period before.
+-- When the brand's page history doesn't reach back to the start of the
+-- period before (a property connected lately, a long custom range), its
+-- impressions would read low and every page as rising: the result says
+-- `incomplete` and lists nothing.
+--
+-- Each row names one tracked prompt about the page, for its Positions panel:
+-- an active prompt whose target URL is the page, else an active prompt whose
+-- answers cited it over the last 90 days. citation_urls has no member select
+-- policy, so this runs as its owner after checking the caller belongs to the
+-- brand's organization; a caller from elsewhere gets nothing.
+
+create or replace function public.ai_traffic_google_pages(
+ p_brand_id uuid,
+ p_since date,
+ p_until date,
+ p_latest boolean default false,
+ p_min_impressions integer default 100,
+ p_limit integer default 50
+)
+returns jsonb
+language plpgsql
+stable
+security definer
+set search_path to 'public'
+as $$
+declare
+ v_first date;
+ v_last date;
+ v_since date;
+ v_until date;
+ v_prev_since date;
+ v_result jsonb;
+begin
+ if not exists (
+ select 1 from public.brands b
+ join public.profiles pf on pf.organization_id = b.organization_id
+ where b.id = p_brand_id and pf.id = auth.uid()
+ ) and auth.role() is distinct from 'service_role' then
+ return null;
+ end if;
+
+ select min(g.date), max(g.date) into v_first, v_last
+ from public.gsc_page_stats g
+ where g.brand_id = p_brand_id;
+
+ if p_since is null or v_last is null then
+ return jsonb_build_object(
+ 'last_day', v_last,
+ 'since', null, 'until', null, 'prev_since', null, 'prev_until', null,
+ 'incomplete', false, 'count', 0, 'rows', '[]'::jsonb
+ );
+ end if;
+
+ v_since := p_since;
+ v_until := p_until;
+ if p_latest and v_last < p_until then
+ v_since := v_last - (p_until - p_since);
+ v_until := v_last;
+ end if;
+ v_prev_since := v_since - (v_until - v_since) - 1;
+
+ if v_first > v_prev_since then
+ return jsonb_build_object(
+ 'last_day', v_last,
+ 'since', v_since, 'until', v_until,
+ 'prev_since', v_prev_since, 'prev_until', v_since - 1,
+ 'incomplete', true, 'count', 0, 'rows', '[]'::jsonb
+ );
+ end if;
+
+ with by_page as (
+ select
+ g.page_key,
+ min(g.page) as url,
+ sum(g.impressions) filter (where g.date >= v_since)::bigint as impressions,
+ sum(g.clicks) filter (where g.date >= v_since)::bigint as clicks,
+ sum(g.impressions) filter (where g.date < v_since)::bigint as prev_impressions,
+ sum(g.clicks) filter (where g.date < v_since)::bigint as prev_clicks
+ from public.gsc_page_stats g
+ where g.brand_id = p_brand_id
+ and g.date >= v_prev_since
+ and g.date <= v_until
+ group by 1
+ ),
+ flagged as (
+ select
+ bp.*,
+ bp.clicks::double precision / nullif(bp.impressions, 0) as ctr,
+ bp.prev_clicks::double precision / nullif(bp.prev_impressions, 0) as prev_ctr
+ from by_page bp
+ where bp.prev_impressions >= greatest(p_min_impressions, 1)
+ and bp.prev_clicks > 0
+ and bp.impressions >= bp.prev_impressions * 1.15
+ and bp.clicks::double precision / nullif(bp.impressions, 0)
+ <= 0.8 * bp.prev_clicks::double precision / nullif(bp.prev_impressions, 0)
+ ),
+ -- Only the rows shown work out their display path.
+ ranked as (
+ select f.*, public.normalize_page_path(f.url) as page,
+ f.prev_ctr * f.impressions - f.clicks as clicks_lost
+ from flagged f
+ order by clicks_lost desc, f.impressions desc, f.page_key
+ limit p_limit
+ ),
+ -- A tracked prompt pointing to the page.
+ targeted as (
+ select distinct on (lower(public.normalize_page_path(ptu.url)))
+ lower(public.normalize_page_path(ptu.url)) as page_key,
+ ptu.prompt_id
+ from public.prompt_target_urls ptu
+ join public.prompts p on p.id = ptu.prompt_id and p.is_active
+ join public.prompt_sets ps on ps.id = p.prompt_set_id
+ where ps.brand_id = p_brand_id
+ and lower(public.normalize_page_path(ptu.url)) in (select page_key from ranked)
+ order by lower(public.normalize_page_path(ptu.url)), p.created_at, ptu.prompt_id
+ ),
+ -- Else an active prompt that cited the page in 90 days.
+ own_domains as (
+ select lower(regexp_replace(bd.domain, '^www\.', '')) as domain
+ from public.brand_domains bd
+ where bd.brand_id = p_brand_id
+ ),
+ own_urls as (
+ select cu.id, lower(public.normalize_page_path(cu.url)) as page_key
+ from public.citation_urls cu
+ where cu.domain in (select domain from own_domains)
+ and lower(public.normalize_page_path(cu.url)) in (
+ select r.page_key from ranked r
+ where r.page_key not in (select t.page_key from targeted t)
+ )
+ ),
+ -- The first citation found whose prompt is active, not the latest or the
+ -- most-citing one: ordering needs every citation's created_at, a heap read
+ -- for each (the (url_id, brand_id) index has none), 35,700 rows and 7.5 s
+ -- cold on the largest brand's 50 pages, past the 8 s timeout with the rest.
+ -- Stopping at the first match read about 2,100 pages, in 2 s. Any prompt
+ -- that cited the page serves the Positions link, and a paused one is
+ -- skipped rather than leaving the page without one.
+ cited as (
+ select pk.page_key, c.prompt_id
+ from (
+ select ou.page_key, array_agg(ou.id) as url_ids
+ from own_urls ou
+ group by ou.page_key
+ ) pk
+ cross join lateral (
+ select pr.prompt_id
+ from public.prompt_result_citations prc
+ join public.prompt_results pr on pr.id = prc.prompt_result_id
+ join public.prompts p on p.id = pr.prompt_id and p.is_active
+ where prc.url_id = any (pk.url_ids)
+ and prc.brand_id = p_brand_id
+ and prc.created_at >= now() - interval '90 days'
+ limit 1
+ ) c
+ )
+ select jsonb_build_object(
+ 'last_day', v_last,
+ 'since', v_since,
+ 'until', v_until,
+ 'prev_since', v_prev_since,
+ 'prev_until', v_since - 1,
+ 'incomplete', false,
+ 'count', (select count(*) from flagged),
+ 'rows', coalesce((
+ select jsonb_agg(
+ jsonb_build_object(
+ 'page', r.page,
+ 'url', r.url,
+ 'impressions', r.impressions,
+ 'prev_impressions', r.prev_impressions,
+ 'clicks', r.clicks,
+ 'prev_clicks', r.prev_clicks,
+ 'prompt_id', coalesce(t.prompt_id, c.prompt_id)
+ )
+ order by r.clicks_lost desc, r.impressions desc, r.page_key
+ )
+ from ranked r
+ left join targeted t on t.page_key = r.page_key
+ left join cited c on c.page_key = r.page_key
+ ), '[]'::jsonb)
+ )
+ into v_result;
+
+ return v_result;
+end;
+$$;
+
+revoke all on function public.ai_traffic_google_pages(uuid, date, date, boolean, integer, integer)
+ from public, anon;
+grant execute on function public.ai_traffic_google_pages(uuid, date, date, boolean, integer, integer)
+ to authenticated, service_role;
+
+comment on function public.ai_traffic_google_pages(uuid, date, date, boolean, integer, integer) is
+ 'AI Traffic "Is AI taking Google clicks?" (AT-008, spec §3.6): pages whose Search Console impressions rose 15%+ while CTR fell 20%+ against the period before (at least p_min_impressions impressions before), most clicks lost first, each with a tracked prompt about it for its Positions panel (its target URL, else an active prompt that cited it in 90 days). p_latest ends the window on Google''s latest reported day. Security definer with an organization check: citation_urls has no member select policy.';
+
+notify pgrst, 'reload schema';
diff --git a/supabase/schema.sql b/supabase/schema.sql
index 747a757c6..0aea706fe 100644
--- a/supabase/schema.sql
+++ b/supabase/schema.sql
@@ -24579,3 +24579,279 @@ comment on function public.ai_traffic_google_trend(uuid, date, date) is
notify pgrst, 'reload schema';
+-- ─────────────────────────────────────────────────────────────────────────
+-- migrations/00181_gsc_page_stats.sql
+-- ─────────────────────────────────────────────────────────────────────────
+-- ============================================================================
+-- 00181: Search Console by page, and "Is AI taking Google clicks?" (AT-008, part B)
+-- ============================================================================
+-- Spec §3.6: pages whose Google impressions rose at least 15% while their
+-- click-through rate fell at least 20% against the period before — the
+-- pattern of an AI Overview answering in place of the click. The next step
+-- for each page is the Positions panel of a tracked prompt about it.
+--
+-- gsc_query_stats (00047) holds Search Console by query, which can't say
+-- which page lost clicks, and drops anonymised queries. gsc_page_stats holds
+-- it by page and day, synced alongside (server/src/lib/gsc-sync.js): a
+-- brand's sync reads 190 days so a 90-day range has a whole period before it,
+-- Google's delay included, until one gets through them all for its property
+-- (brands.gsc_pages_backfilled_property); later ones read the last 28, and
+-- rows older than 190 days are pruned. The sync (service role) upserts on
+-- (brand_id, page, date); org members read.
+--
+-- page_key is the page as the Pages section keys it (00178), stored: worked
+-- out per row at read time it took 4 s warm, 18 s cold, on 110k rows (the
+-- largest brand's 600 pages × 183 days); grouped by the column, 46 ms.
+
+create table public.gsc_page_stats (
+ id uuid primary key default gen_random_uuid(),
+ brand_id uuid not null references public.brands(id) on delete cascade,
+ page text not null,
+ page_key text generated always as (lower(public.normalize_page_path(page))) stored,
+ date date not null,
+ clicks integer not null default 0,
+ impressions integer not null default 0,
+ ctr double precision not null default 0,
+ position double precision not null default 0,
+ created_at timestamptz not null default now(),
+ unique (brand_id, page, date)
+);
+
+create index gsc_page_stats_brand_date_idx
+ on public.gsc_page_stats (brand_id, date desc);
+
+-- The sync's table-wide retention prune (date < 190 days ago).
+create index gsc_page_stats_date_idx
+ on public.gsc_page_stats (date);
+
+-- The property whose 190 days of pages the sync has read in full. Until it
+-- matches gsc_property, every sync reads them again, so a first sync cut
+-- short resumes; when it names another property, that one's page rows are
+-- dropped first.
+alter table public.brands
+ add column if not exists gsc_pages_backfilled_property text;
+
+alter table public.gsc_page_stats enable row level security;
+
+create policy "gsc_page_stats: member select" on public.gsc_page_stats
+ for select using (
+ brand_id in (
+ select b.id
+ from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid()
+ )
+ );
+
+grant select on public.gsc_page_stats to authenticated;
+
+-- ─── Is AI taking Google clicks? ────────────────────────────────────────────
+--
+-- Pages are keyed by lower(normalize_page_path(...)), as the Pages section's
+-- are (00178). A page qualifies when the period before had at least
+-- p_min_impressions impressions (fewer would make the percentages noise),
+-- impressions rose by 15% or more and the CTR fell by 20% or more. Rows come
+-- most clicks lost first: the previous CTR's clicks on this period's
+-- impressions, minus the clicks there were.
+--
+-- Google reports about two days late: with p_latest the window ends on the
+-- latest day it has reported, keeping its length (§3.01), as the Google
+-- clicks card's does (00174). All time (p_since null) has no period before.
+-- When the brand's page history doesn't reach back to the start of the
+-- period before (a property connected lately, a long custom range), its
+-- impressions would read low and every page as rising: the result says
+-- `incomplete` and lists nothing.
+--
+-- Each row names one tracked prompt about the page, for its Positions panel:
+-- an active prompt whose target URL is the page, else an active prompt whose
+-- answers cited it over the last 90 days. citation_urls has no member select
+-- policy, so this runs as its owner after checking the caller belongs to the
+-- brand's organization; a caller from elsewhere gets nothing.
+
+create or replace function public.ai_traffic_google_pages(
+ p_brand_id uuid,
+ p_since date,
+ p_until date,
+ p_latest boolean default false,
+ p_min_impressions integer default 100,
+ p_limit integer default 50
+)
+returns jsonb
+language plpgsql
+stable
+security definer
+set search_path to 'public'
+as $$
+declare
+ v_first date;
+ v_last date;
+ v_since date;
+ v_until date;
+ v_prev_since date;
+ v_result jsonb;
+begin
+ if not exists (
+ select 1 from public.brands b
+ join public.profiles pf on pf.organization_id = b.organization_id
+ where b.id = p_brand_id and pf.id = auth.uid()
+ ) and auth.role() is distinct from 'service_role' then
+ return null;
+ end if;
+
+ select min(g.date), max(g.date) into v_first, v_last
+ from public.gsc_page_stats g
+ where g.brand_id = p_brand_id;
+
+ if p_since is null or v_last is null then
+ return jsonb_build_object(
+ 'last_day', v_last,
+ 'since', null, 'until', null, 'prev_since', null, 'prev_until', null,
+ 'incomplete', false, 'count', 0, 'rows', '[]'::jsonb
+ );
+ end if;
+
+ v_since := p_since;
+ v_until := p_until;
+ if p_latest and v_last < p_until then
+ v_since := v_last - (p_until - p_since);
+ v_until := v_last;
+ end if;
+ v_prev_since := v_since - (v_until - v_since) - 1;
+
+ if v_first > v_prev_since then
+ return jsonb_build_object(
+ 'last_day', v_last,
+ 'since', v_since, 'until', v_until,
+ 'prev_since', v_prev_since, 'prev_until', v_since - 1,
+ 'incomplete', true, 'count', 0, 'rows', '[]'::jsonb
+ );
+ end if;
+
+ with by_page as (
+ select
+ g.page_key,
+ min(g.page) as url,
+ sum(g.impressions) filter (where g.date >= v_since)::bigint as impressions,
+ sum(g.clicks) filter (where g.date >= v_since)::bigint as clicks,
+ sum(g.impressions) filter (where g.date < v_since)::bigint as prev_impressions,
+ sum(g.clicks) filter (where g.date < v_since)::bigint as prev_clicks
+ from public.gsc_page_stats g
+ where g.brand_id = p_brand_id
+ and g.date >= v_prev_since
+ and g.date <= v_until
+ group by 1
+ ),
+ flagged as (
+ select
+ bp.*,
+ bp.clicks::double precision / nullif(bp.impressions, 0) as ctr,
+ bp.prev_clicks::double precision / nullif(bp.prev_impressions, 0) as prev_ctr
+ from by_page bp
+ where bp.prev_impressions >= greatest(p_min_impressions, 1)
+ and bp.prev_clicks > 0
+ and bp.impressions >= bp.prev_impressions * 1.15
+ and bp.clicks::double precision / nullif(bp.impressions, 0)
+ <= 0.8 * bp.prev_clicks::double precision / nullif(bp.prev_impressions, 0)
+ ),
+ -- Only the rows shown work out their display path.
+ ranked as (
+ select f.*, public.normalize_page_path(f.url) as page,
+ f.prev_ctr * f.impressions - f.clicks as clicks_lost
+ from flagged f
+ order by clicks_lost desc, f.impressions desc, f.page_key
+ limit p_limit
+ ),
+ -- A tracked prompt pointing to the page.
+ targeted as (
+ select distinct on (lower(public.normalize_page_path(ptu.url)))
+ lower(public.normalize_page_path(ptu.url)) as page_key,
+ ptu.prompt_id
+ from public.prompt_target_urls ptu
+ join public.prompts p on p.id = ptu.prompt_id and p.is_active
+ join public.prompt_sets ps on ps.id = p.prompt_set_id
+ where ps.brand_id = p_brand_id
+ and lower(public.normalize_page_path(ptu.url)) in (select page_key from ranked)
+ order by lower(public.normalize_page_path(ptu.url)), p.created_at, ptu.prompt_id
+ ),
+ -- Else an active prompt that cited the page in 90 days.
+ own_domains as (
+ select lower(regexp_replace(bd.domain, '^www\.', '')) as domain
+ from public.brand_domains bd
+ where bd.brand_id = p_brand_id
+ ),
+ own_urls as (
+ select cu.id, lower(public.normalize_page_path(cu.url)) as page_key
+ from public.citation_urls cu
+ where cu.domain in (select domain from own_domains)
+ and lower(public.normalize_page_path(cu.url)) in (
+ select r.page_key from ranked r
+ where r.page_key not in (select t.page_key from targeted t)
+ )
+ ),
+ -- The first citation found whose prompt is active, not the latest or the
+ -- most-citing one: ordering needs every citation's created_at, a heap read
+ -- for each (the (url_id, brand_id) index has none), 35,700 rows and 7.5 s
+ -- cold on the largest brand's 50 pages, past the 8 s timeout with the rest.
+ -- Stopping at the first match read about 2,100 pages, in 2 s. Any prompt
+ -- that cited the page serves the Positions link, and a paused one is
+ -- skipped rather than leaving the page without one.
+ cited as (
+ select pk.page_key, c.prompt_id
+ from (
+ select ou.page_key, array_agg(ou.id) as url_ids
+ from own_urls ou
+ group by ou.page_key
+ ) pk
+ cross join lateral (
+ select pr.prompt_id
+ from public.prompt_result_citations prc
+ join public.prompt_results pr on pr.id = prc.prompt_result_id
+ join public.prompts p on p.id = pr.prompt_id and p.is_active
+ where prc.url_id = any (pk.url_ids)
+ and prc.brand_id = p_brand_id
+ and prc.created_at >= now() - interval '90 days'
+ limit 1
+ ) c
+ )
+ select jsonb_build_object(
+ 'last_day', v_last,
+ 'since', v_since,
+ 'until', v_until,
+ 'prev_since', v_prev_since,
+ 'prev_until', v_since - 1,
+ 'incomplete', false,
+ 'count', (select count(*) from flagged),
+ 'rows', coalesce((
+ select jsonb_agg(
+ jsonb_build_object(
+ 'page', r.page,
+ 'url', r.url,
+ 'impressions', r.impressions,
+ 'prev_impressions', r.prev_impressions,
+ 'clicks', r.clicks,
+ 'prev_clicks', r.prev_clicks,
+ 'prompt_id', coalesce(t.prompt_id, c.prompt_id)
+ )
+ order by r.clicks_lost desc, r.impressions desc, r.page_key
+ )
+ from ranked r
+ left join targeted t on t.page_key = r.page_key
+ left join cited c on c.page_key = r.page_key
+ ), '[]'::jsonb)
+ )
+ into v_result;
+
+ return v_result;
+end;
+$$;
+
+revoke all on function public.ai_traffic_google_pages(uuid, date, date, boolean, integer, integer)
+ from public, anon;
+grant execute on function public.ai_traffic_google_pages(uuid, date, date, boolean, integer, integer)
+ to authenticated, service_role;
+
+comment on function public.ai_traffic_google_pages(uuid, date, date, boolean, integer, integer) is
+ 'AI Traffic "Is AI taking Google clicks?" (AT-008, spec §3.6): pages whose Search Console impressions rose 15%+ while CTR fell 20%+ against the period before (at least p_min_impressions impressions before), most clicks lost first, each with a tracked prompt about it for its Positions panel (its target URL, else an active prompt that cited it in 90 days). p_latest ends the window on Google''s latest reported day. Security definer with an organization check: citation_urls has no member select policy.';
+
+notify pgrst, 'reload schema';
+
diff --git a/web/messages/en.json b/web/messages/en.json
index 4810352f1..090302b44 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -3134,6 +3134,7 @@
"convertingPages": "Top converting pages",
"buyerAnswers": "Answers that bring buyers",
"pageStatus": "Pages by status",
+ "googlePages": "Is AI taking Google clicks",
"visits": "Visits"
},
"columns": {
@@ -3172,7 +3173,15 @@
"conversionRate": "Rate (%)",
"bestPlatform": "Best platform",
"topic": "Topic",
- "citedPages": "Cited pages"
+ "citedPages": "Cited pages",
+ "impressions": "Impressions",
+ "previousImpressions": "Impressions, previous period",
+ "ctr": "CTR (%)",
+ "previousCtr": "CTR, previous period (%)",
+ "clicks": "Clicks",
+ "previousClicks": "Clicks, previous period",
+ "days": "Days",
+ "previousDays": "Days, previous period"
},
"metrics": {
"visits": "AI-referred visits",
@@ -3190,7 +3199,10 @@
"noPages": "No pages in this range",
"noConversions": "No key events from AI visitors in this range",
"lastThirtyDays": "Last 30 days: the selected range has fewer than 30 AI visits",
- "conversionLowSample": "Low sample: fewer than 30 AI visits"
+ "conversionLowSample": "Low sample: fewer than 30 AI visits",
+ "noGooglePages": "No page data from Google Search Console yet",
+ "noGoogleClicksLost": "No page's impressions rose 15% while its CTR fell 20% in this range",
+ "googleHistoryShort": "Google Search Console's page history doesn't reach back to the period before this range"
},
"sourceStates": {
"active": "Active",
@@ -3357,7 +3369,37 @@
"keyEvents": "key events from AI",
"googleClicks": "Google clicks",
"conversions": "where AI visitors convert",
- "pageValue": "which valuable pages get no AI traffic"
+ "pageValue": "which valuable pages get no AI traffic",
+ "googleTrend": "AI visits against Google clicks",
+ "googlePages": "which pages AI may be taking Google clicks from"
+ }
+ },
+ "google": {
+ "title": "Google Search Console",
+ "subtitle": "Is AI changing your Google traffic?",
+ "dataUntil": "data until {day} (Google reports about two days late)",
+ "taking": {
+ "title": "Is AI taking Google clicks?",
+ "legend": "Impressions up 15% or more while CTR fell 20% or more, on pages with at least {min} impressions before: an AI Overview may be answering in place of the click. Open Positions to see whether you're cited in it.",
+ "compared": "{days} vs {prevDays}",
+ "empty": "No page's impressions rose while its CTR fell this way.",
+ "allTime": "All time has no period before it to compare with. Pick a range.",
+ "incomplete": "Google Search Console's page history doesn't reach back to the period before this range yet. Pick a shorter range.",
+ "noPages": "Page data from Google Search Console arrives with the next daily sync.",
+ "failed": "Couldn't load these pages.",
+ "more": "{shown} of {count} pages · the export has them all",
+ "columns": {
+ "page": "Page",
+ "impressions": "Impr.",
+ "ctr": "CTR",
+ "next": "Next step"
+ },
+ "impressionsTip": "{prev} → {now} impressions",
+ "ctrTip": "{prev} → {now} clicks",
+ "positions": "Positions",
+ "positionsTip": "See in Positions whether you're cited for a tracked prompt about this page",
+ "addPrompts": "Add prompts",
+ "addPromptsTip": "No tracked prompt is about this page yet"
}
},
"summaryLine": {
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_google-section.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_google-section.test.tsx
new file mode 100644
index 000000000..09f038cf0
--- /dev/null
+++ b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_google-section.test.tsx
@@ -0,0 +1,189 @@
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { NextIntlClientProvider } from 'next-intl';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import messages from '../../../../../../messages/en.json';
+import type { SourceState } from '@/lib/actions/integrations';
+import type { TrafficDateWindow, TrafficGooglePages } from '@/lib/actions/traffic';
+
+/**
+ * The Google Search Console section (AT-008 part B, spec §3.6): pages whose
+ * impressions rose while their CTR fell, over the days Google has reported,
+ * each with its next step; and Search Console's own state in full until it
+ * has data (§3.8b).
+ */
+
+const getTrafficGooglePages = vi.fn();
+vi.mock('@/lib/actions/traffic', () => ({
+ getTrafficGooglePages: (...args: unknown[]) => getTrafficGooglePages(...args),
+}));
+vi.mock('@/hooks/use-integration-connect', () => ({
+ useIntegrationConnect: () => ({ connecting: false, connect: vi.fn() }),
+}));
+vi.mock('@/i18n/navigation', () => ({
+ Link: ({ href, ...props }: { href: string }) => <a href={href} {...props} />,
+}));
+// The Add prompts dialog has its own tests; this one reads which page it opens for.
+const dialogPages: (string | null)[] = [];
+vi.mock('./_page-prompts-dialog', () => ({
+ PagePromptsDialog: ({ page }: { page: string | null }) => {
+ dialogPages.push(page);
+ return null;
+ },
+}));
+
+const { GoogleSection } = await import('./_google-section');
+
+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT =
+ true;
+
+const WEEK: TrafficDateWindow = { dateFrom: '2026-10-12T09:00:00.000Z' };
+
+const gsc = (over: Partial<SourceState> = {}): SourceState => ({
+ configured: true,
+ connected: true,
+ mapped: true,
+ firstDataAt: '2026-09-01T06:00:00.000Z',
+ lastDataAt: '2026-10-18T06:00:00.000Z',
+ property: 'sc-domain:acme.example',
+ ...over,
+});
+
+const pages = (over: Partial<TrafficGooglePages> = {}): TrafficGooglePages => ({
+ lastDay: '2026-10-17',
+ since: '2026-10-11',
+ until: '2026-10-17',
+ prevSince: '2026-10-04',
+ prevUntil: '2026-10-10',
+ incomplete: false,
+ count: 2,
+ rows: [
+ {
+ page: '/blog/how-to-track-ai-overviews',
+ url: 'https://acme.example/blog/how-to-track-ai-overviews',
+ impressions: 1420,
+ prevImpressions: 1000,
+ clicks: 14,
+ prevClicks: 31,
+ promptId: 'p1',
+ },
+ {
+ page: '/faq',
+ url: 'https://acme.example/faq',
+ impressions: 300,
+ prevImpressions: 200,
+ clicks: 2,
+ prevClicks: 8,
+ promptId: null,
+ },
+ ],
+ ...over,
+});
+
+let container: HTMLDivElement;
+let root: Root;
+
+beforeEach(() => {
+ dialogPages.length = 0;
+ getTrafficGooglePages.mockReset().mockResolvedValue(pages());
+ container = document.createElement('div');
+ document.body.appendChild(container);
+ root = createRoot(container);
+});
+
+afterEach(() => {
+ act(() => root.unmount());
+ container.remove();
+});
+
+async function render(source: SourceState | null = gsc()) {
+ await act(async () => {
+ root.render(
+ <NextIntlClientProvider locale="en" messages={messages} timeZone="UTC">
+ <GoogleSection
+ brandId="b1"
+ dateWindow={WEEK}
+ rangeLabel="last 7 days"
+ source={source}
+ domain="acme.example"
+ canConnect
+ onConnected={() => {}}
+ />
+ </NextIntlClientProvider>,
+ );
+ });
+}
+
+/** A fresh section: it reads its pages again. */
+async function remount() {
+ act(() => root.unmount());
+ root = createRoot(container);
+ await render();
+}
+
+const text = () => container.textContent ?? '';
+
+describe('GoogleSection', () => {
+ it('lists the pages with their rise, their CTR and the days compared', async () => {
+ await render();
+ expect(getTrafficGooglePages).toHaveBeenCalledWith('b1', WEEK);
+ expect(text()).toContain('Google Search Console');
+ expect(text()).toContain('Needs Google Search Console');
+ expect(text()).toContain('data until Oct 17 (Google reports about two days late)');
+ expect(text()).toContain('Oct 11 – Oct 17 vs Oct 4 – Oct 10');
+ const rows = [...container.querySelectorAll('tbody tr')].map((r) =>
+ [...r.querySelectorAll('td')].map((td) => td.textContent),
+ );
+ expect(rows).toEqual([
+ ['/blog/how-to-track-ai-overviews', '+42%', '3.1% → 1%', 'Positions'],
+ ['/faq', '+50%', '4% → 0.7%', 'Add prompts'],
+ ]);
+ expect(text()).toContain('at least 100 impressions before');
+ expect(text()).not.toContain('of 2 pages');
+ });
+
+ it("opens a page's prompt in Positions, or Add prompts for the page", async () => {
+ await render();
+ const link = container.querySelector('a');
+ expect(link?.getAttribute('href')).toBe('/dashboard/prompts?positions=p1');
+
+ const add = [...container.querySelectorAll('button')].find(
+ (b) => b.textContent === 'Add prompts',
+ );
+ await act(async () => add!.click());
+ expect(dialogPages.at(-1)).toBe('/faq');
+ });
+
+ it('says how many more pages the export has', async () => {
+ getTrafficGooglePages.mockResolvedValue(pages({ count: 14 }));
+ await render();
+ expect(text()).toContain('2 of 14 pages · the export has them all');
+ });
+
+ it('says why there are no rows', async () => {
+ getTrafficGooglePages.mockResolvedValue(pages({ rows: [], count: 0 }));
+ await render();
+ expect(text()).toContain("No page's impressions rose while its CTR fell this way.");
+
+ getTrafficGooglePages.mockResolvedValue(pages({ lastDay: null, since: null, rows: [] }));
+ await remount();
+ expect(text()).toContain('arrives with the next daily sync');
+
+ getTrafficGooglePages.mockResolvedValue(pages({ since: null, until: null, rows: [] }));
+ await remount();
+ expect(text()).toContain('All time has no period before it');
+
+ getTrafficGooglePages.mockResolvedValue(pages({ incomplete: true, count: 0, rows: [] }));
+ await remount();
+ expect(text()).toContain("page history doesn't reach back to the period before this range");
+ });
+
+ it("shows Search Console's state in full until it has data (§3.8b)", async () => {
+ await render(
+ gsc({ connected: false, mapped: false, firstDataAt: null, lastDataAt: null, property: null }),
+ );
+ expect(getTrafficGooglePages).not.toHaveBeenCalled();
+ expect(text()).toContain('Connect Google Search Console to see this');
+ expect(text()).toContain('which pages AI may be taking Google clicks from');
+ });
+});
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_google-section.tsx b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_google-section.tsx
new file mode 100644
index 000000000..a24de9459
--- /dev/null
+++ b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_google-section.tsx
@@ -0,0 +1,277 @@
+'use client';
+
+import { useEffect, useState, type ReactNode } from 'react';
+import { useTranslations } from 'next-intl';
+import { Search } from 'lucide-react';
+import { Link } from '@/i18n/navigation';
+import { Badge } from '@/components/ui/badge';
+import { Button } from '@/components/ui/button';
+import { buttonVariants } from '@/components/ui/button-variants';
+import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
+import { Skeleton } from '@/components/ui/skeleton';
+import {
+ Table,
+ TableBody,
+ TableCell,
+ TableHead,
+ TableHeader,
+ TableRow,
+} from '@/components/ui/table';
+import { cn } from '@/lib/utils';
+import { formatNumber } from '@/lib/format';
+import { googleSourceStatus } from '@/lib/traffic-sources';
+import type { SourceState } from '@/lib/actions/integrations';
+import { GOOGLE_PAGES_MIN_IMPRESSIONS, ctrPct, impressionsRise } from '@/lib/traffic-google-pages';
+import {
+ getTrafficGooglePages,
+ type TrafficDateWindow,
+ type TrafficGooglePages,
+} from '@/lib/actions/traffic';
+import { formatTrafficBucketDate } from './_charts';
+import { PagePromptsDialog } from './_page-prompts-dialog';
+import { NeedsTag, SourceUnavailable } from './_source-gate';
+
+/** CTR as the table shows it: "3.1%". */
+function ctr(clicks: number, impressions: number): string {
+ return `${(ctrPct(clicks, impressions) ?? 0).toLocaleString('en-US')}%`;
+}
+
+/** "Oct 3 – Oct 17". */
+function days(since: string, until: string): string {
+ return `${formatTrafficBucketDate(since)} – ${formatTrafficBucketDate(until)}`;
+}
+
+/**
+ * The Google Search Console section (AT-008 part B, spec §3.6). Its card
+ * "Is AI taking Google clicks?" lists the pages whose impressions rose 15% or
+ * more while their CTR fell 20% or more against the period before: an AI
+ * Overview may be answering in place of the click. Each page's next step is
+ * the Positions panel of a tracked prompt about it, or Add prompts when
+ * there is none.
+ *
+ * Google reports about two days late, so the days compared end on the latest
+ * it has reported, and the header says which (§3.01). The section needs
+ * Search Console; until it has data it shows its state in full (§3.8b).
+ */
+export function GoogleSection({
+ brandId,
+ dateWindow,
+ rangeLabel,
+ source,
+ domain,
+ canConnect,
+ onConnected,
+ actions,
+}: {
+ brandId: string;
+ dateWindow: TrafficDateWindow;
+ /** The page's own words for the range, e.g. "last 7 days". */
+ rangeLabel: string;
+ /** Google Search Console's state for the brand; null while unknown. */
+ source: SourceState | null;
+ domain: string | undefined;
+ canConnect: boolean;
+ onConnected: () => void;
+ /** The card's Excel / CSV buttons (AT-015). */
+ actions?: ReactNode;
+}) {
+ const t = useTranslations('traffic.google');
+ // Tracking prompts for a page reads the list again, for its Positions link.
+ const [reloads, setReloads] = useState(0);
+ const key = `${brandId}|${dateWindow.dateFrom ?? ''}|${dateWindow.dateTo ?? ''}|${reloads}`;
+ // The result carries the range it answers, as the other sections' do.
+ const [loaded, setLoaded] = useState<{ key: string; result: TrafficGooglePages | null } | null>(
+ null,
+ );
+ // The page whose suggested prompts are open; each opening starts afresh.
+ const [promptsFor, setPromptsFor] = useState<string | null>(null);
+ const [promptsOpened, setPromptsOpened] = useState(0);
+
+ const gscReady = source === null || googleSourceStatus(source) === 'connected';
+
+ useEffect(() => {
+ if (!gscReady) return;
+ let cancelled = false;
+ getTrafficGooglePages(brandId, dateWindow)
+ .then((result) => {
+ if (!cancelled) setLoaded({ key, result });
+ })
+ .catch((err) => {
+ // Best effort: the rest of the page doesn't depend on this section.
+ console.error('Failed to load the Google pages:', err);
+ if (!cancelled) setLoaded({ key, result: null });
+ });
+ return () => {
+ cancelled = true;
+ };
+ }, [brandId, dateWindow, key, gscReady, reloads]);
+
+ const current = loaded?.key === key ? loaded : null;
+ const result = current?.result ?? null;
+
+ const message = (text: string) => (
+ <p className="px-6 py-4 text-sm text-muted-foreground">{text}</p>
+ );
+ const body = !current ? (
+ <div className="px-6 py-4">
+ <Skeleton className="h-4 w-56" />
+ </div>
+ ) : !result ? (
+ message(t('taking.failed'))
+ ) : !result.lastDay ? (
+ message(t('taking.noPages'))
+ ) : !result.since ? (
+ message(t('taking.allTime'))
+ ) : result.incomplete ? (
+ message(t('taking.incomplete'))
+ ) : result.rows.length === 0 ? (
+ message(t('taking.empty'))
+ ) : (
+ <Table>
+ <TableHeader>
+ <TableRow>
+ <TableHead className="pl-6">{t('taking.columns.page')}</TableHead>
+ <TableHead className="text-right">{t('taking.columns.impressions')}</TableHead>
+ <TableHead className="text-right">{t('taking.columns.ctr')}</TableHead>
+ <TableHead className="pr-6 text-right">{t('taking.columns.next')}</TableHead>
+ </TableRow>
+ </TableHeader>
+ <TableBody>
+ {result.rows.map((row) => (
+ <TableRow key={row.page} className="hover:bg-muted/50">
+ <TableCell className="pl-6 max-w-[180px] xl:max-w-[320px]">
+ <span
+ className="font-mono text-xs text-muted-foreground line-clamp-1"
+ title={row.url}
+ >
+ {row.page}
+ </span>
+ </TableCell>
+ <TableCell
+ className="text-right text-sm tabular-nums"
+ title={t('taking.impressionsTip', {
+ prev: formatNumber(row.prevImpressions),
+ now: formatNumber(row.impressions),
+ })}
+ >
+ {`+${formatNumber(impressionsRise(row))}%`}
+ </TableCell>
+ <TableCell
+ className="text-right text-sm tabular-nums text-red-600 dark:text-red-400"
+ title={t('taking.ctrTip', {
+ prev: formatNumber(row.prevClicks),
+ now: formatNumber(row.clicks),
+ })}
+ >
+ {`${ctr(row.prevClicks, row.prevImpressions)} → ${ctr(row.clicks, row.impressions)}`}
+ </TableCell>
+ <TableCell className="pr-6 text-right">
+ {row.promptId ? (
+ <Link
+ href={`/dashboard/prompts?positions=${row.promptId}`}
+ title={t('taking.positionsTip')}
+ className={cn(buttonVariants({ variant: 'outline', size: 'sm' }), 'h-7 text-xs')}
+ >
+ {t('taking.positions')}
+ </Link>
+ ) : (
+ <Button
+ size="sm"
+ variant="outline"
+ className="h-7 text-xs"
+ title={t('taking.addPromptsTip')}
+ onClick={() => {
+ setPromptsFor(row.page);
+ setPromptsOpened((n) => n + 1);
+ }}
+ >
+ {t('taking.addPrompts')}
+ </Button>
+ )}
+ </TableCell>
+ </TableRow>
+ ))}
+ </TableBody>
+ </Table>
+ );
+
+ return (
+ <section className="space-y-3" aria-labelledby="google-search-console-title">
+ <div className="flex flex-wrap items-center gap-x-3 gap-y-1">
+ <h2
+ id="google-search-console-title"
+ className="flex items-center gap-2 text-base font-semibold"
+ >
+ <Search className="h-4 w-4 text-muted-foreground" />
+ {t('title')}
+ </h2>
+ <NeedsTag provider="google-search-console" />
+ <span className="text-xs text-muted-foreground">
+ {t('subtitle')}
+ {result?.lastDay &&
+ ` · ${t('dataUntil', { day: formatTrafficBucketDate(result.lastDay) })}`}
+ </span>
+ <Badge variant="outline" className="ml-auto text-xs">
+ {rangeLabel}
+ </Badge>
+ </div>
+
+ {!gscReady && source ? (
+ <Card>
+ <CardContent className="p-0">
+ <SourceUnavailable
+ provider="google-search-console"
+ state={source}
+ domain={domain}
+ canConnect={canConnect}
+ onConnected={onConnected}
+ />
+ </CardContent>
+ </Card>
+ ) : (
+ <Card className={cn(!current && 'opacity-80')} aria-busy={!current}>
+ <CardHeader className="pb-2">
+ <div className="flex flex-wrap items-center justify-between gap-2">
+ <div className="flex flex-wrap items-baseline gap-x-2">
+ <CardTitle className="text-sm font-medium">{t('taking.title')}</CardTitle>
+ {result?.since && result.until && result.prevSince && result.prevUntil && (
+ <span className="text-xs text-muted-foreground">
+ {t('taking.compared', {
+ days: days(result.since, result.until),
+ prevDays: days(result.prevSince, result.prevUntil),
+ })}
+ </span>
+ )}
+ </div>
+ {actions}
+ </div>
+ </CardHeader>
+ <CardContent className="p-0">
+ {body}
+ {result && result.count > result.rows.length && (
+ <p className="px-6 pt-3 text-xs text-muted-foreground">
+ {t('taking.more', {
+ shown: formatNumber(result.rows.length),
+ count: formatNumber(result.count),
+ })}
+ </p>
+ )}
+ <p className="px-6 py-3 text-xs text-muted-foreground">
+ {t('taking.legend', { min: formatNumber(GOOGLE_PAGES_MIN_IMPRESSIONS) })}
+ </p>
+ </CardContent>
+ </Card>
+ )}
+
+ <PagePromptsDialog
+ key={promptsOpened}
+ brandId={brandId}
+ dateWindow={dateWindow}
+ domain={domain}
+ page={promptsFor}
+ onClose={() => setPromptsFor(null)}
+ onTracked={() => setReloads((n) => n + 1)}
+ />
+ </section>
+ );
+}
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/traffic/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/traffic/page.tsx
index 2d6bba8fe..85442c93f 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/traffic/page.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/traffic/page.tsx
@@ -45,6 +45,7 @@ import { ExportMenu, TableExport, useTrafficExport } from './_export';
import { SourcesStrip } from './_sources-strip';
import { SnippetBanner, SnippetDialog } from './_snippet-code';
import { GoogleTrendView } from './_google-trend';
+import { GoogleSection } from './_google-section';
import { NeedsTag } from './_source-gate';
import { googleSourceStatus, snippetDetected } from '@/lib/traffic-sources';
import { useUserRole } from '@/hooks/use-user-role';
@@ -625,6 +626,22 @@ function TrafficPageContent({ brand }: { brand: Brand }) {
/>
);
+ // Google Search Console (AT-008, spec §3.6): pages whose impressions rose
+ // while their CTR fell. Search Console's own data, so it shows even when
+ // the snippet saw no visit in the range.
+ const googleSection = (
+ <GoogleSection
+ brandId={brand.id}
+ dateWindow={dateWindow}
+ rangeLabel={rangeSubLabel}
+ source={sourcesFailed ? null : (sources?.gsc ?? null)}
+ domain={primaryDomain}
+ canConnect={canManage}
+ onConnected={() => void loadSources()}
+ actions={<TableExport exporter={exporter} section="googlePages" />}
+ />
+ );
+
return (
<div className="space-y-6">
{/* Header */}
@@ -687,6 +704,7 @@ function TrafficPageContent({ brand }: { brand: Brand }) {
) : null}
{isEmpty && conversionsSection}
{isEmpty && pagesCard}
+ {isEmpty && googleSection}
{!isEmpty && (
<>
{/* KPI cards: two rows of four (AT-017, spec §3.2). */}
@@ -864,6 +882,8 @@ function TrafficPageContent({ brand }: { brand: Brand }) {
{pagesCard}
+ {googleSection}
+
{/* Recent Visit Log with filtering and pagination*/}
<Card>
<CardHeader className="pb-3">
diff --git a/web/src/app/api/traffic/export/route.test.ts b/web/src/app/api/traffic/export/route.test.ts
index eb896a99a..5878a843b 100644
--- a/web/src/app/api/traffic/export/route.test.ts
+++ b/web/src/app/api/traffic/export/route.test.ts
@@ -33,6 +33,7 @@ const loaders = vi.hoisted(() => ({
getTrafficSources: vi.fn(),
getTrafficPages: vi.fn(),
getTrafficConversions: vi.fn(),
+ getTrafficGooglePages: vi.fn(),
}));
vi.mock('@/lib/actions/traffic', () => loaders);
@@ -100,6 +101,16 @@ beforeEach(() => {
counts: { noAiTraffic: 0, citedNotClicked: 0, trafficNoPrompt: 0, all: 0 },
rows: [],
});
+ loaders.getTrafficGooglePages.mockResolvedValue({
+ lastDay: '2026-10-17',
+ since: '2026-10-11',
+ until: '2026-10-17',
+ prevSince: '2026-10-04',
+ prevUntil: '2026-10-10',
+ incomplete: false,
+ count: 0,
+ rows: [],
+ });
});
const WINDOW = { dateFrom: '2026-10-11T09:00:00.000Z' };
@@ -136,6 +147,7 @@ describe('GET /api/traffic/export', () => {
'Top converting pages',
'Answers that bring buyers',
'Pages by status',
+ 'Is AI taking Google clicks',
'Visits',
]);
@@ -172,6 +184,7 @@ describe('GET /api/traffic/export', () => {
'top-converting-pages.csv',
'answers-that-bring-buyers.csv',
'pages-by-status.csv',
+ 'is-ai-taking-google-clicks.csv',
'visits.csv',
]);
for (const file of Object.values(files)) expect(startsWithBom(file)).toBe(true);
diff --git a/web/src/app/api/traffic/export/route.ts b/web/src/app/api/traffic/export/route.ts
index 8164a25ff..5f62a01e9 100644
--- a/web/src/app/api/traffic/export/route.ts
+++ b/web/src/app/api/traffic/export/route.ts
@@ -6,6 +6,7 @@ import messages from '../../../../../messages/en.json';
import { createClient } from '@/lib/supabase/server';
import {
getTrafficConversions,
+ getTrafficGooglePages,
getTrafficPages,
getTrafficSources,
getTrafficSummary,
@@ -91,30 +92,35 @@ export async function GET(req: Request) {
const wholePage = !q.section;
try {
- const [summary, trend, visits, sources, pageStatus, conversions] = await Promise.all([
- needs('kpis', 'platforms', 'pages')
- ? getTrafficSummary(q.brandId, window, { topPages: Number.MAX_SAFE_INTEGER })
- : undefined,
- needs('trend') ? getTrafficTrend(q.brandId, window) : undefined,
- needs('visits')
- ? getTrafficVisitsForExport(q.brandId, {
- ...window,
- platform: q.platform,
- search: q.search,
- max: TRAFFIC_EXPORT_MAX_ROWS,
- })
- : undefined,
- // For the Filters sheet.
- wholePage ? getTrafficSources(q.brandId).catch(() => null) : undefined,
- // Every page of the Pages section, with its lists (AT-006).
- needs('pageStatus')
- ? getTrafficPages(q.brandId, window, { tab: 'all', limit: TRAFFIC_EXPORT_MAX_ROWS })
- : undefined,
- // Every row of where AI visitors convert, over the window the page reads (AT-014).
- needs('convertingPages', 'buyerAnswers')
- ? getTrafficConversions(q.brandId, window, { limit: TRAFFIC_EXPORT_MAX_ROWS })
- : undefined,
- ]);
+ const [summary, trend, visits, sources, pageStatus, conversions, googlePages] =
+ await Promise.all([
+ needs('kpis', 'platforms', 'pages')
+ ? getTrafficSummary(q.brandId, window, { topPages: Number.MAX_SAFE_INTEGER })
+ : undefined,
+ needs('trend') ? getTrafficTrend(q.brandId, window) : undefined,
+ needs('visits')
+ ? getTrafficVisitsForExport(q.brandId, {
+ ...window,
+ platform: q.platform,
+ search: q.search,
+ max: TRAFFIC_EXPORT_MAX_ROWS,
+ })
+ : undefined,
+ // For the Filters sheet.
+ wholePage ? getTrafficSources(q.brandId).catch(() => null) : undefined,
+ // Every page of the Pages section, with its lists (AT-006).
+ needs('pageStatus')
+ ? getTrafficPages(q.brandId, window, { tab: 'all', limit: TRAFFIC_EXPORT_MAX_ROWS })
+ : undefined,
+ // Every row of where AI visitors convert, over the window the page reads (AT-014).
+ needs('convertingPages', 'buyerAnswers')
+ ? getTrafficConversions(q.brandId, window, { limit: TRAFFIC_EXPORT_MAX_ROWS })
+ : undefined,
+ // Every page of Is AI taking Google clicks?, over the days it compares (AT-008).
+ needs('googlePages')
+ ? getTrafficGooglePages(q.brandId, window, { limit: TRAFFIC_EXPORT_MAX_ROWS })
+ : undefined,
+ ]);
// Larger than an export built on request should be (spec §3.9).
if (visits === null) return Response.json({ error: 'too_large' }, { status: 413 });
@@ -134,6 +140,7 @@ export async function GET(req: Request) {
visits: visits ?? undefined,
pageStatus,
conversions,
+ googlePages,
sources,
};
diff --git a/web/src/lib/actions/traffic-google-pages.test.ts b/web/src/lib/actions/traffic-google-pages.test.ts
new file mode 100644
index 000000000..729715630
--- /dev/null
+++ b/web/src/lib/actions/traffic-google-pages.test.ts
@@ -0,0 +1,125 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+/**
+ * AT-008 (spec §3.6): Is AI taking Google clicks? reads ai_traffic_google_pages
+ * (00181) over the range's whole days, ending on Google's latest reported day.
+ */
+
+const rpc = vi.fn();
+vi.mock('@/lib/supabase/server', () => ({
+ createClient: async () => ({ rpc }),
+}));
+vi.mock('@/config/api', () => ({ API_BASE_URL: 'http://api.test' }));
+
+import { getTrafficGooglePages } from './traffic';
+
+beforeEach(() => {
+ vi.useFakeTimers();
+ vi.setSystemTime(new Date('2026-10-19T09:30:00.000Z'));
+ rpc.mockReset();
+});
+
+afterEach(() => {
+ vi.useRealTimers();
+});
+
+describe('getTrafficGooglePages', () => {
+ it('asks for the range ending on the latest reported day, and reads the rows', async () => {
+ rpc.mockResolvedValue({
+ data: {
+ last_day: '2026-10-17',
+ since: '2026-10-11',
+ until: '2026-10-17',
+ prev_since: '2026-10-04',
+ prev_until: '2026-10-10',
+ count: 3,
+ rows: [
+ {
+ page: '/blog/how-to',
+ url: 'https://acme.example/blog/how-to',
+ impressions: '1420',
+ prev_impressions: 1000,
+ clicks: 14,
+ prev_clicks: 31,
+ prompt_id: 'p1',
+ },
+ {
+ page: '/faq',
+ url: 'https://acme.example/faq',
+ impressions: 300,
+ prev_impressions: 200,
+ clicks: 2,
+ prev_clicks: 8,
+ prompt_id: null,
+ },
+ ],
+ },
+ error: null,
+ });
+
+ const result = await getTrafficGooglePages('b1', {
+ dateFrom: '2026-10-12T09:30:00.000Z',
+ });
+ expect(rpc).toHaveBeenCalledWith('ai_traffic_google_pages', {
+ p_brand_id: 'b1',
+ p_since: '2026-10-13',
+ p_until: '2026-10-19',
+ p_latest: true,
+ p_min_impressions: 100,
+ p_limit: 10,
+ });
+ expect(result).toMatchObject({
+ lastDay: '2026-10-17',
+ since: '2026-10-11',
+ prevUntil: '2026-10-10',
+ incomplete: false,
+ count: 3,
+ });
+ expect(result.rows).toEqual([
+ {
+ page: '/blog/how-to',
+ url: 'https://acme.example/blog/how-to',
+ impressions: 1420,
+ prevImpressions: 1000,
+ clicks: 14,
+ prevClicks: 31,
+ promptId: 'p1',
+ },
+ {
+ page: '/faq',
+ url: 'https://acme.example/faq',
+ impressions: 300,
+ prevImpressions: 200,
+ clicks: 2,
+ prevClicks: 8,
+ promptId: null,
+ },
+ ]);
+ });
+
+ it('sends no first day for all time, and throws when the read fails', async () => {
+ rpc.mockResolvedValue({
+ data: { last_day: '2026-10-17', since: null, until: null, count: 0, rows: [] },
+ error: null,
+ });
+ const all = await getTrafficGooglePages('b1', undefined, { limit: 100_000 });
+ expect(rpc.mock.calls[0][1]).toMatchObject({
+ p_since: null,
+ p_until: '2026-10-19',
+ p_limit: 100_000,
+ });
+ expect(all).toMatchObject({ lastDay: '2026-10-17', since: null, rows: [] });
+
+ rpc.mockResolvedValue({
+ data: { last_day: '2026-10-17', since: '2026-07-01', incomplete: true, count: 0, rows: [] },
+ error: null,
+ });
+ await expect(getTrafficGooglePages('b1', undefined)).resolves.toMatchObject({
+ incomplete: true,
+ rows: [],
+ });
+
+ rpc.mockResolvedValue({ data: null, error: { message: 'boom' } });
+ await expect(getTrafficGooglePages('b1', undefined)).rejects.toThrow('boom');
+ });
+});
diff --git a/web/src/lib/actions/traffic.ts b/web/src/lib/actions/traffic.ts
index 8b93418de..9e92a5248 100644
--- a/web/src/lib/actions/traffic.ts
+++ b/web/src/lib/actions/traffic.ts
@@ -2,6 +2,7 @@
import { createClient } from '@/lib/supabase/server';
import { API_BASE_URL } from '@/config/api';
+import { GOOGLE_PAGES_MIN_IMPRESSIONS, googlePagesWindow } from '@/lib/traffic-google-pages';
import { TRAFFIC_PAGE_SIZE, countTrafficLogs, scanTrafficLogs } from '@/lib/traffic-scan';
import { expandDateToEndOfDay, utcDay } from '@/lib/dates';
import { isHourlyWindow, listUtcHours, utcHourKey } from '@/lib/traffic-buckets';
@@ -964,3 +965,81 @@ export async function getTrafficGoogleTrend(
})),
};
}
+
+/** A page whose Google impressions rose while its CTR fell (AT-008). */
+export interface GooglePageRow {
+ /** The page's path, as the Pages section names it. */
+ page: string;
+ /** One full URL Search Console reported for it. */
+ url: string;
+ impressions: number;
+ prevImpressions: number;
+ clicks: number;
+ prevClicks: number;
+ /** A tracked prompt about the page, for its Positions panel; null with none. */
+ promptId: string | null;
+}
+
+export interface TrafficGooglePages {
+ /** The latest day Google has reported for the brand's pages; null with no page data yet. */
+ lastDay: string | null;
+ /** The days compared, ending on Google's latest; null for all time. */
+ since: string | null;
+ until: string | null;
+ prevSince: string | null;
+ prevUntil: string | null;
+ /** The page history doesn't reach back to the period before: nothing is compared. */
+ incomplete: boolean;
+ /** Every page that qualifies; `rows` holds the first `limit`. */
+ count: number;
+ rows: GooglePageRow[];
+}
+
+/**
+ * "Is AI taking Google clicks?" (AT-008, spec §3.6; 00181): pages whose
+ * impressions rose 15% or more while their CTR fell 20% or more against the
+ * period before, most clicks lost first. The window keeps the range's length
+ * and ends on the latest day Google has reported (about two days late,
+ * §3.01), so a short range compares whole reported days.
+ */
+export async function getTrafficGooglePages(
+ brandId: string,
+ window: TrafficDateWindow | undefined,
+ { limit = 10 }: { limit?: number } = {},
+): Promise<TrafficGooglePages> {
+ const supabase = await createClient();
+ const { from, to } = resolveTrafficWindow(window);
+ const { since, until } = googlePagesWindow(from, to);
+
+ const { data, error } = await supabase.rpc('ai_traffic_google_pages', {
+ p_brand_id: brandId,
+ p_since: since,
+ p_until: until,
+ p_latest: true,
+ p_min_impressions: GOOGLE_PAGES_MIN_IMPRESSIONS,
+ p_limit: limit,
+ });
+ if (error) throw new Error(error.message);
+ const raw = (data ?? {}) as Record<string, unknown>;
+ const n = (v: unknown) => Number(v) || 0;
+ const day = (v: unknown) => (typeof v === 'string' ? v : null);
+ const list = Array.isArray(raw.rows) ? (raw.rows as Record<string, unknown>[]) : [];
+ return {
+ lastDay: day(raw.last_day),
+ since: day(raw.since),
+ until: day(raw.until),
+ prevSince: day(raw.prev_since),
+ prevUntil: day(raw.prev_until),
+ incomplete: raw.incomplete === true,
+ count: n(raw.count),
+ rows: list.map((r) => ({
+ page: String(r.page),
+ url: String(r.url),
+ impressions: n(r.impressions),
+ prevImpressions: n(r.prev_impressions),
+ clicks: n(r.clicks),
+ prevClicks: n(r.prev_clicks),
+ promptId: typeof r.prompt_id === 'string' ? r.prompt_id : null,
+ })),
+ };
+}
diff --git a/web/src/lib/traffic-export.test.ts b/web/src/lib/traffic-export.test.ts
index 3eb19751a..0c61076ed 100644
--- a/web/src/lib/traffic-export.test.ts
+++ b/web/src/lib/traffic-export.test.ts
@@ -356,6 +356,82 @@ describe('where AI visitors convert (AT-014)', () => {
});
});
+describe('trafficTable: Is AI taking Google clicks (AT-008)', () => {
+ const googlePages = (
+ over: Partial<NonNullable<TrafficExportData['googlePages']>> = {},
+ ): NonNullable<TrafficExportData['googlePages']> => ({
+ lastDay: '2026-10-17',
+ since: '2026-10-11',
+ until: '2026-10-17',
+ prevSince: '2026-10-04',
+ prevUntil: '2026-10-10',
+ incomplete: false,
+ count: 1,
+ rows: [
+ {
+ page: '/blog/how-to',
+ url: 'https://acme.example/blog/how-to',
+ impressions: 1420,
+ prevImpressions: 1000,
+ clicks: 14,
+ prevClicks: 31,
+ promptId: 'p1',
+ },
+ ],
+ ...over,
+ });
+
+ it('holds both periods, the rise, the CTRs and the days compared', () => {
+ const table = trafficTable('googlePages', data({ googlePages: googlePages() }), label);
+ expect(table.headers).toEqual([
+ 'Page',
+ 'URL',
+ 'Impressions',
+ 'Impressions, previous period',
+ 'Change (%)',
+ 'CTR (%)',
+ 'CTR, previous period (%)',
+ 'Clicks',
+ 'Clicks, previous period',
+ 'Days',
+ 'Days, previous period',
+ ]);
+ expect(table.rows).toEqual([
+ [
+ '/blog/how-to',
+ 'https://acme.example/blog/how-to',
+ 1420,
+ 1000,
+ 42,
+ 1,
+ 3.1,
+ 14,
+ 31,
+ '2026-10-11 – 2026-10-17',
+ '2026-10-04 – 2026-10-10',
+ ],
+ ]);
+ });
+
+ it('says why there are no rows, never an empty sheet', () => {
+ const rows = (g: TrafficExportData['googlePages']) =>
+ trafficTable('googlePages', data({ googlePages: g }), label).rows;
+ expect(rows(googlePages({ rows: [], count: 0 }))).toEqual([
+ ["No page's impressions rose 15% while its CTR fell 20% in this range"],
+ ]);
+ expect(rows(googlePages({ lastDay: null, since: null, rows: [] }))).toEqual([
+ ['No page data from Google Search Console yet'],
+ ]);
+ expect(rows(null)).toEqual([['No page data from Google Search Console yet']]);
+ expect(rows(googlePages({ since: null, rows: [] }))).toEqual([
+ ['No previous period for all time'],
+ ]);
+ expect(rows(googlePages({ incomplete: true, rows: [] }))).toEqual([
+ ["Google Search Console's page history doesn't reach back to the period before this range"],
+ ]);
+ });
+});
+
describe('filtersTable', () => {
it('records the range, the filters, the sources and when', () => {
const table = filtersTable(
@@ -389,6 +465,7 @@ describe('trafficWorkbook', () => {
'Top converting pages',
'Answers that bring buyers',
'Pages by status',
+ 'Is AI taking Google clicks',
'Visits',
]);
// A bold header row, then the rows with their numbers as numbers.
diff --git a/web/src/lib/traffic-export.ts b/web/src/lib/traffic-export.ts
index 2ce975889..a6f83d855 100644
--- a/web/src/lib/traffic-export.ts
+++ b/web/src/lib/traffic-export.ts
@@ -11,6 +11,7 @@
import type { Sheet } from 'write-excel-file/node';
import type {
TrafficConversions,
+ TrafficGooglePages,
TrafficLog,
TrafficPages,
TrafficSources,
@@ -21,6 +22,7 @@ import { csvLine } from '@/lib/csv';
import { getPlatformName } from '@/lib/traffic-platforms';
import { pageStatuses } from '@/lib/traffic-pages';
import { conversionRate } from '@/lib/traffic-conversions';
+import { ctrPct, impressionsRise } from '@/lib/traffic-google-pages';
import { visitChange } from '@/lib/traffic-change';
import {
googleSourceStatus,
@@ -37,6 +39,7 @@ export const TRAFFIC_EXPORT_SECTIONS = [
'convertingPages',
'buyerAnswers',
'pageStatus',
+ 'googlePages',
'visits',
] as const;
@@ -49,6 +52,7 @@ export const TRAFFIC_TABLE_SECTIONS = [
'convertingPages',
'buyerAnswers',
'pageStatus',
+ 'googlePages',
'visits',
] as const;
@@ -75,6 +79,8 @@ export interface TrafficExportData {
pageStatus?: TrafficPages | null;
/** Where AI visitors convert, every row (AT-014). */
conversions?: TrafficConversions | null;
+ /** Is AI taking Google clicks?, every page (AT-008). */
+ googlePages?: TrafficGooglePages | null;
sources?: TrafficSources | null;
}
@@ -325,6 +331,50 @@ function buyerAnswers(data: TrafficExportData, label: ExportLabel): ExportTable
}));
}
+/**
+ * Is AI taking Google clicks? (AT-008): every page that qualifies, with both
+ * periods' figures and the days compared, since they end on Google's latest
+ * reported day rather than the range's last.
+ */
+function googlePages(data: TrafficExportData, label: ExportLabel): ExportTable {
+ const g = data.googlePages;
+ const note = (key: string) => ({ headers: [label('columns.note')], rows: [[label(key)]] });
+ if (!g || !g.lastDay) return note('notes.noGooglePages');
+ if (!g.since) return note('notes.noPreviousPeriod');
+ if (g.incomplete) return note('notes.googleHistoryShort');
+ if (g.rows.length === 0) return note('notes.noGoogleClicksLost');
+ const days = `${g.since} – ${g.until}`;
+ const prevDays = `${g.prevSince} – ${g.prevUntil}`;
+ return {
+ headers: [
+ label('columns.page'),
+ label('columns.url'),
+ label('columns.impressions'),
+ label('columns.previousImpressions'),
+ label('columns.change'),
+ label('columns.ctr'),
+ label('columns.previousCtr'),
+ label('columns.clicks'),
+ label('columns.previousClicks'),
+ label('columns.days'),
+ label('columns.previousDays'),
+ ],
+ rows: g.rows.map((row) => [
+ row.page,
+ row.url,
+ row.impressions,
+ row.prevImpressions,
+ impressionsRise(row),
+ ctrPct(row.clicks, row.impressions),
+ ctrPct(row.prevClicks, row.prevImpressions),
+ row.clicks,
+ row.prevClicks,
+ days,
+ prevDays,
+ ]),
+ };
+}
+
function visits(data: TrafficExportData, label: ExportLabel): ExportTable {
return {
headers: [
@@ -369,6 +419,8 @@ export function trafficTable(
return buyerAnswers(data, label);
case 'pageStatus':
return pageStatus(data, label);
+ case 'googlePages':
+ return googlePages(data, label);
case 'visits':
return visits(data, label);
}
diff --git a/web/src/lib/traffic-google-pages.test.ts b/web/src/lib/traffic-google-pages.test.ts
new file mode 100644
index 000000000..49cd2b17d
--- /dev/null
+++ b/web/src/lib/traffic-google-pages.test.ts
@@ -0,0 +1,43 @@
+import { describe, expect, it } from 'vitest';
+import { ctrPct, googlePagesWindow, impressionsRise } from './traffic-google-pages';
+
+/** Is AI taking Google clicks? (AT-008, spec §3.6): the days compared and the figures. */
+
+const NOW = Date.parse('2026-10-19T09:30:00.000Z');
+
+describe('googlePagesWindow', () => {
+ it('keeps a rolling range as whole days ending on its last', () => {
+ expect(googlePagesWindow('2026-10-12T09:30:00.000Z', '2026-10-19T09:30:00.000Z')).toEqual({
+ since: '2026-10-13',
+ until: '2026-10-19',
+ });
+ expect(googlePagesWindow('2026-10-18T09:30:00.000Z', undefined, NOW)).toEqual({
+ since: '2026-10-19',
+ until: '2026-10-19',
+ });
+ expect(googlePagesWindow('2026-09-19T09:30:00.000Z', undefined, NOW).since).toBe('2026-09-20');
+ });
+
+ it('reads a custom range as its days', () => {
+ expect(googlePagesWindow('2026-08-01', '2026-08-31T23:59:59.999Z')).toEqual({
+ since: '2026-08-01',
+ until: '2026-08-31',
+ });
+ });
+
+ it('has no first day for all time', () => {
+ expect(googlePagesWindow(undefined, undefined, NOW)).toEqual({
+ since: null,
+ until: '2026-10-19',
+ });
+ });
+});
+
+describe('impressionsRise and ctrPct', () => {
+ it('reads the rise in impressions and the CTR as the table shows them', () => {
+ expect(impressionsRise({ impressions: 1420, prevImpressions: 1000 })).toBe(42);
+ expect(ctrPct(31, 1000)).toBe(3.1);
+ expect(ctrPct(14, 1000)).toBe(1.4);
+ expect(ctrPct(3, 0)).toBeNull();
+ });
+});
diff --git a/web/src/lib/traffic-google-pages.ts b/web/src/lib/traffic-google-pages.ts
new file mode 100644
index 000000000..2a9f38d2a
--- /dev/null
+++ b/web/src/lib/traffic-google-pages.ts
@@ -0,0 +1,40 @@
+/**
+ * The rules behind "Is AI taking Google clicks?" (AT-008, spec §3.6), kept
+ * apart from the section so they're tested on their own.
+ */
+
+/** Impressions a page needs in the period before to be compared. */
+export const GOOGLE_PAGES_MIN_IMPRESSIONS = 100;
+
+const DAY_MS = 86_400_000;
+
+/**
+ * The days the section compares, before Google's delay moves them: the
+ * range's length in whole days, ending on its last day. The database ends
+ * them on the latest day Google has reported instead (about two days late,
+ * §3.01). All time has no first day, so nothing to compare.
+ */
+export function googlePagesWindow(
+ from: string | undefined,
+ to: string | undefined,
+ now = Date.now(),
+): { since: string | null; until: string } {
+ const end = to ? Date.parse(to) : now;
+ const until = new Date(end).toISOString().slice(0, 10);
+ if (!from) return { since: null, until };
+ const days = Math.max(1, Math.round((end - Date.parse(from)) / DAY_MS));
+ const since = new Date(Date.parse(`${until}T00:00:00Z`) - (days - 1) * DAY_MS)
+ .toISOString()
+ .slice(0, 10);
+ return { since, until };
+}
+
+/** The % rise in impressions: 42 for 1,000 → 1,420. */
+export function impressionsRise(row: { impressions: number; prevImpressions: number }): number {
+ return Math.round((row.impressions / row.prevImpressions - 1) * 100);
+}
+
+/** CTR as a percent with one decimal; null without impressions. */
+export function ctrPct(clicks: number, impressions: number): number | null {
+ return impressions > 0 ? Math.round((clicks / impressions) * 1000) / 10 : null;
+}
diff --git a/web/src/lib/traffic-sources.test.ts b/web/src/lib/traffic-sources.test.ts
index 0e2774b27..a07418dec 100644
--- a/web/src/lib/traffic-sources.test.ts
+++ b/web/src/lib/traffic-sources.test.ts
@@ -67,7 +67,11 @@ describe('sectionsNeeding', () => {
'conversions',
'pageValue',
]);
- expect(sectionsNeeding('google-search-console')).toEqual(['googleClicks']);
+ expect(sectionsNeeding('google-search-console')).toEqual([
+ 'googleClicks',
+ 'googleTrend',
+ 'googlePages',
+ ]);
});
});
diff --git a/web/src/lib/traffic-sources.ts b/web/src/lib/traffic-sources.ts
index 70b15ef07..53558c33e 100644
--- a/web/src/lib/traffic-sources.ts
+++ b/web/src/lib/traffic-sources.ts
@@ -84,8 +84,10 @@ export const TRAFFIC_SECTION_SOURCES = {
engagedVisits: 'google-analytics',
keyEvents: 'google-analytics',
googleClicks: 'google-search-console',
+ googleTrend: 'google-search-console',
conversions: 'google-analytics',
pageValue: 'google-analytics',
+ googlePages: 'google-search-console',
} as const satisfies Record<string, IntegrationProvider>;
export type TrafficSection = keyof typeof TRAFFIC_SECTION_SOURCES;
diff --git a/web/src/types/supabase.ts b/web/src/types/supabase.ts
index 78284f4ad..dea68b579 100644
--- a/web/src/types/supabase.ts
+++ b/web/src/types/supabase.ts
@@ -610,6 +610,7 @@ export type Database = {
created_at: string
description: string | null
ga_property_id: string | null
+ gsc_pages_backfilled_property: string | null
gsc_property: string | null
id: string
industry: string | null
@@ -631,6 +632,7 @@ export type Database = {
created_at?: string
description?: string | null
ga_property_id?: string | null
+ gsc_pages_backfilled_property?: string | null
gsc_property?: string | null
id?: string
industry?: string | null
@@ -652,6 +654,7 @@ export type Database = {
created_at?: string
description?: string | null
ga_property_id?: string | null
+ gsc_pages_backfilled_property?: string | null
gsc_property?: string | null
id?: string
industry?: string | null
@@ -1095,6 +1098,53 @@ export type Database = {
},
]
}
+ gsc_page_stats: {
+ Row: {
+ brand_id: string
+ clicks: number
+ created_at: string
+ ctr: number
+ date: string
+ id: string
+ impressions: number
+ page: string
+ page_key: string | null
+ position: number
+ }
+ Insert: {
+ brand_id: string
+ clicks?: number
+ created_at?: string
+ ctr?: number
+ date: string
+ id?: string
+ impressions?: number
+ page: string
+ page_key?: never
+ position?: number
+ }
+ Update: {
+ brand_id?: string
+ clicks?: number
+ created_at?: string
+ ctr?: number
+ date?: string
+ id?: string
+ impressions?: number
+ page?: string
+ page_key?: never
+ position?: number
+ }
+ Relationships: [
+ {
+ foreignKeyName: "gsc_page_stats_brand_id_fkey"
+ columns: ["brand_id"]
+ isOneToOne: false
+ referencedRelation: "brands"
+ referencedColumns: ["id"]
+ },
+ ]
+ }
gsc_query_stats: {
Row: {
brand_id: string
@@ -2870,6 +2920,17 @@ export type Database = {
}
Returns: Json
}
+ ai_traffic_google_pages: {
+ Args: {
+ p_brand_id: string
+ p_latest?: boolean
+ p_limit?: number
+ p_min_impressions?: number
+ p_since: string | null
+ p_until: string
+ }
+ Returns: Json
+ }
ai_traffic_google_trend: {
Args: {
p_brand_id: string