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.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
feat(traffic): list AI visits that landed on a missing page, with a redirect to suggest (#1192)
Cihanbfb1baeca012
diff --git a/server/src/lib/tracking-script.js b/server/src/lib/tracking-script.jsindex 505bf3599..9c9e21e20 100644--- a/server/src/lib/tracking-script.js+++ b/server/src/lib/tracking-script.js@@ -10,6 +10,11 @@* chatgpt.com are stored as one engine, and the list's version, stored with* the visit.*+ * It also sends the landing page's HTTP status as the browser saw it, for+ * Broken AI links (AT-012): st, from the navigation entry, or 0 where the+ * browser doesn't report one (Safari); then nf is 1 when the page's title+ * reads "404" or "not found".+ ** Plain ES5 apart from URL / URLSearchParams, as before: it runs on the* customer's pages, in whatever browser their visitors have.*/@@ -49,7 +54,10 @@ if(!pf){try{pf=m(new URLSearchParams(location.search).get("utm_source"),true)}caif(!pf)return;window.__at=1;var c=document.currentScript,k=c.getAttribute("data-t"),o=c.src.replace(/\\/t\\.js(\\?.*)?$/,"");-var b=JSON.stringify({u:location.href,r:r,s:pf,v:V,a:navigator.userAgent,t:Date.now(),l:navigator.language,d:screen.width+"x"+screen.height});+var st=0,nf=0;+try{var n=performance.getEntriesByType("navigation")[0];st=n&&n.responseStatus||0}catch(e){}+if(!st&&/(^|[^0-9])404([^0-9]|$)|not found/i.test(document.title||""))nf=1;+var b=JSON.stringify({u:location.href,r:r,s:pf,v:V,a:navigator.userAgent,t:Date.now(),l:navigator.language,d:screen.width+"x"+screen.height,st:st,nf:nf});var x=o+"/track/"+k;if(navigator.sendBeacon)navigator.sendBeacon(x,new Blob([b],{type:"text/plain;charset=UTF-8"}));else fetch(x,{method:"POST",body:b,headers:{"Content-Type":"text/plain;charset=UTF-8"},keepalive:!0}).catch(function(){});diff --git a/server/src/lib/tracking-script.test.js b/server/src/lib/tracking-script.test.jsindex a20a29620..9407254d4 100644--- a/server/src/lib/tracking-script.test.js+++ b/server/src/lib/tracking-script.test.js@@ -12,12 +12,23 @@ import { buildTrackingScript } from './tracking-script.js';const SCRIPT = buildTrackingScript();/** Runs t.js on a page; returns the beacon it sent, or null. */-function visit({ referrer = '', search = '', alreadyTracked = false } = {}) {+function visit({+ referrer = '',+ search = '',+ alreadyTracked = false,+ title = 'Menu · Acme Coffee',+ navigation,+} = {}) {const sent = [];const window = alreadyTracked ? { __at: 1 } : {};runInNewContext(SCRIPT, {window,+ // A browser without the navigation entry (older Safari) has none.+ ...(navigation === undefined+ ? {}+ : { performance: { getEntriesByType: () => (navigation ? [navigation] : []) } }),document: {+ title,referrer,currentScript: {src: 'https://api.ansvisor.test/t.js',@@ -85,6 +96,32 @@ describe('the tracking snippet', () => {expect(visit({ referrer: 'https://chatgpt.com/', alreadyTracked: true })).toBeNull();});+ it("sends the landing page's status as the browser saw it (AT-012)", () => {+ const from = { referrer: 'https://chatgpt.com/' };+ expect(visit({ ...from, navigation: { responseStatus: 404 } })).toMatchObject({+ st: 404,+ nf: 0,+ });+ // A status says it all, whatever the title.+ expect(+ visit({ ...from, navigation: { responseStatus: 200 }, title: 'How we fixed our 404s' }),+ ).toMatchObject({ st: 200, nf: 0 });+ });++ it('says whether the title reads "not found" when the browser gives no status', () => {+ const from = { referrer: 'https://chatgpt.com/' };+ // Safari: no responseStatus on the entry, or no entry at all.+ expect(visit({ ...from, navigation: {}, title: 'Page Not Found – Acme' })).toMatchObject({+ st: 0,+ nf: 1,+ });+ expect(visit({ ...from, title: '404 | Acme Coffee' })).toMatchObject({ st: 0, nf: 1 });+ expect(visit({ ...from, navigation: null, title: 'Error 404' })).toMatchObject({ nf: 1 });+ // Not a 404: a number that contains 404, or an ordinary page.+ expect(visit({ ...from, title: 'Order #14045 shipped' })).toMatchObject({ st: 0, nf: 0 });+ expect(visit({ ...from, title: 'Menu · Acme Coffee' })).toMatchObject({ st: 0, nf: 0 });+ });+it('names every listed host and alias as the server does', () => {for (const source of AI_SOURCES) {for (const host of source.hosts) {diff --git a/server/src/routes/traffic-track.test.js b/server/src/routes/traffic-track.test.jsindex 5fdd2f175..beba274de 100644--- a/server/src/routes/traffic-track.test.js+++ b/server/src/routes/traffic-track.test.js@@ -4,7 +4,8 @@ import { AI_SOURCES_VERSION } from '../lib/ai-sources.js';/*** POST /track/:trackingCode (AT-003): each pageview is stored with the visit- * it belongs to, and a failed visit lookup still stores the pageview.+ * it belongs to, and a failed visit lookup still stores the pageview; with+ * the landing page's status, for Broken AI links (AT-012).*/const inserted = [];@@ -136,6 +137,37 @@ describe('POST /track/:trackingCode', () => {expect(inserted[0].row.source_platform).toBe('some-new-assistant.example');});+ it("stores the landing page's status, or whether its title said not found (AT-012)", async () => {+ findVisitOf.mockResolvedValue(null);+ await beacon({ st: 404, nf: 0 });+ // Safari: no status, and the title said "not found".+ await beacon({ st: 0, nf: 1 });+ // A status wins over the title.+ await beacon({ st: 200, nf: 1 });+ // An older snippet sends neither.+ await beacon();+ expect(inserted.map((i) => [i.row.http_status, i.row.not_found_title])).toEqual([+ [404, false],+ [null, true],+ [200, false],+ [null, false],+ ]);+ });++ it('ignores a status no response could have', async () => {+ findVisitOf.mockResolvedValue(null);+ await beacon({ st: 99 });+ await beacon({ st: 600 });+ await beacon({ st: '404' });+ await beacon({ st: 404.5, nf: 'yes' });+ expect(inserted.map((i) => [i.row.http_status, i.row.not_found_title])).toEqual([+ [null, false],+ [null, false],+ [null, false],+ [null, false],+ ]);+ });+it('logs a write the database refuses', async () => {const { default: logger } = await import('../lib/logger.js');findVisitOf.mockResolvedValue(null);diff --git a/server/src/routes/traffic.js b/server/src/routes/traffic.jsindex ad7918eec..7401987f0 100644--- a/server/src/routes/traffic.js+++ b/server/src/routes/traffic.js@@ -125,6 +125,13 @@ router.post('/track/:trackingCode', trafficLimiter, beaconBody, async (req, res)const sourceListVersion =Number.isInteger(body.v) && body.v >= 1 && body.v <= AI_SOURCES_VERSION ? body.v : null;+ // The landing page's HTTP status as the browser saw it, for Broken AI+ // links (AT-012). Without one (Safari, older snippets), whether the+ // page's title said "not found".+ const httpStatus =+ Number.isInteger(body.st) && body.st >= 100 && body.st <= 599 ? body.st : null;+ const notFoundTitle = httpStatus === null && body.nf === 1;+const userAgent = (body.a || '').slice(0, 512) || null;const ipAddress = ip || null;@@ -150,6 +157,8 @@ router.post('/track/:trackingCode', trafficLimiter, beaconBody, async (req, res)screen: (body.d || '').slice(0, 16) || null,visit_of: visitOf,source_list_version: sourceListVersion,+ http_status: httpStatus,+ not_found_title: notFoundTitle,});// The beacon can't be retried, so a failed write is at least visible.if (insertError) logger.error({ err: insertError }, 'traffic track insert failed');diff --git a/supabase/migrations/00186_ai_traffic_broken_links.sql b/supabase/migrations/00186_ai_traffic_broken_links.sqlnew file mode 100644index 000000000..336e49156--- /dev/null+++ b/supabase/migrations/00186_ai_traffic_broken_links.sql@@ -0,0 +1,622 @@+-- ============================================================================+-- 00186: broken AI links (AT-012)+-- ============================================================================+-- Spec §3.5: "AI-referred visits that landed on a 404 → suggested redirect,+-- exportable as a redirect list." Snippet only (§3.8b).+--+-- The snippet now sends the landing page's HTTP status as the browser saw it+-- (the navigation entry's responseStatus). Browsers that don't report one+-- (Safari) send none, and say instead whether the page's title read "404" or+-- "not found":+--+-- * http_status — the status, or null when the browser didn't tell;+-- * not_found_title — no status, and the title said "not found".+--+-- A page is broken in a range when AI visitors found it missing in the range+-- and its latest pageview with a status, up to now, still says 404 or 410.+-- With no status at all, a title saying "not found" counts unless the nightly+-- page read (site_pages, 00104) found the page working: a post about fixing+-- 404s is not a missing page. A page the nightly read found working after its+-- last miss is fixed, and isn't listed. The status is decided per address+-- (the path plus its query, utm_* and click ids such as gclid left out), and+-- a path is broken only when none of its addresses last answered 2xx or 3xx:+-- on a shop where /index.php?route=product&id=42 is gone and the other+-- products work, /index.php isn't broken. When every missing address of a+-- page carries a query (a route on it, not the page), no redirect is+-- suggested: one from the path would move the whole route. Its visits are+-- the pageviews that found it missing (404, 410, or no status and a+-- not-found title), not those from before it broke.+--+-- Each other broken page gets a suggested redirect to a page known to work+-- (read 2xx by the nightly page read, or its address without a query landed+-- on with a 2xx in the last 90 days), the first that fits of:+--+-- * similar — the most path words in common, at least half of both pages'+-- words together, a plural read as its singular+-- (/blog/best-tools-2025 → /blog/best-tools-2026);+-- * parent — the nearest working page above it (/guides/old → /guides);+-- * home — the home page.+--+-- The Pages section's Traffic, no prompt list leaves broken pages out: their+-- next step is a redirect, not prompts. ai_traffic_pages is redefined from+-- 00178 for that, and its rows say whether a page is broken.+--+-- Pages are keyed as the Pages section keys them (00178). citation_urls has+-- no member select policy, so both reports run as their owner after+-- checking the caller belongs to the brand's organization; a caller from+-- elsewhere gets nothing. The rule itself (ai_traffic_broken_pages) has no+-- check of its own, so members can't call it.++-- Beacon inserts don't queue behind the column change for long: it gives up+-- after 3 s instead (run it again).+set lock_timeout = '3s';++alter table public.ai_traffic_logs+ add column if not exists http_status smallint,+ add column if not exists not_found_title boolean not null default false;++reset lock_timeout;++comment on column public.ai_traffic_logs.http_status is+ 'The landing page''s HTTP status as the visitor''s browser saw it; null when the browser doesn''t report one (Safari) or from an older snippet (AT-012).';+comment on column public.ai_traffic_logs.not_found_title is+ 'True when the browser reported no status and the page''s title read "404" or "not found" (AT-012).';++-- ─── Which pages are broken ─────────────────────────────────────────────────+-- The rule above, for both the Broken AI links tab and the Pages section's+-- other lists, which leave these pages out (a missing page needs a redirect,+-- not prompts). No organization check of its own: only the two functions+-- below call it, after theirs.++create or replace function public.ai_traffic_broken_pages(+ p_brand_id uuid,+ p_from timestamptz,+ p_to timestamptz+)+returns table (page_key text, status text, routed boolean)+language sql+stable+set search_path to 'public'+as $$+ with+ -- Only the pageviews that say something about the page: a status, or a+ -- not-found title. Read up to now, so a page that works again since the+ -- window ended isn't listed.+ logs as (+ select+ n.page_key,+ -- The address: the path plus its query, tracking parameters left out,+ -- in one order. Worked out only when the URL has a query.+ case when q.query is null then n.page_key else n.page_key || '?' || q.query end as address,+ l.created_at,+ l.http_status,+ l.not_found_title,+ (p_to is null or l.created_at <= p_to)+ and (l.http_status in (404, 410) or (l.http_status is null and l.not_found_title))+ as failed_in_window+ from public.ai_traffic_logs l+ cross join lateral (select lower(public.normalize_page_path(l.url)) as page_key) n+ cross join lateral (+ select case when strpos(l.url, '?') > 0 then (+ select string_agg(qp.part, '&' order by qp.part)+ from unnest(string_to_array(split_part(split_part(l.url, '#', 1), '?', 2), '&')) as qp(part)+ where qp.part <> ''+ and qp.part !~* '^(utm_[^=]*|gclid|fbclid|msclkid|srsltid|_gl|ref)(=|$)'+ ) end as query+ ) q+ where l.brand_id = p_brand_id+ and l.created_at >= coalesce(p_from, '-infinity'::timestamptz)+ and (l.http_status is not null or l.not_found_title)+ ),+ address_state as (+ select+ page_key,+ address,+ (array_agg(http_status order by created_at desc) filter (where http_status is not null))[1]+ as last_status,+ bool_or(not_found_title) as title_not_found,+ max(created_at) filter (where failed_in_window) as last_failed_at+ from logs+ group by page_key, address+ ),+ page_state as (+ select+ page_key,+ max(last_status) filter (where last_status in (404, 410)) as last_status,+ bool_or(last_status is null and title_not_found) as title_not_found,+ -- No address with a status reads as not working, not as unknown.+ coalesce(bool_or(last_status between 200 and 399), false) as works,+ max(last_failed_at) as last_failed_at,+ -- Every missing address carries a query: a route on the page, not the page.+ bool_and(address <> page_key)+ filter (where last_status in (404, 410) or (last_status is null and title_not_found))+ as routed+ from address_state+ group by page_key+ ),+ -- What the nightly page read found (00104).+ fetched as (+ select lower(public.normalize_page_path(sp.url)) as page_key,+ bool_or(sp.fetch_status between 200 and 399) as works,+ max(sp.fetched_at) filter (where sp.fetch_status between 200 and 299) as last_ok_at+ from public.site_pages sp+ where sp.brand_id = p_brand_id+ and sp.fetch_status is not null+ group by 1+ )+ select+ s.page_key,+ case when s.last_status is not null then s.last_status::text else 'title' end,+ coalesce(s.routed, false)+ from page_state s+ left join fetched f on f.page_key = s.page_key+ where not s.works+ and s.last_failed_at is not null+ -- The nightly read found it working after its last miss: fixed since.+ and not coalesce(f.last_ok_at > s.last_failed_at, false)+ and (+ s.last_status in (404, 410)+ or (s.last_status is null and s.title_not_found and not coalesce(f.works, false))+ );+$$;++revoke all on function public.ai_traffic_broken_pages(uuid, timestamptz, timestamptz)+ from public, anon, authenticated;+grant execute on function public.ai_traffic_broken_pages(uuid, timestamptz, timestamptz)+ to service_role;++comment on function public.ai_traffic_broken_pages(uuid, timestamptz, timestamptz) is+ 'AI Traffic (AT-012): the brand''s pages AI visitors found missing in a window whose latest reported status up to now is 404 or 410, or, with no status, whose title said not found and that the nightly page read never found working; not those the nightly read found working after their last miss. Decided per address (path plus query, utm_* and click ids left out), and a path none of whose addresses last answered 2xx or 3xx; routed when every missing address carries a query. No organization check: called only by ai_traffic_broken_links and ai_traffic_pages, after theirs.';++-- ─── Broken AI links ────────────────────────────────────────────────────────++create or replace function public.ai_traffic_broken_links(+ p_brand_id uuid,+ p_from timestamptz,+ p_to timestamptz,+ p_limit integer default 50,+ p_offset integer default 0+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+as $$+declare+ v_since date := (p_from at time zone 'utc')::date;+ v_until date := coalesce((p_to at time zone 'utc')::date, (now() at time zone 'utc')::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;++ with+ logs as (+ select+ lower(n.page) as page_key,+ n.page,+ coalesce(l.visit_of, l.id) as visit,+ l.created_at,+ coalesce(nullif(l.source_platform, ''), 'unknown') as platform+ from public.ai_traffic_logs l+ cross join lateral (select public.normalize_page_path(l.url) as page) n+ where l.brand_id = p_brand_id+ and l.created_at >= coalesce(p_from, '-infinity'::timestamptz)+ and (p_to is null or l.created_at <= p_to)+ -- Only the pageviews that found the page missing, not those from before it broke.+ and (l.http_status in (404, 410) or (l.http_status is null and l.not_found_title))+ ),+ broken as (+ select bp.page_key, bp.status, bp.routed+ from public.ai_traffic_broken_pages(p_brand_id, p_from, p_to) bp+ ),+ visits as (+ select l.page_key, min(l.page) as page, count(distinct l.visit)::bigint as visits,+ max(l.created_at) as last_visit+ from logs l+ join broken b on b.page_key = l.page_key+ group by l.page_key+ ),+ platforms as (+ select page_key, (array_agg(platform order by visits desc, platform))[1:3] as platforms+ from (+ select l.page_key, l.platform, count(distinct l.visit) as visits+ from logs l+ join broken b on b.page_key = l.page_key+ group by 1, 2+ ) t+ group by 1+ ),+ cited as (+ select c.page_key, c.citations+ from public.ai_traffic_page_citations(p_brand_id, v_since, v_until) c+ ),+ -- Pages known to work, to redirect to. Only a page's own address counts:+ -- /product.php?id=5 answering 200 says nothing about /product.php.+ landed as (+ select lower(n.page) as page_key,+ min(n.page) as page,+ (array_agg(l.http_status order by l.created_at desc))[1] as last_status+ from public.ai_traffic_logs l+ cross join lateral (select public.normalize_page_path(l.url) as page) n+ where l.brand_id = p_brand_id+ and l.http_status is not null+ and l.created_at >= now() - interval '90 days'+ and (+ strpos(l.url, '?') = 0+ or not exists (+ select 1+ from unnest(string_to_array(split_part(split_part(l.url, '#', 1), '?', 2), '&')) as qp(part)+ where qp.part <> ''+ and qp.part !~* '^(utm_[^=]*|gclid|fbclid|msclkid|srsltid|_gl|ref)(=|$)'+ )+ )+ group by 1+ ),+ candidates as (+ select page_key, min(page) as page, max(weight) as weight+ from (+ select lower(public.normalize_page_path(sp.url)) as page_key,+ public.normalize_page_path(sp.url) as page,+ sp.ga_sessions as weight+ from public.site_pages sp+ where sp.brand_id = p_brand_id+ and sp.fetch_status between 200 and 299+ union all+ select page_key, page, 0 from landed where last_status between 200 and 299+ ) c+ where page_key not in (select page_key from broken)+ group by page_key+ ),+ candidate_words as (+ select distinct c.page_key, regexp_replace(w.word, '^(.{3,})s$', '\1') as word+ from candidates c+ cross join lateral regexp_split_to_table(c.page_key, '[^[:alnum:]]+') as w(word)+ where length(w.word) >= 2+ ),+ broken_words as (+ select distinct b.page_key, regexp_replace(w.word, '^(.{3,})s$', '\1') as word+ from broken b+ cross join lateral regexp_split_to_table(b.page_key, '[^[:alnum:]]+') as w(word)+ where length(w.word) >= 2+ ),+ candidate_sizes as (select page_key, count(*) as words from candidate_words group by 1),+ broken_sizes as (select page_key, count(*) as words from broken_words group by 1),+ similar_pages as (+ select distinct on (page_key) page_key, target+ from (+ select+ bw.page_key,+ c.page as target,+ c.weight,+ count(*)::numeric / (bs.words + cs.words - count(*)) as overlap+ from broken_words bw+ join candidate_words cw on cw.word = bw.word+ join candidates c on c.page_key = cw.page_key+ join broken_sizes bs on bs.page_key = bw.page_key+ join candidate_sizes cs on cs.page_key = cw.page_key+ group by bw.page_key, c.page_key, c.page, c.weight, bs.words, cs.words+ ) m+ where overlap >= 0.5+ order by page_key, overlap desc, weight desc, length(target), target+ ),+ parent_pages as (+ select distinct on (b.page_key) b.page_key, c.page as target+ from broken b+ join candidates c+ on c.page_key <> '/'+ and left(b.page_key, length(c.page_key) + 1) = c.page_key || '/'+ order by b.page_key, length(c.page_key) desc+ ),+ result_rows as (+ select+ v.page,+ v.visits,+ v.last_visit,+ b.status,+ coalesce(p.platforms, '{}'::text[]) as platforms,+ coalesce(ci.citations, 0)::bigint as citations,+ -- A route on the page (every missing address has a query) gets no+ -- redirect: one from its path would move the whole route.+ case when v.page_key <> '/' and not b.routed then coalesce(s.target, pa.target, '/') end+ as target,+ case+ when v.page_key = '/' then null+ when b.routed then 'query'+ when s.target is not null then 'similar'+ when pa.target is not null then 'parent'+ else 'home'+ end as target_reason+ from visits v+ join broken b on b.page_key = v.page_key+ left join platforms p on p.page_key = v.page_key+ left join cited ci on ci.page_key = v.page_key+ left join similar_pages s on s.page_key = v.page_key+ left join parent_pages pa on pa.page_key = v.page_key+ )+ select jsonb_build_object(+ 'count', (select count(*) from result_rows),+ 'rows', coalesce((+ select jsonb_agg(to_jsonb(r))+ from (+ select * from result_rows+ order by visits desc, citations desc, last_visit desc, page+ limit greatest(p_limit, 0) offset greatest(p_offset, 0)+ ) r+ ), '[]'::jsonb)+ )+ into v_result;++ return v_result;+end;+$$;++revoke all on function public.ai_traffic_broken_links(uuid, timestamptz, timestamptz, integer, integer)+ from public, anon;+grant execute on function public.ai_traffic_broken_links(uuid, timestamptz, timestamptz, integer, integer)+ to authenticated, service_role;++comment on function public.ai_traffic_broken_links(uuid, timestamptz, timestamptz, integer, integer) is+ 'AI Traffic "Broken AI links" (AT-012, spec §3.5): the pages AI-referred visitors landed on that answered 404 or 410 (or, without a status, whose title said not found) in a window, with their visits, platforms, citations and a suggested redirect to a working page (similar path, parent page or home; none for a route on the page, target_reason query). Security definer with an organization check.';++-- ─── The Pages section leaves broken pages out of Traffic, no prompt ─────────+-- 00178's function with one change: a page AI visitors found missing is not+-- "Traffic, no prompt" (its next step is a redirect, on Broken AI links), and+-- each row says whether it is broken. Everything else is as 00178 wrote it.++create or replace function public.ai_traffic_pages(+ p_brand_id uuid,+ p_from timestamptz,+ p_to timestamptz,+ p_tab text default 'all',+ p_limit integer default 50,+ p_offset integer default 0+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+as $$+declare+ v_since date := (p_from at time zone 'utc')::date;+ v_until date := coalesce((p_to at time zone 'utc')::date, (now() at time zone 'utc')::date);+ -- The 24-hour range reads the snippet alone, as the rest of the page does:+ -- Analytics has no hours (00176).+ v_hourly boolean := p_from is not null and coalesce(p_to, now()) - p_from <= interval '25 hours';+ v_result jsonb;+begin+ if p_tab not in ('all', 'no_ai_traffic', 'cited_not_clicked', 'traffic_no_prompt') then+ raise exception 'unknown tab %', p_tab;+ end if;+ 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;++ with+ -- AI visits per page, day by day from the snippet or Analytics (00176).+ ai as (+ select lower(e->>'page') as page_key, min(e->>'page') as page, sum((e->>'visits')::bigint) as visits+ from jsonb_array_elements(+ public.ai_traffic_merged(p_brand_id, p_from, p_to, true, not v_hourly) -> 'pages'+ ) e+ group by 1+ ),+ -- Days the snippet was active, over the whole UTC day (00176's rule).+ snippet_days as (+ select distinct (l.created_at at time zone 'utc')::date as day+ from public.ai_traffic_logs l+ where l.brand_id = p_brand_id+ and l.visit_of is null+ and l.created_at >= coalesce(v_since::timestamp at time zone 'utc', '-infinity'::timestamptz)+ and l.created_at < (v_until + 1)::timestamp at time zone 'utc'+ ),+ -- Which assistants sent them, counted as the AI visits are: the snippet,+ -- and Analytics on days without it (never within 24 hours).+ platform_visits as (+ select lower(public.normalize_page_path(l.url)) as page_key,+ coalesce(nullif(l.source_platform, ''), 'unknown') as platform,+ count(*)::bigint as visits+ from public.ai_traffic_logs l+ where l.brand_id = p_brand_id+ and l.visit_of is null+ and l.created_at >= coalesce(p_from, '-infinity'::timestamptz)+ and (p_to is null or l.created_at <= p_to)+ group by 1, 2+ union all+ select lower(public.normalize_page_path(g.landing_page)),+ coalesce(g.platform, g.source),+ sum(g.sessions)::bigint+ from public.ga_ai_traffic_stats g+ where not v_hourly+ and g.brand_id = p_brand_id+ and g.landing_page <> ''+ and g.sessions > 0+ and g.date >= coalesce(v_since, '-infinity'::date)+ and g.date <= v_until+ and not exists (select 1 from snippet_days s where s.day = g.date)+ group by 1, 2+ ),+ platforms as (+ select page_key, (array_agg(platform order by visits desc, platform))[1:3] as platforms+ from (select page_key, platform, sum(visits) as visits from platform_visits group by 1, 2) t+ group by 1+ ),+ -- Key events in AI sessions: only Analytics has them, and it has no hours.+ ai_key_events as (+ select lower(public.normalize_page_path(g.landing_page)) as page_key, sum(g.key_events)::bigint as key_events+ from public.ga_ai_traffic_stats g+ where not v_hourly+ and g.brand_id = p_brand_id+ and g.landing_page <> ''+ and g.date >= coalesce(v_since, '-infinity'::date)+ and g.date <= v_until+ group by 1+ ),+ -- Every channel's traffic, for the No AI traffic list's value.+ traffic as (+ select+ lower(public.normalize_page_path(gps.landing_page)) as page_key,+ min(public.normalize_page_path(gps.landing_page)) as page,+ sum(gps.sessions)::bigint as sessions,+ sum(gps.engaged_sessions)::bigint as engaged_sessions,+ sum(gps.key_events)::bigint as key_events,+ sum(gps.transactions)::bigint as transactions,+ sum(gps.purchase_revenue) as revenue,+ sum(gps.engagement_duration_seconds)::bigint as engagement_seconds+ from public.ga_page_stats gps+ where gps.brand_id = p_brand_id+ and gps.date >= coalesce(v_since, '-infinity'::date)+ and gps.date <= v_until+ group by 1+ ),+ cited as (+ select c.page_key, c.page, c.citations+ from public.ai_traffic_page_citations(p_brand_id, v_since, v_until) c+ ),+ -- Prompts pointing to the page: an active prompt's target URL.+ targeted as (+ select lower(public.normalize_page_path(ptu.url)) as page_key,+ count(distinct ptu.prompt_id)::bigint as prompts+ 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+ group by 1+ ),+ -- Pages that answered 404 or 410 to AI visitors (AT-012): Broken AI links.+ broken as (+ select bp.page_key from public.ai_traffic_broken_pages(p_brand_id, p_from, p_to) bp+ ),+ in_sitemap as (+ select distinct lower(public.normalize_page_path(sp.url)) as page_key,+ public.normalize_page_path(sp.url) as page+ from public.site_pages sp+ where sp.brand_id = p_brand_id+ and 'sitemap' = any(sp.sources)+ ),+ keys as (+ select page_key, page from ai+ union select page_key, page from traffic+ union select page_key, page from cited+ union select page_key, page from in_sitemap+ ),+ pages as (+ select+ k.page_key,+ min(k.page) as page,+ coalesce(max(ai.visits), 0) as ai_visits,+ coalesce(max(ke.key_events), 0) as ai_key_events,+ coalesce(max(pl.platforms), '{}'::text[]) as ai_platforms,+ coalesce(max(c.citations), 0) as citations,+ coalesce(max(t.prompts), 0) as targeting_prompts,+ coalesce(max(tr.sessions), 0) as sessions,+ coalesce(max(tr.engaged_sessions), 0) as engaged_sessions,+ coalesce(max(tr.key_events), 0) as key_events,+ coalesce(max(tr.transactions), 0) as transactions,+ coalesce(max(tr.revenue), 0) as revenue,+ coalesce(max(tr.engagement_seconds), 0) as engagement_seconds,+ bool_or(s.page_key is not null) as in_sitemap,+ bool_or(br.page_key is not null) as broken+ from keys k+ left join ai on ai.page_key = k.page_key+ left join ai_key_events ke on ke.page_key = k.page_key+ left join platforms pl on pl.page_key = k.page_key+ left join cited c on c.page_key = k.page_key+ left join targeted t on t.page_key = k.page_key+ left join traffic tr on tr.page_key = k.page_key+ left join in_sitemap s on s.page_key = k.page_key+ left join broken br on br.page_key = k.page_key+ group by k.page_key+ ),+ kept as (+ select p.*,+ -- The page's place on the site by value, among pages with traffic.+ case when p.sessions > 0 then+ rank() over (+ partition by (p.sessions > 0)+ order by p.revenue desc, p.key_events desc, p.sessions desc, p.engagement_seconds desc+ )+ end as value_rank,+ p.ai_visits = 0 and (p.sessions > 0 or p.in_sitemap) as no_ai_traffic,+ p.citations >= 20 and p.ai_visits = 0 as cited_not_clicked,+ -- A missing page needs a redirect, not prompts (AT-012).+ p.ai_visits > 0 and p.citations = 0 and not p.broken as traffic_no_prompt+ from pages p+ where p.page not in ('', '(not set)', '(other)')+ -- The nightly detection's excluded segments (page-paths.js).+ and not exists (+ select 1+ from unnest(string_to_array(lower(p.page), '/')) seg+ where seg = any (array[+ 'checkout', 'cart', 'basket', 'order', 'orders', 'order-confirmation',+ 'confirmation', 'thank-you', 'thanks', 'payment', 'pay', 'login', 'signin',+ 'sign-in', 'signup', 'sign-up', 'register', 'logout', 'account', 'my-account',+ 'profile', 'dashboard', 'admin', 'wishlist', 'favorites', 'search', 'career',+ 'careers', 'jobs', 'job', 'cookie-policy', 'privacy', 'privacy-policy', 'terms',+ 'terms-of-service', 'legal', 'unsubscribe'+ ])+ )+ ),+ tab as (+ select * from kept+ where case p_tab+ when 'no_ai_traffic' then no_ai_traffic+ when 'cited_not_clicked' then cited_not_clicked+ when 'traffic_no_prompt' then traffic_no_prompt+ else true+ end+ )+ select jsonb_build_object(+ 'counts', (+ select jsonb_build_object(+ 'all', count(*),+ 'no_ai_traffic', count(*) filter (where no_ai_traffic),+ 'cited_not_clicked', count(*) filter (where cited_not_clicked),+ 'traffic_no_prompt', count(*) filter (where traffic_no_prompt),+ -- The Broken AI links tab's count, so the card needn't read that list for it.+ 'broken_ai_links', (select count(*) from broken)+ )+ from kept+ ),+ 'rows', coalesce((+ select jsonb_agg(to_jsonb(r) - 'page_key' - 'no_ai_traffic' - 'cited_not_clicked' - 'traffic_no_prompt')+ from (+ select * from tab+ order by+ -- Each list in the order it's worked through.+ case when p_tab = 'no_ai_traffic' then revenue end desc nulls last,+ case when p_tab = 'no_ai_traffic' then key_events end desc nulls last,+ case when p_tab = 'no_ai_traffic' then sessions end desc nulls last,+ case when p_tab = 'no_ai_traffic' then engagement_seconds end desc nulls last,+ case when p_tab = 'cited_not_clicked' then citations end desc nulls last,+ ai_visits desc, citations desc, sessions desc, page+ limit greatest(p_limit, 0) offset greatest(p_offset, 0)+ ) r+ ), '[]'::jsonb)+ )+ into v_result;++ return v_result;+end;+$$;++revoke all on function public.ai_traffic_pages(uuid, timestamptz, timestamptz, text, integer, integer)+ from public, anon;+grant execute on function public.ai_traffic_pages(uuid, timestamptz, timestamptz, text, integer, integer)+ to authenticated, service_role;++comment on function public.ai_traffic_pages(uuid, timestamptz, timestamptz, text, integer, integer) is+ 'AI Traffic Pages section (AT-006, spec §3.5): the brand''s pages in a window with AI visits (ai_traffic_merged), AI key events, top platforms, citations, prompts pointing to them, all-channel traffic, sitemap presence and whether AI visitors found the page missing (AT-012); the counts of the No AI traffic / Cited, not clicked / Traffic, no prompt lists, which leave missing pages out, and one page of the asked tab''s rows. Security definer with an organization check.';++notify pgrst, 'reload schema';diff --git a/supabase/schema.sql b/supabase/schema.sqlindex 8374cfa72..17e561fcc 100644--- a/supabase/schema.sql+++ b/supabase/schema.sql@@ -25521,3 +25521,629 @@ comment on function public.ai_bot_report(uuid, date, date, integer) isnotify pgrst, 'reload schema';+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00186_ai_traffic_broken_links.sql+-- ─────────────────────────────────────────────────────────────────────────+-- ============================================================================+-- 00186: broken AI links (AT-012)+-- ============================================================================+-- Spec §3.5: "AI-referred visits that landed on a 404 → suggested redirect,+-- exportable as a redirect list." Snippet only (§3.8b).+--+-- The snippet now sends the landing page's HTTP status as the browser saw it+-- (the navigation entry's responseStatus). Browsers that don't report one+-- (Safari) send none, and say instead whether the page's title read "404" or+-- "not found":+--+-- * http_status — the status, or null when the browser didn't tell;+-- * not_found_title — no status, and the title said "not found".+--+-- A page is broken in a range when AI visitors found it missing in the range+-- and its latest pageview with a status, up to now, still says 404 or 410.+-- With no status at all, a title saying "not found" counts unless the nightly+-- page read (site_pages, 00104) found the page working: a post about fixing+-- 404s is not a missing page. A page the nightly read found working after its+-- last miss is fixed, and isn't listed. The status is decided per address+-- (the path plus its query, utm_* and click ids such as gclid left out), and+-- a path is broken only when none of its addresses last answered 2xx or 3xx:+-- on a shop where /index.php?route=product&id=42 is gone and the other+-- products work, /index.php isn't broken. When every missing address of a+-- page carries a query (a route on it, not the page), no redirect is+-- suggested: one from the path would move the whole route. Its visits are+-- the pageviews that found it missing (404, 410, or no status and a+-- not-found title), not those from before it broke.+--+-- Each other broken page gets a suggested redirect to a page known to work+-- (read 2xx by the nightly page read, or its address without a query landed+-- on with a 2xx in the last 90 days), the first that fits of:+--+-- * similar — the most path words in common, at least half of both pages'+-- words together, a plural read as its singular+-- (/blog/best-tools-2025 → /blog/best-tools-2026);+-- * parent — the nearest working page above it (/guides/old → /guides);+-- * home — the home page.+--+-- The Pages section's Traffic, no prompt list leaves broken pages out: their+-- next step is a redirect, not prompts. ai_traffic_pages is redefined from+-- 00178 for that, and its rows say whether a page is broken.+--+-- Pages are keyed as the Pages section keys them (00178). citation_urls has+-- no member select policy, so both reports run as their owner after+-- checking the caller belongs to the brand's organization; a caller from+-- elsewhere gets nothing. The rule itself (ai_traffic_broken_pages) has no+-- check of its own, so members can't call it.++-- Beacon inserts don't queue behind the column change for long: it gives up+-- after 3 s instead (run it again).+set lock_timeout = '3s';++alter table public.ai_traffic_logs+ add column if not exists http_status smallint,+ add column if not exists not_found_title boolean not null default false;++reset lock_timeout;++comment on column public.ai_traffic_logs.http_status is+ 'The landing page''s HTTP status as the visitor''s browser saw it; null when the browser doesn''t report one (Safari) or from an older snippet (AT-012).';+comment on column public.ai_traffic_logs.not_found_title is+ 'True when the browser reported no status and the page''s title read "404" or "not found" (AT-012).';++-- ─── Which pages are broken ─────────────────────────────────────────────────+-- The rule above, for both the Broken AI links tab and the Pages section's+-- other lists, which leave these pages out (a missing page needs a redirect,+-- not prompts). No organization check of its own: only the two functions+-- below call it, after theirs.++create or replace function public.ai_traffic_broken_pages(+ p_brand_id uuid,+ p_from timestamptz,+ p_to timestamptz+)+returns table (page_key text, status text, routed boolean)+language sql+stable+set search_path to 'public'+as $$+ with+ -- Only the pageviews that say something about the page: a status, or a+ -- not-found title. Read up to now, so a page that works again since the+ -- window ended isn't listed.+ logs as (+ select+ n.page_key,+ -- The address: the path plus its query, tracking parameters left out,+ -- in one order. Worked out only when the URL has a query.+ case when q.query is null then n.page_key else n.page_key || '?' || q.query end as address,+ l.created_at,+ l.http_status,+ l.not_found_title,+ (p_to is null or l.created_at <= p_to)+ and (l.http_status in (404, 410) or (l.http_status is null and l.not_found_title))+ as failed_in_window+ from public.ai_traffic_logs l+ cross join lateral (select lower(public.normalize_page_path(l.url)) as page_key) n+ cross join lateral (+ select case when strpos(l.url, '?') > 0 then (+ select string_agg(qp.part, '&' order by qp.part)+ from unnest(string_to_array(split_part(split_part(l.url, '#', 1), '?', 2), '&')) as qp(part)+ where qp.part <> ''+ and qp.part !~* '^(utm_[^=]*|gclid|fbclid|msclkid|srsltid|_gl|ref)(=|$)'+ ) end as query+ ) q+ where l.brand_id = p_brand_id+ and l.created_at >= coalesce(p_from, '-infinity'::timestamptz)+ and (l.http_status is not null or l.not_found_title)+ ),+ address_state as (+ select+ page_key,+ address,+ (array_agg(http_status order by created_at desc) filter (where http_status is not null))[1]+ as last_status,+ bool_or(not_found_title) as title_not_found,+ max(created_at) filter (where failed_in_window) as last_failed_at+ from logs+ group by page_key, address+ ),+ page_state as (+ select+ page_key,+ max(last_status) filter (where last_status in (404, 410)) as last_status,+ bool_or(last_status is null and title_not_found) as title_not_found,+ -- No address with a status reads as not working, not as unknown.+ coalesce(bool_or(last_status between 200 and 399), false) as works,+ max(last_failed_at) as last_failed_at,+ -- Every missing address carries a query: a route on the page, not the page.+ bool_and(address <> page_key)+ filter (where last_status in (404, 410) or (last_status is null and title_not_found))+ as routed+ from address_state+ group by page_key+ ),+ -- What the nightly page read found (00104).+ fetched as (+ select lower(public.normalize_page_path(sp.url)) as page_key,+ bool_or(sp.fetch_status between 200 and 399) as works,+ max(sp.fetched_at) filter (where sp.fetch_status between 200 and 299) as last_ok_at+ from public.site_pages sp+ where sp.brand_id = p_brand_id+ and sp.fetch_status is not null+ group by 1+ )+ select+ s.page_key,+ case when s.last_status is not null then s.last_status::text else 'title' end,+ coalesce(s.routed, false)+ from page_state s+ left join fetched f on f.page_key = s.page_key+ where not s.works+ and s.last_failed_at is not null+ -- The nightly read found it working after its last miss: fixed since.+ and not coalesce(f.last_ok_at > s.last_failed_at, false)+ and (+ s.last_status in (404, 410)+ or (s.last_status is null and s.title_not_found and not coalesce(f.works, false))+ );+$$;++revoke all on function public.ai_traffic_broken_pages(uuid, timestamptz, timestamptz)+ from public, anon, authenticated;+grant execute on function public.ai_traffic_broken_pages(uuid, timestamptz, timestamptz)+ to service_role;++comment on function public.ai_traffic_broken_pages(uuid, timestamptz, timestamptz) is+ 'AI Traffic (AT-012): the brand''s pages AI visitors found missing in a window whose latest reported status up to now is 404 or 410, or, with no status, whose title said not found and that the nightly page read never found working; not those the nightly read found working after their last miss. Decided per address (path plus query, utm_* and click ids left out), and a path none of whose addresses last answered 2xx or 3xx; routed when every missing address carries a query. No organization check: called only by ai_traffic_broken_links and ai_traffic_pages, after theirs.';++-- ─── Broken AI links ────────────────────────────────────────────────────────++create or replace function public.ai_traffic_broken_links(+ p_brand_id uuid,+ p_from timestamptz,+ p_to timestamptz,+ p_limit integer default 50,+ p_offset integer default 0+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+as $$+declare+ v_since date := (p_from at time zone 'utc')::date;+ v_until date := coalesce((p_to at time zone 'utc')::date, (now() at time zone 'utc')::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;++ with+ logs as (+ select+ lower(n.page) as page_key,+ n.page,+ coalesce(l.visit_of, l.id) as visit,+ l.created_at,+ coalesce(nullif(l.source_platform, ''), 'unknown') as platform+ from public.ai_traffic_logs l+ cross join lateral (select public.normalize_page_path(l.url) as page) n+ where l.brand_id = p_brand_id+ and l.created_at >= coalesce(p_from, '-infinity'::timestamptz)+ and (p_to is null or l.created_at <= p_to)+ -- Only the pageviews that found the page missing, not those from before it broke.+ and (l.http_status in (404, 410) or (l.http_status is null and l.not_found_title))+ ),+ broken as (+ select bp.page_key, bp.status, bp.routed+ from public.ai_traffic_broken_pages(p_brand_id, p_from, p_to) bp+ ),+ visits as (+ select l.page_key, min(l.page) as page, count(distinct l.visit)::bigint as visits,+ max(l.created_at) as last_visit+ from logs l+ join broken b on b.page_key = l.page_key+ group by l.page_key+ ),+ platforms as (+ select page_key, (array_agg(platform order by visits desc, platform))[1:3] as platforms+ from (+ select l.page_key, l.platform, count(distinct l.visit) as visits+ from logs l+ join broken b on b.page_key = l.page_key+ group by 1, 2+ ) t+ group by 1+ ),+ cited as (+ select c.page_key, c.citations+ from public.ai_traffic_page_citations(p_brand_id, v_since, v_until) c+ ),+ -- Pages known to work, to redirect to. Only a page's own address counts:+ -- /product.php?id=5 answering 200 says nothing about /product.php.+ landed as (+ select lower(n.page) as page_key,+ min(n.page) as page,+ (array_agg(l.http_status order by l.created_at desc))[1] as last_status+ from public.ai_traffic_logs l+ cross join lateral (select public.normalize_page_path(l.url) as page) n+ where l.brand_id = p_brand_id+ and l.http_status is not null+ and l.created_at >= now() - interval '90 days'+ and (+ strpos(l.url, '?') = 0+ or not exists (+ select 1+ from unnest(string_to_array(split_part(split_part(l.url, '#', 1), '?', 2), '&')) as qp(part)+ where qp.part <> ''+ and qp.part !~* '^(utm_[^=]*|gclid|fbclid|msclkid|srsltid|_gl|ref)(=|$)'+ )+ )+ group by 1+ ),+ candidates as (+ select page_key, min(page) as page, max(weight) as weight+ from (+ select lower(public.normalize_page_path(sp.url)) as page_key,+ public.normalize_page_path(sp.url) as page,+ sp.ga_sessions as weight+ from public.site_pages sp+ where sp.brand_id = p_brand_id+ and sp.fetch_status between 200 and 299+ union all+ select page_key, page, 0 from landed where last_status between 200 and 299+ ) c+ where page_key not in (select page_key from broken)+ group by page_key+ ),+ candidate_words as (+ select distinct c.page_key, regexp_replace(w.word, '^(.{3,})s$', '\1') as word+ from candidates c+ cross join lateral regexp_split_to_table(c.page_key, '[^[:alnum:]]+') as w(word)+ where length(w.word) >= 2+ ),+ broken_words as (+ select distinct b.page_key, regexp_replace(w.word, '^(.{3,})s$', '\1') as word+ from broken b+ cross join lateral regexp_split_to_table(b.page_key, '[^[:alnum:]]+') as w(word)+ where length(w.word) >= 2+ ),+ candidate_sizes as (select page_key, count(*) as words from candidate_words group by 1),+ broken_sizes as (select page_key, count(*) as words from broken_words group by 1),+ similar_pages as (+ select distinct on (page_key) page_key, target+ from (+ select+ bw.page_key,+ c.page as target,+ c.weight,+ count(*)::numeric / (bs.words + cs.words - count(*)) as overlap+ from broken_words bw+ join candidate_words cw on cw.word = bw.word+ join candidates c on c.page_key = cw.page_key+ join broken_sizes bs on bs.page_key = bw.page_key+ join candidate_sizes cs on cs.page_key = cw.page_key+ group by bw.page_key, c.page_key, c.page, c.weight, bs.words, cs.words+ ) m+ where overlap >= 0.5+ order by page_key, overlap desc, weight desc, length(target), target+ ),+ parent_pages as (+ select distinct on (b.page_key) b.page_key, c.page as target+ from broken b+ join candidates c+ on c.page_key <> '/'+ and left(b.page_key, length(c.page_key) + 1) = c.page_key || '/'+ order by b.page_key, length(c.page_key) desc+ ),+ result_rows as (+ select+ v.page,+ v.visits,+ v.last_visit,+ b.status,+ coalesce(p.platforms, '{}'::text[]) as platforms,+ coalesce(ci.citations, 0)::bigint as citations,+ -- A route on the page (every missing address has a query) gets no+ -- redirect: one from its path would move the whole route.+ case when v.page_key <> '/' and not b.routed then coalesce(s.target, pa.target, '/') end+ as target,+ case+ when v.page_key = '/' then null+ when b.routed then 'query'+ when s.target is not null then 'similar'+ when pa.target is not null then 'parent'+ else 'home'+ end as target_reason+ from visits v+ join broken b on b.page_key = v.page_key+ left join platforms p on p.page_key = v.page_key+ left join cited ci on ci.page_key = v.page_key+ left join similar_pages s on s.page_key = v.page_key+ left join parent_pages pa on pa.page_key = v.page_key+ )+ select jsonb_build_object(+ 'count', (select count(*) from result_rows),+ 'rows', coalesce((+ select jsonb_agg(to_jsonb(r))+ from (+ select * from result_rows+ order by visits desc, citations desc, last_visit desc, page+ limit greatest(p_limit, 0) offset greatest(p_offset, 0)+ ) r+ ), '[]'::jsonb)+ )+ into v_result;++ return v_result;+end;+$$;++revoke all on function public.ai_traffic_broken_links(uuid, timestamptz, timestamptz, integer, integer)+ from public, anon;+grant execute on function public.ai_traffic_broken_links(uuid, timestamptz, timestamptz, integer, integer)+ to authenticated, service_role;++comment on function public.ai_traffic_broken_links(uuid, timestamptz, timestamptz, integer, integer) is+ 'AI Traffic "Broken AI links" (AT-012, spec §3.5): the pages AI-referred visitors landed on that answered 404 or 410 (or, without a status, whose title said not found) in a window, with their visits, platforms, citations and a suggested redirect to a working page (similar path, parent page or home; none for a route on the page, target_reason query). Security definer with an organization check.';++-- ─── The Pages section leaves broken pages out of Traffic, no prompt ─────────+-- 00178's function with one change: a page AI visitors found missing is not+-- "Traffic, no prompt" (its next step is a redirect, on Broken AI links), and+-- each row says whether it is broken. Everything else is as 00178 wrote it.++create or replace function public.ai_traffic_pages(+ p_brand_id uuid,+ p_from timestamptz,+ p_to timestamptz,+ p_tab text default 'all',+ p_limit integer default 50,+ p_offset integer default 0+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+as $$+declare+ v_since date := (p_from at time zone 'utc')::date;+ v_until date := coalesce((p_to at time zone 'utc')::date, (now() at time zone 'utc')::date);+ -- The 24-hour range reads the snippet alone, as the rest of the page does:+ -- Analytics has no hours (00176).+ v_hourly boolean := p_from is not null and coalesce(p_to, now()) - p_from <= interval '25 hours';+ v_result jsonb;+begin+ if p_tab not in ('all', 'no_ai_traffic', 'cited_not_clicked', 'traffic_no_prompt') then+ raise exception 'unknown tab %', p_tab;+ end if;+ 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;++ with+ -- AI visits per page, day by day from the snippet or Analytics (00176).+ ai as (+ select lower(e->>'page') as page_key, min(e->>'page') as page, sum((e->>'visits')::bigint) as visits+ from jsonb_array_elements(+ public.ai_traffic_merged(p_brand_id, p_from, p_to, true, not v_hourly) -> 'pages'+ ) e+ group by 1+ ),+ -- Days the snippet was active, over the whole UTC day (00176's rule).+ snippet_days as (+ select distinct (l.created_at at time zone 'utc')::date as day+ from public.ai_traffic_logs l+ where l.brand_id = p_brand_id+ and l.visit_of is null+ and l.created_at >= coalesce(v_since::timestamp at time zone 'utc', '-infinity'::timestamptz)+ and l.created_at < (v_until + 1)::timestamp at time zone 'utc'+ ),+ -- Which assistants sent them, counted as the AI visits are: the snippet,+ -- and Analytics on days without it (never within 24 hours).+ platform_visits as (+ select lower(public.normalize_page_path(l.url)) as page_key,+ coalesce(nullif(l.source_platform, ''), 'unknown') as platform,+ count(*)::bigint as visits+ from public.ai_traffic_logs l+ where l.brand_id = p_brand_id+ and l.visit_of is null+ and l.created_at >= coalesce(p_from, '-infinity'::timestamptz)+ and (p_to is null or l.created_at <= p_to)+ group by 1, 2+ union all+ select lower(public.normalize_page_path(g.landing_page)),+ coalesce(g.platform, g.source),+ sum(g.sessions)::bigint+ from public.ga_ai_traffic_stats g+ where not v_hourly+ and g.brand_id = p_brand_id+ and g.landing_page <> ''+ and g.sessions > 0+ and g.date >= coalesce(v_since, '-infinity'::date)+ and g.date <= v_until+ and not exists (select 1 from snippet_days s where s.day = g.date)+ group by 1, 2+ ),+ platforms as (+ select page_key, (array_agg(platform order by visits desc, platform))[1:3] as platforms+ from (select page_key, platform, sum(visits) as visits from platform_visits group by 1, 2) t+ group by 1+ ),+ -- Key events in AI sessions: only Analytics has them, and it has no hours.+ ai_key_events as (+ select lower(public.normalize_page_path(g.landing_page)) as page_key, sum(g.key_events)::bigint as key_events+ from public.ga_ai_traffic_stats g+ where not v_hourly+ and g.brand_id = p_brand_id+ and g.landing_page <> ''+ and g.date >= coalesce(v_since, '-infinity'::date)+ and g.date <= v_until+ group by 1+ ),+ -- Every channel's traffic, for the No AI traffic list's value.+ traffic as (+ select+ lower(public.normalize_page_path(gps.landing_page)) as page_key,+ min(public.normalize_page_path(gps.landing_page)) as page,+ sum(gps.sessions)::bigint as sessions,+ sum(gps.engaged_sessions)::bigint as engaged_sessions,+ sum(gps.key_events)::bigint as key_events,+ sum(gps.transactions)::bigint as transactions,+ sum(gps.purchase_revenue) as revenue,+ sum(gps.engagement_duration_seconds)::bigint as engagement_seconds+ from public.ga_page_stats gps+ where gps.brand_id = p_brand_id+ and gps.date >= coalesce(v_since, '-infinity'::date)+ and gps.date <= v_until+ group by 1+ ),+ cited as (+ select c.page_key, c.page, c.citations+ from public.ai_traffic_page_citations(p_brand_id, v_since, v_until) c+ ),+ -- Prompts pointing to the page: an active prompt's target URL.+ targeted as (+ select lower(public.normalize_page_path(ptu.url)) as page_key,+ count(distinct ptu.prompt_id)::bigint as prompts+ 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+ group by 1+ ),+ -- Pages that answered 404 or 410 to AI visitors (AT-012): Broken AI links.+ broken as (+ select bp.page_key from public.ai_traffic_broken_pages(p_brand_id, p_from, p_to) bp+ ),+ in_sitemap as (+ select distinct lower(public.normalize_page_path(sp.url)) as page_key,+ public.normalize_page_path(sp.url) as page+ from public.site_pages sp+ where sp.brand_id = p_brand_id+ and 'sitemap' = any(sp.sources)+ ),+ keys as (+ select page_key, page from ai+ union select page_key, page from traffic+ union select page_key, page from cited+ union select page_key, page from in_sitemap+ ),+ pages as (+ select+ k.page_key,+ min(k.page) as page,+ coalesce(max(ai.visits), 0) as ai_visits,+ coalesce(max(ke.key_events), 0) as ai_key_events,+ coalesce(max(pl.platforms), '{}'::text[]) as ai_platforms,+ coalesce(max(c.citations), 0) as citations,+ coalesce(max(t.prompts), 0) as targeting_prompts,+ coalesce(max(tr.sessions), 0) as sessions,+ coalesce(max(tr.engaged_sessions), 0) as engaged_sessions,+ coalesce(max(tr.key_events), 0) as key_events,+ coalesce(max(tr.transactions), 0) as transactions,+ coalesce(max(tr.revenue), 0) as revenue,+ coalesce(max(tr.engagement_seconds), 0) as engagement_seconds,+ bool_or(s.page_key is not null) as in_sitemap,+ bool_or(br.page_key is not null) as broken+ from keys k+ left join ai on ai.page_key = k.page_key+ left join ai_key_events ke on ke.page_key = k.page_key+ left join platforms pl on pl.page_key = k.page_key+ left join cited c on c.page_key = k.page_key+ left join targeted t on t.page_key = k.page_key+ left join traffic tr on tr.page_key = k.page_key+ left join in_sitemap s on s.page_key = k.page_key+ left join broken br on br.page_key = k.page_key+ group by k.page_key+ ),+ kept as (+ select p.*,+ -- The page's place on the site by value, among pages with traffic.+ case when p.sessions > 0 then+ rank() over (+ partition by (p.sessions > 0)+ order by p.revenue desc, p.key_events desc, p.sessions desc, p.engagement_seconds desc+ )+ end as value_rank,+ p.ai_visits = 0 and (p.sessions > 0 or p.in_sitemap) as no_ai_traffic,+ p.citations >= 20 and p.ai_visits = 0 as cited_not_clicked,+ -- A missing page needs a redirect, not prompts (AT-012).+ p.ai_visits > 0 and p.citations = 0 and not p.broken as traffic_no_prompt+ from pages p+ where p.page not in ('', '(not set)', '(other)')+ -- The nightly detection's excluded segments (page-paths.js).+ and not exists (+ select 1+ from unnest(string_to_array(lower(p.page), '/')) seg+ where seg = any (array[+ 'checkout', 'cart', 'basket', 'order', 'orders', 'order-confirmation',+ 'confirmation', 'thank-you', 'thanks', 'payment', 'pay', 'login', 'signin',+ 'sign-in', 'signup', 'sign-up', 'register', 'logout', 'account', 'my-account',+ 'profile', 'dashboard', 'admin', 'wishlist', 'favorites', 'search', 'career',+ 'careers', 'jobs', 'job', 'cookie-policy', 'privacy', 'privacy-policy', 'terms',+ 'terms-of-service', 'legal', 'unsubscribe'+ ])+ )+ ),+ tab as (+ select * from kept+ where case p_tab+ when 'no_ai_traffic' then no_ai_traffic+ when 'cited_not_clicked' then cited_not_clicked+ when 'traffic_no_prompt' then traffic_no_prompt+ else true+ end+ )+ select jsonb_build_object(+ 'counts', (+ select jsonb_build_object(+ 'all', count(*),+ 'no_ai_traffic', count(*) filter (where no_ai_traffic),+ 'cited_not_clicked', count(*) filter (where cited_not_clicked),+ 'traffic_no_prompt', count(*) filter (where traffic_no_prompt),+ -- The Broken AI links tab's count, so the card needn't read that list for it.+ 'broken_ai_links', (select count(*) from broken)+ )+ from kept+ ),+ 'rows', coalesce((+ select jsonb_agg(to_jsonb(r) - 'page_key' - 'no_ai_traffic' - 'cited_not_clicked' - 'traffic_no_prompt')+ from (+ select * from tab+ order by+ -- Each list in the order it's worked through.+ case when p_tab = 'no_ai_traffic' then revenue end desc nulls last,+ case when p_tab = 'no_ai_traffic' then key_events end desc nulls last,+ case when p_tab = 'no_ai_traffic' then sessions end desc nulls last,+ case when p_tab = 'no_ai_traffic' then engagement_seconds end desc nulls last,+ case when p_tab = 'cited_not_clicked' then citations end desc nulls last,+ ai_visits desc, citations desc, sessions desc, page+ limit greatest(p_limit, 0) offset greatest(p_offset, 0)+ ) r+ ), '[]'::jsonb)+ )+ into v_result;++ return v_result;+end;+$$;++revoke all on function public.ai_traffic_pages(uuid, timestamptz, timestamptz, text, integer, integer)+ from public, anon;+grant execute on function public.ai_traffic_pages(uuid, timestamptz, timestamptz, text, integer, integer)+ to authenticated, service_role;++comment on function public.ai_traffic_pages(uuid, timestamptz, timestamptz, text, integer, integer) is+ 'AI Traffic Pages section (AT-006, spec §3.5): the brand''s pages in a window with AI visits (ai_traffic_merged), AI key events, top platforms, citations, prompts pointing to them, all-channel traffic, sitemap presence and whether AI visitors found the page missing (AT-012); the counts of the No AI traffic / Cited, not clicked / Traffic, no prompt lists, which leave missing pages out, and one page of the asked tab''s rows. Security definer with an organization check.';++notify pgrst, 'reload schema';+diff --git a/web/messages/en.json b/web/messages/en.jsonindex 836af8ee2..0f0e40518 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",+ "brokenLinks": "Broken AI links","googlePages": "Is AI taking Google clicks","googleQueries": "Untracked Google queries","bots": "AI bots",@@ -3193,7 +3194,13 @@"lastSeen": "Last seen (UTC)","trainingLast": "Training, last read (UTC)","searchLast": "Search index, last read (UTC)",- "userLast": "User fetch, last read (UTC)"+ "userLast": "User fetch, last read (UTC)",+ "redirectFrom": "From",+ "redirectTo": "To",+ "redirectType": "Redirect type",+ "redirectReason": "Why this page",+ "lastVisit": "Last visit (UTC)",+ "landingStatus": "What the visitor got"},"metrics": {"visits": "AI-referred visits",@@ -3218,7 +3225,8 @@"noGoogleQueries": "No query data from Google Search Console yet","noUntrackedQueries": "Every Google query with clicks in this range is tracked","queriesCapped": "Only the 5,000 most clicked queries were checked",- "noBots": "No AI-bot requests in this range"+ "noBots": "No AI-bot requests in this range",+ "noBrokenLinks": "No AI visitor landed on a missing page in this range"},"botTypes": {"training": "Training",@@ -3252,10 +3260,22 @@"pageStatuses": {"noAiTraffic": "No AI traffic","citedNotClicked": "Cited, not clicked",- "trafficNoPrompt": "Traffic, no prompt"+ "trafficNoPrompt": "Traffic, no prompt",+ "brokenAiLinks": "Broken AI links"},"yes": "Yes",- "no": "No"+ "no": "No",+ "brokenStatuses": {+ "404": "Not found (404)",+ "410": "Gone (410)",+ "title": "Page title says not found"+ },+ "redirectReasons": {+ "similar": "Similar page",+ "parent": "The page above it",+ "home": "Home page",+ "query": "Missing addresses have a query: no redirect suggested"+ }},"change": {"new": "new",@@ -3653,6 +3673,11 @@"explain": "Pages AI assistants sent visitors to that no tracked prompt's answer cited in this range: the visits come from questions you don't track.","empty": "Every page with AI visits is cited by a prompt you track in this range."},+ "brokenAiLinks": {+ "label": "Broken AI links",+ "explain": "Pages AI visitors landed on that don't exist (404). Redirect each one to the suggested page: Excel or CSV gives you the redirect list.",+ "empty": "No AI visitor landed on a missing page in this range. They show here when your 404 page carries the tracking snippet too."+ },"all": {"label": "All pages","explain": "Every page with AI visits, citations, traffic or a sitemap entry in this range.",@@ -3669,7 +3694,9 @@"platforms": "Top platforms","keyEvents": "Key events","status": "Status",- "citedInAnswers": "Cited in answers"+ "citedInAnswers": "Cited in answers",+ "lastVisit": "Last visit",+ "redirectTo": "Redirect to"},"auditPage": "Audit page","addPrompts": "Add prompts",@@ -3732,6 +3759,19 @@"planFull": "Your plan's prompt locations are full","trackFailed": "Couldn't track the prompts.","openPrompts": "Open Prompts"+ },+ "broken": {+ "status": {+ "404": "Not found (404)",+ "410": "Gone (410)",+ "title": "Page title says not found"+ },+ "reason": {+ "similar": "Similar page",+ "parent": "The page above it",+ "home": "Home page",+ "query": "Missing addresses have a query: no redirect suggested"+ }}}},diff --git a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_pages-card.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_pages-card.test.tsxindex ab563e7b1..cc585a4f8 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_pages-card.test.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_pages-card.test.tsx@@ -3,7 +3,12 @@ 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 { TrafficDateWindow, TrafficPageRow, TrafficPages } from '@/lib/actions/traffic';+import type {+ TrafficBrokenLink,+ TrafficDateWindow,+ TrafficPageRow,+ TrafficPages,+} from '@/lib/actions/traffic';/*** The Pages section (AT-006): three work lists and the full list, each with@@ -84,13 +89,26 @@ const page = (over: Partial<TrafficPageRow> = {}): TrafficPageRow => ({revenue: 0,engagementSeconds: 0,inSitemap: true,+ broken: false,valueRank: 1,...over,});-const pages = (rows: TrafficPageRow[], counts: Partial<TrafficPages['counts']> = {}) => ({- counts: { noAiTraffic: 3, citedNotClicked: 1, trafficNoPrompt: 2, all: 14, ...counts },+const pages = (+ rows: TrafficPageRow[],+ counts: Partial<TrafficPages['counts']> = {},+ brokenLinks: TrafficBrokenLink[] = [],+): TrafficPages => ({+ counts: {+ noAiTraffic: 3,+ citedNotClicked: 1,+ trafficNoPrompt: 2,+ brokenAiLinks: 2,+ all: 14,+ ...counts,+ },rows,+ brokenLinks,});let container: HTMLDivElement;@@ -120,6 +138,8 @@ async function render({domain = 'acme-coffee.example.com',analyticsMissing = false,}: { domain?: string; analyticsMissing?: boolean } = {}) {+ // Each list's own Excel / CSV, told apart by name.+ const exportButton = (name: string) => <span data-testid="export">{name}</span>;await act(async () => {root.render(<NextIntlClientProvider locale="en" messages={messages} timeZone="UTC">@@ -129,6 +149,8 @@ async function render({rangeLabel="Last 7 days"domain={domain}analyticsMissing={analyticsMissing}+ actions={exportButton('pages by status')}+ brokenActions={exportButton('redirect list')}/></NextIntlClientProvider>,);@@ -255,6 +277,8 @@ describe('PagesCard', () => {pages([page({ citations: 25, sessions: 40 }),page({ page: '/subscriptions', aiVisits: 8, citations: 4 }),+ // A missing page AI visitors land on is a broken link, not Traffic, no prompt (AT-012).+ page({ page: '/old-menu', aiVisits: 2, sessions: 0, inSitemap: false, broken: true }),]),);await act(async () => tab('All pages').click());@@ -262,7 +286,7 @@ describe('PagesCard', () => {const badges = [...container.querySelectorAll('tbody [data-slot="badge"]')].map((b) => b.textContent,);- expect(badges).toEqual(['No AI traffic', 'Cited, not clicked']);+ expect(badges).toEqual(['No AI traffic', 'Cited, not clicked', 'Broken AI links']);// A page on no list is doing fine.const status = [...container.querySelectorAll('tbody tr')][1].lastElementChild;expect(status?.textContent).toBe('—');@@ -280,6 +304,97 @@ describe('PagesCard', () => {expect(text()).toContain("The pages couldn't be loaded.");});+ it('lists the broken AI links with where to redirect each, and exports the redirect list (AT-012)', async () => {+ getTrafficPages.mockResolvedValue(pages([]));+ await render();+ expect(tab('Broken AI links').textContent).toBe('Broken AI links2');+ const exported = () => container.querySelector('[data-testid="export"]')?.textContent;+ expect(exported()).toBe('pages by status');++ const hoursAgo = (h: number) => new Date(Date.now() - h * 3_600_000).toISOString();+ getTrafficPages.mockResolvedValue(+ pages([], { brokenAiLinks: 3 }, [+ {+ page: '/guides/espresso-machine-for-beginner',+ visits: 3,+ platforms: ['chatgpt.com', 'perplexity.ai'],+ citations: 14,+ lastVisit: hoursAgo(5),+ status: '404',+ target: '/guides/espresso-machines-for-beginners',+ targetReason: 'similar',+ },+ {+ page: '/subscriptions/old-plan',+ visits: 1,+ platforms: [],+ citations: 0,+ lastVisit: hoursAgo(30),+ status: 'title',+ target: '/subscriptions',+ targetReason: 'parent',+ },+ {+ page: '/index.php',+ visits: 1,+ platforms: [],+ citations: 0,+ lastVisit: hoursAgo(30),+ status: '404',+ target: null,+ targetReason: 'query',+ },+ ]),+ );+ await act(async () => tab('Broken AI links').click());++ expect(getTrafficPages).toHaveBeenLastCalledWith(BRAND, WEEK, {+ tab: 'brokenAiLinks',+ limit: 10,+ });+ const rows = [...container.querySelectorAll('tbody tr')].map((row) =>+ [...row.querySelectorAll('td')].map((cell) => cell.textContent),+ );+ expect(rows).toEqual([+ [+ '/guides/espresso-machine-for-beginnerNot found (404)',+ '3',+ 'ChatGPT, Perplexity',+ '14',+ '5h ago',+ '→ /guides/espresso-machines-for-beginnersSimilar page',+ ],+ [+ '/subscriptions/old-planPage title says not found',+ '1',+ '—',+ '0',+ '1d ago',+ '→ /subscriptionsThe page above it',+ ],+ [+ '/index.phpNot found (404)',+ '1',+ '—',+ '0',+ '1d ago',+ '—Missing addresses have a query: no redirect suggested',+ ],+ ]);+ expect(text()).toContain('3 of 3 pages');+ // Its Excel / CSV is the redirect list; no Action Center footer.+ expect(exported()).toBe('redirect list');+ expect(text()).not.toContain('Send to Action Center');+ });++ it('says how broken links show up when there are none', async () => {+ getTrafficPages.mockResolvedValue(pages([], { brokenAiLinks: 0 }));+ await render();+ await act(async () => tab('Broken AI links').click());+ expect(text()).toContain('when your 404 page carries the tracking snippet too');+ expect(text()).not.toContain('{');+ });+it('audits a page on a domain stored with its scheme', async () => {getTrafficPages.mockResolvedValue(pages([page({ citations: 6 })]));runAudit.mockResolvedValue({ id: 'audit-2' });diff --git a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_pages-card.tsx b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_pages-card.tsxindex 03fc78233..7c09c3faa 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_pages-card.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_pages-card.tsx@@ -26,10 +26,12 @@ import {} from '@/components/ui/table';import { cn } from '@/lib/utils';import { formatNumber } from '@/lib/format';+import { formatRelative } from '@/lib/format-relative';import { getPlatformName } from '@/lib/traffic-platforms';import { citationState, pageStatuses, pageUrl } from '@/lib/traffic-pages';import {getTrafficPages,+ type TrafficBrokenLink,type TrafficDateWindow,type TrafficPageRow,type TrafficPages,@@ -52,6 +54,9 @@ import { PagePromptsDialog } from './_page-prompts-dialog';* - Cited, not clicked: answers cite them at least 20 times, nobody clicks.* - Traffic, no prompt: AI visitors arrive from questions no tracked* prompt's answer covers.+ * - Broken AI links: AI visitors landed on a page that doesn't exist, with+ * the working page to redirect it to (AT-012); its Excel / CSV is the+ * redirect list.** A row's action is the next step for it: audit the page, or track prompts* suggested for it that point to it; its menu sends it to the Action Center. Under the two@@ -59,7 +64,13 @@ import { PagePromptsDialog } from './_page-prompts-dialog';* their cited pages can be audited at once (AT-006 part B).*/-const TABS: TrafficPagesTab[] = ['noAiTraffic', 'citedNotClicked', 'trafficNoPrompt', 'all'];+const TABS: TrafficPagesTab[] = [+ 'noAiTraffic',+ 'citedNotClicked',+ 'trafficNoPrompt',+ 'brokenAiLinks',+ 'all',+];const PAGE_SIZE = 10;/** Which figure ranks No AI traffic, in the words of what the property reports. */@@ -92,6 +103,74 @@ function PageCell({ page }: { page: string }) {const num = (n: number) => <span className="tabular-nums">{formatNumber(n)}</span>;+/** Broken AI links (AT-012): each missing page, what the visitor got and where to send it. */+function BrokenLinksTable({ rows }: { rows: TrafficBrokenLink[] }) {+ const t = useTranslations('traffic.pageList');+ const tCommon = useTranslations('common');+ return (+ <Table>+ <TableHeader>+ <TableRow>+ <TableHead className="pl-6">{t('columns.page')}</TableHead>+ <TableHead className="whitespace-normal text-right">{t('columns.aiVisits')}</TableHead>+ <TableHead className="whitespace-normal">{t('columns.platforms')}</TableHead>+ <TableHead className="whitespace-normal text-right">{t('columns.citations')}</TableHead>+ <TableHead className="whitespace-normal">{t('columns.lastVisit')}</TableHead>+ <TableHead className="pr-6">{t('columns.redirectTo')}</TableHead>+ </TableRow>+ </TableHeader>+ <TableBody>+ {rows.map((row) => (+ <TableRow key={row.page} className="hover:bg-muted/50">+ <TableCell className="pl-6 min-w-36 max-w-[180px] whitespace-normal xl:max-w-[280px]">+ <span className="font-mono text-xs break-all line-clamp-2" title={row.page}>+ {row.page}+ </span>+ <div className="text-[10px] text-muted-foreground">+ {t(`broken.status.${row.status}`)}+ </div>+ </TableCell>+ <TableCell className="text-right font-semibold text-sm">{num(row.visits)}</TableCell>+ <TableCell className="min-w-24 whitespace-normal text-sm text-muted-foreground">+ {row.platforms.length ? row.platforms.map(getPlatformName).join(', ') : '—'}+ </TableCell>+ <TableCell className="text-right text-sm">{num(row.citations)}</TableCell>+ <TableCell className="text-sm text-muted-foreground">+ {formatRelative(row.lastVisit, tCommon)}+ </TableCell>+ <TableCell className="pr-6 min-w-36 max-w-[200px] whitespace-normal xl:max-w-[360px]">+ {row.target ? (+ <>+ <span+ className="font-mono text-xs text-muted-foreground break-all line-clamp-2"+ title={row.target}+ >+ → {row.target}+ </span>+ {row.targetReason && (+ <div className="text-[10px] text-muted-foreground">+ {t(`broken.reason.${row.targetReason}`)}+ </div>+ )}+ </>+ ) : (+ <>+ <span className="text-sm text-muted-foreground">—</span>+ {row.targetReason === 'query' && (+ <div className="text-[10px] text-muted-foreground">+ {t('broken.reason.query')}+ </div>+ )}+ </>+ )}+ </TableCell>+ </TableRow>+ ))}+ </TableBody>+ </Table>+ );+}+export function PagesCard({brandId,dateWindow,@@ -99,6 +178,7 @@ export function PagesCard({domain,analyticsMissing,actions,+ brokenActions,}: {brandId: string;dateWindow: TrafficDateWindow;@@ -110,6 +190,8 @@ export function PagesCard({analyticsMissing: boolean;/** The section's Excel / CSV buttons (AT-015). */actions?: ReactNode;+ /** Broken AI links' own Excel / CSV: the redirect list (AT-012). */+ brokenActions?: ReactNode;}) {const t = useTranslations('traffic.pageList');const tReason = useTranslations('traffic.pageOpportunities');@@ -155,6 +237,8 @@ export function PagesCard({// Another tab's rows don't fit this tab's columns: wait for its own.const loading = !loaded || loaded.tab !== tab;const rows = loading ? [] : (result?.rows ?? []);+ const brokenRows = loading ? [] : (result?.brokenLinks ?? []);+ const shown = tab === 'brokenAiLinks' ? brokenRows.length : rows.length;const total = result?.counts[tab] ?? 0;const signal = valueSignal(rows);@@ -284,7 +368,7 @@ export function PagesCard({<Badge variant="outline" className="text-xs">{rangeLabel}</Badge>- {actions}+ {tab === 'brokenAiLinks' ? brokenActions : actions}</div></div><p className="text-xs text-muted-foreground">{t('subtitle')}</p>@@ -311,8 +395,10 @@ export function PagesCard({</div>) : !result ? (<p className="px-6 py-4 text-sm text-muted-foreground">{t('failed')}</p>- ) : rows.length === 0 ? (+ ) : shown === 0 ? (<p className="px-6 py-4 text-sm text-muted-foreground">{t(`tabs.${tab}.empty`)}</p>+ ) : tab === 'brokenAiLinks' ? (+ <BrokenLinksTable rows={brokenRows} />) : (<Table><TableHeader>@@ -486,9 +572,9 @@ export function PagesCard({<div className="flex flex-wrap items-center justify-between gap-2 px-6 py-3 text-xs text-muted-foreground"><span className="flex flex-wrap items-center gap-x-3 gap-y-1"><span>- {t('shown', { shown: rows.length, total })} · {t('sameWindow')}+ {t('shown', { shown, total })} · {t('sameWindow')}</span>- {rows.length < total && (+ {shown < total && (<buttontype="button"disabled={stale}diff --git a/web/src/app/[locale]/(dashboard)/dashboard/traffic/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/traffic/page.tsxindex 50702fa73..b8d21d28f 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/traffic/page.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/traffic/page.tsx@@ -629,6 +629,7 @@ function TrafficPageContent({ brand }: { brand: Brand }) {domain={primaryDomain}analyticsMissing={gaMissing}actions={<TableExport exporter={exporter} section="pageStatus" />}+ brokenActions={<TableExport exporter={exporter} section="brokenLinks" />}/>);diff --git a/web/src/app/api/traffic/export/route.test.ts b/web/src/app/api/traffic/export/route.test.tsindex 26b8b0f70..e4bfed518 100644--- a/web/src/app/api/traffic/export/route.test.ts+++ b/web/src/app/api/traffic/export/route.test.ts@@ -36,6 +36,7 @@ const loaders = vi.hoisted(() => ({getTrafficGooglePages: vi.fn(),getUntrackedGoogleQueries: vi.fn(),getTrafficBots: vi.fn(),+ getTrafficBrokenLinks: vi.fn(),}));vi.mock('@/lib/actions/traffic', () => loaders);@@ -114,6 +115,7 @@ beforeEach(() => {rows: [],});loaders.getTrafficBots.mockResolvedValue(null);+ loaders.getTrafficBrokenLinks.mockResolvedValue({ count: 0, rows: [] });loaders.getUntrackedGoogleQueries.mockResolvedValue({lastDay: '2026-10-17',since: '2026-10-11',@@ -158,6 +160,7 @@ describe('GET /api/traffic/export', () => {'Top converting pages','Answers that bring buyers','Pages by status',+ 'Broken AI links','Is AI taking Google clicks','Untracked Google queries','AI bots',@@ -198,6 +201,7 @@ describe('GET /api/traffic/export', () => {'top-converting-pages.csv','answers-that-bring-buyers.csv','pages-by-status.csv',+ 'broken-ai-links.csv','is-ai-taking-google-clicks.csv','untracked-google-queries.csv','ai-bots.csv',@@ -246,6 +250,38 @@ describe('GET /api/traffic/export', () => {expect(loaders.getTrafficSources).not.toHaveBeenCalled();});+ it('reads every broken AI link for the redirect list (AT-012)', async () => {+ loaders.getTrafficBrokenLinks.mockResolvedValue({+ count: 1,+ rows: [+ {+ page: '/guides/old-roast',+ visits: 2,+ platforms: ['chatgpt.com'],+ citations: 5,+ lastVisit: '2026-10-18T12:00:00Z',+ status: '404',+ target: '/guides',+ targetReason: 'parent',+ },+ ],+ });+ const res = await get({ format: 'csv', section: 'brokenLinks' });++ expect(res.headers.get('Content-Disposition')).toContain('acme-coffee-broken-ai-links-');+ const csv = await csvOf(res);+ expect(csv.text.split('\n')).toEqual([+ 'From,To,Redirect type,Why this page,AI visits,Top platforms,Citations,Last visit (UTC),What the visitor got',+ '/guides/old-roast,/guides,301,The page above it,2,ChatGPT,5,2026-10-18 12:00,Not found (404)',+ ]);+ expect(loaders.getTrafficBrokenLinks).toHaveBeenCalledWith(+ BRAND,+ { dateFrom: undefined, dateTo: undefined },+ { limit: TRAFFIC_EXPORT_MAX_ROWS },+ );+ expect(loaders.getTrafficPages).not.toHaveBeenCalled();+ });+it('reads every row of where AI visitors convert for its tables (AT-014)', async () => {const res = await get({ format: 'csv', section: 'buyerAnswers' });diff --git a/web/src/app/api/traffic/export/route.ts b/web/src/app/api/traffic/export/route.tsindex a8a4597ec..a4366bf6e 100644--- a/web/src/app/api/traffic/export/route.ts+++ b/web/src/app/api/traffic/export/route.ts@@ -5,6 +5,7 @@ import { z } from 'zod';import messages from '../../../../../messages/en.json';import { createClient } from '@/lib/supabase/server';import {+ getTrafficBrokenLinks,getTrafficConversions,getTrafficGooglePages,getTrafficPages,@@ -105,6 +106,7 @@ export async function GET(req: Request) {googlePages,googleQueries,bots,+ brokenLinks,] = await Promise.all([needs('kpis', 'platforms', 'pages')? getTrafficSummary(q.brandId, window, { topPages: Number.MAX_SAFE_INTEGER })@@ -141,6 +143,10 @@ export async function GET(req: Request) {needs('bots', 'botPages')? getTrafficBots(q.brandId, window, { limit: TRAFFIC_EXPORT_MAX_ROWS }).catch(() => null): undefined,+ // Every broken AI link, as the redirect list (AT-012).+ needs('brokenLinks')+ ? getTrafficBrokenLinks(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 });@@ -164,6 +170,7 @@ export async function GET(req: Request) {googlePages,googleQueries,bots,+ brokenLinks,sources,};diff --git a/web/src/lib/actions/traffic-broken-links.test.ts b/web/src/lib/actions/traffic-broken-links.test.tsnew file mode 100644index 000000000..8270d9c77--- /dev/null+++ b/web/src/lib/actions/traffic-broken-links.test.ts@@ -0,0 +1,186 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++/**+ * AT-012 (spec §3.5): Broken AI links read ai_traffic_broken_links (00186)+ * over the page's range, and the Pages section counts them with its tabs.+ */++const rpc = vi.fn();+vi.mock('@/lib/supabase/server', () => ({+ createClient: async () => ({ rpc }),+}));+vi.mock('@/config/api', () => ({ API_BASE_URL: 'http://api.test' }));++import { getTrafficBrokenLinks, getTrafficPages } from './traffic';++const BRAND = '33333333-3333-3333-3333-333333333333';+const WEEK = { dateFrom: '2026-10-12T09:00:00.000Z', dateTo: '2026-10-19T09:00:00.000Z' };++const link = {+ page: '/guides/espresso-machine-for-beginner',+ visits: '3',+ platforms: ['chatgpt.com', 'perplexity.ai'],+ citations: 14,+ last_visit: '2026-10-19T08:27:11.697849+00:00',+ status: '404',+ target: '/guides/espresso-machines-for-beginners',+ target_reason: 'similar',+};++beforeEach(() => rpc.mockReset());++describe('getTrafficBrokenLinks', () => {+ it('reads each missing page with its visits and suggested redirect', async () => {+ rpc.mockResolvedValue({+ data: {+ count: 3,+ rows: [+ link,+ {+ page: '/',+ visits: 1,+ platforms: null,+ citations: null,+ last_visit: '2026-10-18T12:00:00+00:00',+ status: 'title',+ target: null,+ target_reason: null,+ },+ {+ page: '/index.php',+ visits: 2,+ platforms: ['chatgpt.com'],+ citations: 0,+ last_visit: '2026-10-18T10:00:00+00:00',+ status: '404',+ target: null,+ target_reason: 'query',+ },+ ],+ },+ error: null,+ });++ const result = await getTrafficBrokenLinks(BRAND, WEEK, { limit: 20 });++ expect(rpc).toHaveBeenCalledWith('ai_traffic_broken_links', {+ p_brand_id: BRAND,+ p_from: WEEK.dateFrom,+ p_to: expect.stringMatching(/^2026-10-19T/),+ p_limit: 20,+ p_offset: 0,+ });+ expect(result).toEqual({+ count: 3,+ rows: [+ {+ page: '/guides/espresso-machine-for-beginner',+ visits: 3,+ platforms: ['chatgpt.com', 'perplexity.ai'],+ citations: 14,+ lastVisit: '2026-10-19T08:27:11.697849+00:00',+ status: '404',+ target: '/guides/espresso-machines-for-beginners',+ targetReason: 'similar',+ },+ {+ page: '/',+ visits: 1,+ platforms: [],+ citations: 0,+ lastVisit: '2026-10-18T12:00:00+00:00',+ status: 'title',+ target: null,+ targetReason: null,+ },+ // A route on the page: no redirect, and the reason says why.+ {+ page: '/index.php',+ visits: 2,+ platforms: ['chatgpt.com'],+ citations: 0,+ lastVisit: '2026-10-18T10:00:00+00:00',+ status: '404',+ target: null,+ targetReason: 'query',+ },+ ],+ });+ });++ it('throws on a failed read', async () => {+ rpc.mockResolvedValue({ data: null, error: { message: 'permission denied' } });+ await expect(getTrafficBrokenLinks(BRAND, WEEK)).rejects.toThrow('permission denied');+ });+});++describe('getTrafficPages: Broken AI links', () => {+ const answer = (name: string) =>+ name === 'ai_traffic_pages'+ ? {+ data: {+ counts: {+ all: 14,+ no_ai_traffic: 3,+ cited_not_clicked: 1,+ traffic_no_prompt: 2,+ broken_ai_links: 6,+ },+ rows: [],+ },+ error: null,+ }+ : { data: { count: 6, rows: [link] }, error: null };++ it('counts the broken links with the other tabs, and reads none of their rows elsewhere', async () => {+ rpc.mockImplementation(async (name: string) => answer(name));++ const result = await getTrafficPages(BRAND, WEEK, { tab: 'noAiTraffic', limit: 10 });++ expect(result.counts).toEqual({+ noAiTraffic: 3,+ citedNotClicked: 1,+ trafficNoPrompt: 2,+ brokenAiLinks: 6,+ all: 14,+ });+ expect(rpc).toHaveBeenCalledWith(+ 'ai_traffic_pages',+ expect.objectContaining({ p_tab: 'no_ai_traffic', p_limit: 10 }),+ );+ // The count comes with the other tabs': the broken list isn't read for it.+ expect(rpc).not.toHaveBeenCalledWith('ai_traffic_broken_links', expect.anything());+ });++ it('keeps the card up when the broken list fails on its own tab', async () => {+ vi.spyOn(console, 'error').mockImplementation(() => {});+ rpc.mockImplementation(async (name: string) =>+ name === 'ai_traffic_pages' ? answer(name) : { data: null, error: { message: 'timeout' } },+ );+ const result = await getTrafficPages(BRAND, WEEK, { tab: 'brokenAiLinks', limit: 10 });+ expect(result.counts.brokenAiLinks).toBe(6);+ expect(result.brokenLinks).toEqual([]);+ });++ it('reads the broken links as the tab, and only the counts of the rest', async () => {+ rpc.mockImplementation(async (name: string) => answer(name));++ const result = await getTrafficPages(BRAND, WEEK, {+ tab: 'brokenAiLinks',+ limit: 20,+ offset: 10,+ });++ expect(result.brokenLinks.map((row) => row.page)).toEqual([+ '/guides/espresso-machine-for-beginner',+ ]);+ expect(rpc).toHaveBeenCalledWith(+ 'ai_traffic_pages',+ expect.objectContaining({ p_tab: 'all', p_limit: 0, p_offset: 0 }),+ );+ expect(rpc).toHaveBeenCalledWith(+ 'ai_traffic_broken_links',+ expect.objectContaining({ p_limit: 20, p_offset: 10 }),+ );+ });+});diff --git a/web/src/lib/actions/traffic-pages.test.ts b/web/src/lib/actions/traffic-pages.test.tsindex c9e87f815..2db1c3c31 100644--- a/web/src/lib/actions/traffic-pages.test.ts+++ b/web/src/lib/actions/traffic-pages.test.ts@@ -49,7 +49,7 @@ describe('getTrafficPages', () => {// All time has no start; ten rows unless asked for more.await getTrafficPages('b1', undefined, { tab: 'all' });- expect(rpc).toHaveBeenLastCalledWith('ai_traffic_pages', {+ expect(rpc).toHaveBeenCalledWith('ai_traffic_pages', {p_brand_id: 'b1',p_from: null,p_to: null,@@ -60,9 +60,20 @@ describe('getTrafficPages', () => {});it('gives every tab its count and each row in the card’s terms', async () => {- rpc.mockResolvedValue({+ // Broken AI links are counted on their own (AT-012).+ const brokenLinks = { data: { count: 2, rows: [] }, error: null };+ rpc.mockImplementation(async (name: string) =>+ name === 'ai_traffic_pages' ? pages : brokenLinks,+ );+ const pages = {data: {- counts: { all: 14, no_ai_traffic: 8, cited_not_clicked: 1, traffic_no_prompt: 5 },+ counts: {+ all: 14,+ no_ai_traffic: 8,+ cited_not_clicked: 1,+ traffic_no_prompt: 5,+ broken_ai_links: 2,+ },rows: [{page: '/subscriptions',@@ -78,16 +89,17 @@ describe('getTrafficPages', () => {revenue: '129.50',engagement_seconds: 5400,in_sitemap: true,+ broken: true,value_rank: 2,},{ page: '/menu', ai_platforms: null, in_sitemap: false, value_rank: null },],},error: null,- });+ };expect(await getTrafficPages('b1', undefined, { tab: 'all' })).toEqual({- counts: { noAiTraffic: 8, citedNotClicked: 1, trafficNoPrompt: 5, all: 14 },+ counts: { noAiTraffic: 8, citedNotClicked: 1, trafficNoPrompt: 5, brokenAiLinks: 2, all: 14 },rows: [{page: '/subscriptions',@@ -103,6 +115,7 @@ describe('getTrafficPages', () => {revenue: 129.5,engagementSeconds: 5400,inSitemap: true,+ broken: true,valueRank: 2,},{@@ -119,9 +132,11 @@ describe('getTrafficPages', () => {revenue: 0,engagementSeconds: 0,inSitemap: false,+ broken: false,valueRank: null,},],+ brokenLinks: [],});});@@ -129,8 +144,9 @@ describe('getTrafficPages', () => {// 00178 answers null rather than another brand's pages.rpc.mockResolvedValue({ data: null, error: null });expect(await getTrafficPages('b1', undefined, { tab: 'noAiTraffic' })).toEqual({- counts: { noAiTraffic: 0, citedNotClicked: 0, trafficNoPrompt: 0, all: 0 },+ counts: { noAiTraffic: 0, citedNotClicked: 0, trafficNoPrompt: 0, brokenAiLinks: 0, all: 0 },rows: [],+ brokenLinks: [],});});diff --git a/web/src/lib/actions/traffic.ts b/web/src/lib/actions/traffic.tsindex a201378c2..4d84dbb3e 100644--- a/web/src/lib/actions/traffic.ts+++ b/web/src/lib/actions/traffic.ts@@ -515,11 +515,18 @@ export async function getTrafficVisitsForExport(* The Pages section (AT-006, spec §3.5; 00178): the brand's pages in the* page's range with their AI visits (the snippet or Analytics, day by day),* how often tracked answers cite them, the prompts pointing to them and- * their traffic from every channel, sorted into three work lists.+ * their traffic from every channel, sorted into three work lists; and+ * Broken AI links, the pages AI visitors landed on that don't exist+ * (AT-012, 00186).*/-export type TrafficPagesTab = 'noAiTraffic' | 'citedNotClicked' | 'trafficNoPrompt' | 'all';--const TAB_PARAM: Record<TrafficPagesTab, string> = {+export type TrafficPagesTab =+ | 'noAiTraffic'+ | 'citedNotClicked'+ | 'trafficNoPrompt'+ | 'brokenAiLinks'+ | 'all';++const TAB_PARAM: Record<Exclude<TrafficPagesTab, 'brokenAiLinks'>, string> = {noAiTraffic: 'no_ai_traffic',citedNotClicked: 'cited_not_clicked',trafficNoPrompt: 'traffic_no_prompt',@@ -546,32 +553,124 @@ export interface TrafficPageRow {revenue: number;engagementSeconds: number;inSitemap: boolean;+ /** AI visitors found the page missing: it is on Broken AI links (AT-012). */+ broken: boolean;/** The page's place on the site by value, among pages with traffic. */valueRank: number | null;}-export interface TrafficPages {- counts: Record<TrafficPagesTab, number>;- rows: TrafficPageRow[];+/**+ * A page AI visitors landed on that doesn't exist (AT-012): the browser said+ * 404 or 410, or, giving no status (Safari), the page's title said "not+ * found" and the nightly page read never found it working.+ */+export interface TrafficBrokenLink {+ /** The path, as normalize_page_path writes it. */+ page: string;+ /** AI-referred visits that landed on it in the range. */+ visits: number;+ /** The assistants that sent the most visits, most first (up to three). */+ platforms: string[];+ /** Times tracked AI answers cited it in the range. */+ citations: number;+ lastVisit: string;+ /** The status the browser reported, or 'title' when only the title said so. */+ status: '404' | '410' | 'title';+ /** Where to redirect it: a working page; none for the home page itself or a route on a page. */+ target: string | null;+ /**+ * Why that page: the most path words in common, the nearest page above it,+ * or home; `query` when every missing address has a query (no redirect).+ */+ targetReason: 'similar' | 'parent' | 'home' | 'query' | null;}-export async function getTrafficPages(+export interface TrafficBrokenLinks {+ count: number;+ rows: TrafficBrokenLink[];+}++const BROKEN_STATUSES = new Set(['404', '410', 'title']);+const TARGET_REASONS = new Set(['similar', 'parent', 'home', 'query']);++/** Broken AI links (AT-012, spec §3.5; 00186), from the snippet alone. */+export async function getTrafficBrokenLinks(brandId: string,window: TrafficDateWindow | undefined,- opts: { tab: TrafficPagesTab; limit?: number; offset?: number },-): Promise<TrafficPages> {+ opts: { limit?: number; offset?: number } = {},+): Promise<TrafficBrokenLinks> {const supabase = await createClient();const { from, to } = resolveTrafficWindow(window);- const { data, error } = await supabase.rpc('ai_traffic_pages', {+ const { data, error } = await supabase.rpc('ai_traffic_broken_links', {p_brand_id: brandId,p_from: from ?? null,p_to: to ?? null,- p_tab: TAB_PARAM[opts.tab],p_limit: opts.limit ?? 10,p_offset: opts.offset ?? 0,});if (error) throw new Error(error.message);+ const raw = (data ?? {}) as { count?: number; rows?: Record<string, unknown>[] };+ return {+ count: Number(raw.count) || 0,+ rows: (raw.rows ?? []).map((row) => {+ const status = String(row.status);+ const reason = row.target_reason as string | null;+ return {+ page: row.page as string,+ visits: Number(row.visits) || 0,+ platforms: (row.platforms as string[] | null) ?? [],+ citations: Number(row.citations) || 0,+ lastVisit: row.last_visit as string,+ status: (BROKEN_STATUSES.has(status) ? status : '404') as TrafficBrokenLink['status'],+ target: (row.target as string | null) ?? null,+ targetReason:+ reason && TARGET_REASONS.has(reason)+ ? (reason as TrafficBrokenLink['targetReason'])+ : null,+ };+ }),+ };+}++export interface TrafficPages {+ counts: Record<TrafficPagesTab, number>;+ rows: TrafficPageRow[];+ /** The Broken AI links tab's rows, read when it is the tab asked for. */+ brokenLinks: TrafficBrokenLink[];+}++export async function getTrafficPages(+ brandId: string,+ window: TrafficDateWindow | undefined,+ opts: { tab: TrafficPagesTab; limit?: number; offset?: number },+): Promise<TrafficPages> {+ const supabase = await createClient();+ const { from, to } = resolveTrafficWindow(window);+ const broken = opts.tab === 'brokenAiLinks';+ // Every tab is counted here; only the asked tab's rows are read, Broken AI+ // links' from its own list, which is left empty when it can't be read.+ const [{ data, error }, brokenLinks] = await Promise.all([+ supabase.rpc('ai_traffic_pages', {+ p_brand_id: brandId,+ p_from: from ?? null,+ p_to: to ?? null,+ p_tab: broken ? 'all' : TAB_PARAM[opts.tab as keyof typeof TAB_PARAM],+ p_limit: broken ? 0 : (opts.limit ?? 10),+ p_offset: broken ? 0 : (opts.offset ?? 0),+ }),+ broken+ ? getTrafficBrokenLinks(brandId, window, {+ limit: opts.limit ?? 10,+ offset: opts.offset ?? 0,+ }).catch((err) => {+ console.error('Failed to load the broken AI links:', err);+ return null;+ })+ : null,+ ]);+ if (error) throw new Error(error.message);+const raw = (data ?? {}) as { counts?: Record<string, number>; rows?: Record<string, unknown>[] };const counts = raw.counts ?? {};return {@@ -579,8 +678,10 @@ export async function getTrafficPages(noAiTraffic: Number(counts.no_ai_traffic) || 0,citedNotClicked: Number(counts.cited_not_clicked) || 0,trafficNoPrompt: Number(counts.traffic_no_prompt) || 0,+ brokenAiLinks: Number(counts.broken_ai_links) || 0,all: Number(counts.all) || 0,},+ brokenLinks: brokenLinks?.rows ?? [],rows: (raw.rows ?? []).map((row) => ({page: row.page as string,aiVisits: Number(row.ai_visits) || 0,@@ -595,6 +696,7 @@ export async function getTrafficPages(revenue: Number(row.revenue) || 0,engagementSeconds: Number(row.engagement_seconds) || 0,inSitemap: Boolean(row.in_sitemap),+ broken: Boolean(row.broken),valueRank:row.value_rank === null || row.value_rank === undefined ? null : Number(row.value_rank),})),diff --git a/web/src/lib/traffic-export.test.ts b/web/src/lib/traffic-export.test.tsindex 8dabbdbdc..98e07601d 100644--- a/web/src/lib/traffic-export.test.ts+++ b/web/src/lib/traffic-export.test.ts@@ -80,7 +80,7 @@ const data = (over: Partial<TrafficExportData> = {}): TrafficExportData => ({},],pageStatus: {- counts: { noAiTraffic: 1, citedNotClicked: 1, trafficNoPrompt: 1, all: 3 },+ counts: { noAiTraffic: 1, citedNotClicked: 1, trafficNoPrompt: 1, brokenAiLinks: 0, all: 3 },rows: [{page: '/shop/beans',@@ -96,6 +96,7 @@ const data = (over: Partial<TrafficExportData> = {}): TrafficExportData => ({revenue: 0,engagementSeconds: 0,inSitemap: true,+ broken: false,valueRank: 3,},{@@ -112,9 +113,11 @@ const data = (over: Partial<TrafficExportData> = {}): TrafficExportData => ({revenue: 0,engagementSeconds: 0,inSitemap: false,+ broken: false,valueRank: null,},],+ brokenLinks: [],},sources: {snippetLastVisitAt: new Date().toISOString(),@@ -242,6 +245,22 @@ describe('trafficTable', () => {]);});+ it('labels a page AI visitors found missing as a broken link (AT-012)', () => {+ const pageStatus = data().pageStatus!;+ const table = trafficTable(+ 'pageStatus',+ data({+ pageStatus: {+ ...pageStatus,+ rows: [{ ...pageStatus.rows[1], page: '/old-menu', broken: true }],+ },+ }),+ label,+ );+ expect(table.rows[0][0]).toBe('/old-menu');+ expect(table.rows[0].at(-1)).toBe('Broken AI links');+ });+it('says there are no pages, never an empty sheet', () => {const table = trafficTable('pageStatus', data({ pageStatus: null }), label);expect(table.rows).toEqual([['No pages in this range']]);@@ -531,6 +550,93 @@ describe('trafficTable: AI bots (AT-011)', () => {});});+describe('trafficTable: Broken AI links (AT-012)', () => {+ const brokenLinks = (): NonNullable<TrafficExportData['brokenLinks']> => ({+ count: 2,+ rows: [+ {+ page: '/guides/espresso-machine-for-beginner',+ visits: 3,+ platforms: ['chatgpt.com', 'perplexity.ai'],+ citations: 14,+ lastVisit: '2026-10-19T08:27:11.697Z',+ status: '404',+ target: '/guides/espresso-machines-for-beginners',+ targetReason: 'similar',+ },+ {+ page: '/',+ visits: 1,+ platforms: [],+ citations: 0,+ lastVisit: '2026-10-18T12:00:00Z',+ status: 'title',+ target: null,+ targetReason: null,+ },+ {+ page: '/index.php',+ visits: 2,+ platforms: ['chatgpt.com'],+ citations: 0,+ lastVisit: '2026-10-18T10:00:00Z',+ status: '404',+ target: null,+ targetReason: 'query',+ },+ ],+ });++ it('is a redirect list: from, to and a 301, with why that page and what visitors got', () => {+ expect(trafficTable('brokenLinks', data({ brokenLinks: brokenLinks() }), label)).toEqual({+ headers: [+ 'From',+ 'To',+ 'Redirect type',+ 'Why this page',+ 'AI visits',+ 'Top platforms',+ 'Citations',+ 'Last visit (UTC)',+ 'What the visitor got',+ ],+ rows: [+ [+ '/guides/espresso-machine-for-beginner',+ '/guides/espresso-machines-for-beginners',+ 301,+ 'Similar page',+ 3,+ 'ChatGPT, Perplexity',+ 14,+ '2026-10-19 08:27',+ 'Not found (404)',+ ],+ // Nothing to redirect the home page to.+ ['/', null, null, null, 1, null, 0, '2026-10-18 12:00', 'Page title says not found'],+ // Nor a route on a page: a 301 from its path would move the whole route.+ [+ '/index.php',+ null,+ null,+ 'Missing addresses have a query: no redirect suggested',+ 2,+ 'ChatGPT',+ 0,+ '2026-10-18 10:00',+ 'Not found (404)',+ ],+ ],+ });+ });++ it('says no AI visitor landed on a missing page, never an empty sheet', () => {+ expect(trafficTable('brokenLinks', data({ brokenLinks: null }), label).rows).toEqual([+ ['No AI visitor landed on a missing page in this range'],+ ]);+ });+});+describe('filtersTable', () => {it('records the range, the filters, the sources and when', () => {const table = filtersTable(@@ -564,6 +670,7 @@ describe('trafficWorkbook', () => {'Top converting pages','Answers that bring buyers','Pages by status',+ 'Broken AI links','Is AI taking Google clicks','Untracked Google queries','AI bots',diff --git a/web/src/lib/traffic-export.ts b/web/src/lib/traffic-export.tsindex cba06da52..62def94fd 100644--- a/web/src/lib/traffic-export.ts+++ b/web/src/lib/traffic-export.ts@@ -12,6 +12,7 @@ import type { Sheet } from 'write-excel-file/node';import type {TrafficConversions,TrafficBots,+ TrafficBrokenLinks,TrafficGooglePages,TrafficLog,TrafficUntrackedQueries,@@ -42,6 +43,7 @@ export const TRAFFIC_EXPORT_SECTIONS = ['convertingPages','buyerAnswers','pageStatus',+ 'brokenLinks','googlePages','googleQueries','bots',@@ -58,6 +60,7 @@ export const TRAFFIC_TABLE_SECTIONS = ['convertingPages','buyerAnswers','pageStatus',+ 'brokenLinks','googlePages','googleQueries','bots',@@ -86,6 +89,8 @@ export interface TrafficExportData {visits?: TrafficLog[];/** Every page of the Pages section, with the lists it is on (AT-006). */pageStatus?: TrafficPages | null;+ /** Broken AI links, every page, with its suggested redirect (AT-012). */+ brokenLinks?: TrafficBrokenLinks | null;/** Where AI visitors convert, every row (AT-014). */conversions?: TrafficConversions | null;/** Is AI taking Google clicks?, every page (AT-008). */@@ -261,6 +266,42 @@ function pageStatus(data: TrafficExportData, label: ExportLabel): ExportTable {};}+/**+ * Broken AI links (AT-012) as a redirect list: each missing page and the+ * working page to send it to, ready for a 301 rule, with why that page and+ * what AI visitors got.+ */+function brokenLinks(data: TrafficExportData, label: ExportLabel): ExportTable {+ const rows = data.brokenLinks?.rows ?? [];+ if (rows.length === 0) {+ return { headers: [label('columns.note')], rows: [[label('notes.noBrokenLinks')]] };+ }+ return {+ headers: [+ label('columns.redirectFrom'),+ label('columns.redirectTo'),+ label('columns.redirectType'),+ label('columns.redirectReason'),+ label('columns.aiVisits'),+ label('columns.topPlatforms'),+ label('columns.citations'),+ label('columns.lastVisit'),+ label('columns.landingStatus'),+ ],+ rows: rows.map((row) => [+ row.page,+ row.target,+ row.target ? 301 : null,+ row.targetReason ? label(`redirectReasons.${row.targetReason}`) : null,+ row.visits,+ row.platforms.map(getPlatformName).join(', ') || null,+ row.citations,+ utcMinute(row.lastVisit),+ label(`brokenStatuses.${row.status}`),+ ]),+ };+}+/** A row's rate as a number, or why the page shows none (§5.4). */function rateCells(figures: { aiVisits: number; keyEvents: number; keyEventSessions: number },@@ -499,6 +540,8 @@ export function trafficTable(return buyerAnswers(data, label);case 'pageStatus':return pageStatus(data, label);+ case 'brokenLinks':+ return brokenLinks(data, label);case 'googlePages':return googlePages(data, label);case 'googleQueries':diff --git a/web/src/lib/traffic-pages.test.ts b/web/src/lib/traffic-pages.test.tsindex 5d46cad9a..5e4cb209a 100644--- a/web/src/lib/traffic-pages.test.ts+++ b/web/src/lib/traffic-pages.test.ts@@ -1,13 +1,14 @@import { describe, expect, it } from 'vitest';import { CITED_NOT_CLICKED_MIN, citationState, pageStatuses, pageUrl } from './traffic-pages';-/** The Pages section's lists, as 00178 sorts pages into them (AT-006). */+/** The Pages section's lists, as 00178 and 00186 sort pages into them (AT-006, AT-012). */const row = (over: Partial<Parameters<typeof pageStatuses>[0]> = {}) => ({aiVisits: 0,citations: 0,sessions: 0,inSitemap: false,+ broken: false,...over,});@@ -32,6 +33,13 @@ describe('pageStatuses', () => {expect(pageStatuses(row({ aiVisits: 3 }))).toEqual(['trafficNoPrompt']);expect(pageStatuses(row({ aiVisits: 3, citations: 1 }))).toEqual([]);});++ it('puts a page AI visitors found missing on Broken AI links, not Traffic, no prompt (AT-012)', () => {+ expect(pageStatuses(row({ aiVisits: 3, broken: true }))).toEqual(['brokenAiLinks']);+ expect(pageStatuses(row({ aiVisits: 3, citations: 9, broken: true }))).toEqual([+ 'brokenAiLinks',+ ]);+ });});describe('citationState', () => {diff --git a/web/src/lib/traffic-pages.ts b/web/src/lib/traffic-pages.tsindex ce48607c5..6b389ecee 100644--- a/web/src/lib/traffic-pages.ts+++ b/web/src/lib/traffic-pages.ts@@ -1,6 +1,7 @@/*** The Pages section's work lists (AT-006, spec §3.5), as 00178 sorts pages- * into them, for the card's labels and the export's Status column.+ * into them (and 00186 Broken AI links, AT-012), for the card's labels and+ * the export's Status column.*/import type { TrafficPageRow } from '@/lib/actions/traffic';@@ -8,17 +9,23 @@ import type { TrafficPageRow } from '@/lib/actions/traffic';/** Cited at least this often with no AI visit: answers show the page, nobody clicks. */export const CITED_NOT_CLICKED_MIN = 20;-export type TrafficPageStatus = 'noAiTraffic' | 'citedNotClicked' | 'trafficNoPrompt';+export type TrafficPageStatus =+ | 'noAiTraffic'+ | 'citedNotClicked'+ | 'trafficNoPrompt'+ | 'brokenAiLinks';/** The lists a page is on, in the tabs' order; none for a page doing fine. */export function pageStatuses(- row: Pick<TrafficPageRow, 'aiVisits' | 'citations' | 'sessions' | 'inSitemap'>,+ row: Pick<TrafficPageRow, 'aiVisits' | 'citations' | 'sessions' | 'inSitemap' | 'broken'>,): TrafficPageStatus[] {const statuses: TrafficPageStatus[] = [];if (row.aiVisits === 0 && (row.sessions > 0 || row.inSitemap)) statuses.push('noAiTraffic');if (row.aiVisits === 0 && row.citations >= CITED_NOT_CLICKED_MIN)statuses.push('citedNotClicked');- if (row.aiVisits > 0 && row.citations === 0) statuses.push('trafficNoPrompt');+ // A missing page needs a redirect, not prompts.+ if (row.aiVisits > 0 && row.citations === 0 && !row.broken) statuses.push('trafficNoPrompt');+ if (row.broken) statuses.push('brokenAiLinks');return statuses;}diff --git a/web/src/types/supabase.ts b/web/src/types/supabase.tsindex 352d72b00..b999e7b1a 100644--- a/web/src/types/supabase.ts+++ b/web/src/types/supabase.ts@@ -2920,6 +2920,16 @@ export type Database = {}Returns: Json}+ ai_traffic_broken_links: {+ Args: {+ p_brand_id: string+ p_from: string | null+ p_limit?: number+ p_offset?: number+ p_to: string | null+ }+ Returns: Json+ }ai_traffic_conversions: {Args: {p_brand_id: string