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(audit): add pages closest to competitor-won prompts to Site Audit Opportunities (#1137)
Cihancc393699530c
diff --git a/server/src/lib/audit/opportunities.js b/server/src/lib/audit/opportunities.jsindex 3db53bcfb..670fdbda3 100644--- a/server/src/lib/audit/opportunities.js+++ b/server/src/lib/audit/opportunities.js@@ -8,12 +8,16 @@* valuable pages AI engines send no visitors to (refreshed nightly).* - Site pages (site_pages: the sitemap, GA and cited pages): pages never* audited, or whose last audit is older than auditMaxAgeDays.+ * - Citations: prompts where a competitor is cited and the brand never is+ * (audit_competitor_cited_prompts), each through the brand page closest+ * to it (the prompt's target URL, else the best site_pages match).** Pinned pages always show, first. Excluded pages are left out of the rows* and listed apart, so they can be put back.*/import { isExcludedPath } from '../page-paths.js';+import { candidatePages } from '../page-matching.js';import { pageKey } from './history.js';const DAY_MS = 24 * 60 * 60 * 1000;@@ -30,6 +34,55 @@ export function opportunityKey(url) {const pathOf = (url) => pageKey(url)?.path ?? '/';+/**+ * The brand pages closest to the prompts a competitor is cited on and the+ * brand never is: the prompt's target URLs when it has any (a customer said+ * which page answers it), else the site page that best matches its text.+ * Prompts with no such page are left out: there is nothing to audit.+ *+ * @param {object} input+ * @param {Array<{ prompt_id: string, prompt_text: string, own_results: number|string, competitor_domains: string[] }>} input.prompts+ * audit_competitor_cited_prompts rows+ * @param {Array<{ prompt_id: string, url: string }>} input.targetUrls prompt_target_urls of those prompts+ * @param {Array<{ url: string, title?: string|null, h1?: string|null, description?: string|null }>} input.sitePages+ * @param {Map<string, string>} input.competitorNames competitor host → name+ * @returns {Array<{ url: string, prompts: number, competitors: string[] }>}+ */+export function competitorGapPages({+ prompts = [],+ targetUrls = [],+ sitePages = [],+ competitorNames,+}) {+ const targets = new Map();+ for (const t of targetUrls) {+ if (!targets.has(t.prompt_id)) targets.set(t.prompt_id, []);+ targets.get(t.prompt_id).push(t.url);+ }+ const pages = new Map();+ for (const prompt of prompts) {+ if (Number(prompt.own_results) > 0) continue;+ const urls =+ targets.get(prompt.prompt_id) ??+ candidatePages(prompt.prompt_text, sitePages, 1).map((page) => page.url);+ for (const url of urls) {+ const key = opportunityKey(url);+ if (!key) continue;+ if (!pages.has(key)) pages.set(key, { url, prompts: new Set(), competitors: new Set() });+ const page = pages.get(key);+ page.prompts.add(prompt.prompt_id);+ for (const domain of prompt.competitor_domains ?? []) {+ page.competitors.add(competitorNames?.get(domain) ?? domain);+ }+ }+ }+ return [...pages.values()].map((page) => ({+ url: page.url,+ prompts: page.prompts.size,+ competitors: [...page.competitors].sort((a, b) => a.localeCompare(b)),+ }));+}+/*** @param {object} input* @param {string|null} input.primaryHost the brand's primary host (normStoredDomain)@@ -38,6 +91,8 @@ const pathOf = (url) => pageKey(url)?.path ?? '/';* @param {Array<{ url: string, title: string|null, ga_sessions: number|null, ai_citations: number|null }>} input.sitePages* @param {Array<{ key: string, url: string, score: number|null, auditId: string, lastAuditedAt: string, status: string }>} input.auditPages* buildAuditPages output+ * @param {Array<{ url: string, prompts: number, competitors: string[] }>} [input.competitorPages]+ * competitorGapPages output* @param {Array<{ page_key: string, url: string, state: 'pinned'|'excluded' }>} input.prefs* @param {number} input.staleDays an audit older than this no longer counts* @param {number} [input.now]@@ -47,6 +102,7 @@ export function buildOpportunities({gaPages = [],sitePages = [],auditPages = [],+ competitorPages = [],prefs = [],staleDays,now = Date.now(),@@ -109,6 +165,16 @@ export function buildOpportunities({}}+ for (const gap of competitorPages) {+ const page = pageFor(gap.url);+ if (!page) continue;+ page.reasons.push({+ type: 'competitor_cited',+ prompts: gap.prompts,+ competitors: gap.competitors,+ });+ }+for (const pref of prefs) {const page = pref.state === 'pinned' ? pageFor(pref.url) : null;if (page) page.pinned = true;@@ -150,16 +216,20 @@ export function buildOpportunities({const gaPercentile = (row) => row.reasons.find((r) => r.type === 'no_ai_traffic')?.percentile ?? -1;const neverAudited = (row) => row.reasons.some((r) => r.type === 'never_audited');+const competitorPrompts = (row) =>+ row.reasons.find((r) => r.type === 'competitor_cited')?.prompts ?? 0;/*** Until the priority score (SA-005): pinned first, then GA's most valuable- * pages without AI traffic, then pages never audited before stale ones, then- * the most cited and visited.+ * pages without AI traffic, then the pages closest to the most prompts a+ * competitor wins, then pages never audited before stale ones, then the most+ * cited and visited.*/export function compareOpportunities(a, b) {return (Number(b.pinned) - Number(a.pinned) ||gaPercentile(b) - gaPercentile(a) ||+ competitorPrompts(b) - competitorPrompts(a) ||Number(neverAudited(b)) - Number(neverAudited(a)) ||(b.citations ?? 0) - (a.citations ?? 0) ||(b.sessions ?? 0) - (a.sessions ?? 0) ||diff --git a/server/src/lib/audit/opportunities.test.js b/server/src/lib/audit/opportunities.test.jsindex ef3cf7cfc..bed2afdb2 100644--- a/server/src/lib/audit/opportunities.test.js+++ b/server/src/lib/audit/opportunities.test.js@@ -3,6 +3,7 @@ import {aiVisitsByPage,buildOpportunities,compareOpportunities,+ competitorGapPages,opportunityKey,} from './opportunities.js';@@ -208,3 +209,111 @@ describe('aiVisitsByPage', () => {expect(aiVisitsByPage(null).size).toBe(0);});});++describe('competitorGapPages', () => {+ const sitePages = [+ {+ url: 'https://acme.com/subscriptions/pricing',+ title: 'Coffee subscription plans and pricing',+ h1: 'Plans & Pricing',+ description: 'Compare our coffee subscription plans.',+ },+ {+ url: 'https://acme.com/guides/espresso',+ title: 'Espresso machines for beginners',+ h1: 'Espresso machines',+ description: 'How to choose a home espresso machine.',+ },+ ];+ const names = new Map([+ ['crema.example', 'Crema Club'],+ ['north.example', 'Northline'],+ ]);+ const prompt = (id, text, over = {}) => ({+ prompt_id: id,+ prompt_text: text,+ own_results: 0,+ competitor_domains: ['crema.example'],+ ...over,+ });++ it("maps each prompt a competitor wins to the brand's closest page", () => {+ const pages = competitorGapPages({+ prompts: [+ prompt('p1', 'best coffee subscription plans pricing'),+ prompt('p2', 'cheapest coffee subscription plans', {+ competitor_domains: ['north.example', 'crema.example'],+ }),+ prompt('p3', 'which espresso machine for beginners at home'),+ ],+ sitePages,+ competitorNames: names,+ });+ expect(pages).toEqual([+ {+ url: 'https://acme.com/subscriptions/pricing',+ prompts: 2,+ competitors: ['Crema Club', 'Northline'],+ },+ { url: 'https://acme.com/guides/espresso', prompts: 1, competitors: ['Crema Club'] },+ ]);+ });++ it("prefers a prompt's target URLs, and skips prompts the brand is cited on or no page matches", () => {+ const pages = competitorGapPages({+ prompts: [+ prompt('p1', 'best coffee subscription plans pricing'),+ prompt('p2', 'espresso machine for beginners', { own_results: '2' }),+ prompt('p3', 'gift ideas for tea lovers'),+ ],+ targetUrls: [{ prompt_id: 'p1', url: 'https://acme.com/gifts' }],+ sitePages,+ competitorNames: names,+ });+ expect(pages).toEqual([+ { url: 'https://acme.com/gifts', prompts: 1, competitors: ['Crema Club'] },+ ]);+ });++ it('names an unknown competitor domain by its domain', () => {+ const [page] = competitorGapPages({+ prompts: [+ prompt('p1', 'coffee subscription plans', { competitor_domains: ['other.example'] }),+ ],+ sitePages,+ competitorNames: names,+ });+ expect(page.competitors).toEqual(['other.example']);+ });+});++describe('buildOpportunities with competitor-cited pages', () => {+ it('adds the reason to the page and ranks it after GA value, before never audited', () => {+ const { rows } = build({+ gaPages: [+ { landing_page: '/ga', value_signal: 'revenue', value_percentile: 90, sessions: 10 },+ ],+ sitePages: [+ { url: 'https://acme.com/never', title: null, ga_sessions: 999, ai_citations: 9 },+ { url: 'https://acme.com/pricing', title: 'Pricing', ga_sessions: 1, ai_citations: 0 },+ ],+ competitorPages: [+ { url: 'https://acme.com/pricing', prompts: 3, competitors: ['Crema Club'] },+ { url: 'https://acme.com/fresh', prompts: 1, competitors: ['Northline'] },+ ],+ auditPages: [audit('https://acme.com/fresh', 0.8, 2)],+ });+ expect(rows.map((r) => [r.path, r.reasons.map((x) => x.type)])).toEqual([+ ['/ga', ['no_ai_traffic']],+ ['/pricing', ['never_audited', 'competitor_cited']],+ // Audited two days ago, still listed: the competitor gap is the reason.+ ['/fresh', ['competitor_cited']],+ ['/never', ['never_audited']],+ ]);+ expect(rows[1].reasons[1]).toEqual({+ type: 'competitor_cited',+ prompts: 3,+ competitors: ['Crema Club'],+ });+ });+});diff --git a/server/src/routes/audits-opportunities.test.js b/server/src/routes/audits-opportunities.test.jsindex 74f382f1e..172cab4f8 100644--- a/server/src/routes/audits-opportunities.test.js+++ b/server/src/routes/audits-opportunities.test.js@@ -73,6 +73,12 @@ async function startApp() {from: (t) => query(t),rpc: async (name, args) => {rpcCalls.push({ name, args });+ if (name === 'audit_competitor_cited_prompts') {+ if (tables.competitorPromptsMissing) {+ return { data: null, error: { code: 'PGRST202', message: 'not found' } };+ }+ return { data: tables.competitorPrompts ?? [], error: null };+ }return {data: {analytics: [{ page: args.p_pages[0], platform: 'chatgpt', cur_sessions: 7 }],@@ -184,7 +190,7 @@ describe('GET /api/audits/opportunities', () => {expect(body.staleDays).toBe(90);// AI visits over the last 30 days, for the listed pages.- const [{ name, args }] = rpcCalls;+ const { name, args } = rpcCalls.find((c) => c.name === 'ae_page_traffic_evidence');expect(name).toBe('ae_page_traffic_evidence');expect(args.p_pages).toEqual(['https://acme.com/pricing']);});@@ -207,7 +213,71 @@ describe('GET /api/audits/opportunities', () => {tables.brands = [{ id: brandId, ga_property_id: null }];const body = await (await fetch(`${baseUrl}/opportunities?brandId=${brandId}`)).json();expect(body.opportunities[0].aiVisits).toBeNull();- expect(rpcCalls).toEqual([]);+ expect(rpcCalls.map((c) => c.name)).not.toContain('ae_page_traffic_evidence');+ });++ it("adds the pages closest to the prompts a competitor wins and the brand doesn't", async () => {+ tables.competitors = [+ { brand_id: brandId, name: 'Crema Club', domain: 'https://www.Crema.example/' },+ { brand_id: brandId, name: 'No domain', domain: '' },+ ];+ tables.site_pages.push({+ brand_id: brandId,+ url: 'https://acme.com/subscriptions',+ title: 'Coffee subscription plans',+ h1: 'Coffee subscriptions',+ description: null,+ ga_sessions: 5,+ ai_citations: 0,+ });+ tables.competitorPrompts = [+ {+ prompt_id: 'p1',+ prompt_text: 'best coffee subscription plans',+ results: 6,+ competitor_results: 4,+ own_results: 0,+ competitor_domains: ['crema.example'],+ },+ // Cited on this one too: not a gap.+ {+ prompt_id: 'p2',+ prompt_text: 'coffee subscription plans for offices',+ results: 6,+ competitor_results: 2,+ own_results: 1,+ competitor_domains: ['crema.example'],+ },+ ];+ tables.prompt_target_urls = [];++ const body = await (await fetch(`${baseUrl}/opportunities?brandId=${brandId}`)).json();+ const page = body.opportunities.find((o) => o.path === '/subscriptions');+ expect(page.reasons).toContainEqual({+ type: 'competitor_cited',+ prompts: 1,+ competitors: ['Crema Club'],+ });++ const { args } = rpcCalls.find((c) => c.name === 'audit_competitor_cited_prompts');+ expect(args).toMatchObject({+ p_brand_id: brandId,+ p_own_domains: ['acme.com'],+ p_competitor_domains: ['crema.example'],+ });+ });++ it('lists the other sources on a database without migration 00166', async () => {+ tables.competitors = [{ brand_id: brandId, name: 'Crema Club', domain: 'crema.example' }];+ tables.competitorPromptsMissing = true;+ const res = await fetch(`${baseUrl}/opportunities?brandId=${brandId}`);+ expect(res.status).toBe(200);+ expect((await res.json()).opportunities.map((o) => o.path)).toEqual(['/pricing']);+ });++ it('asks for no citations without competitor domains', async () => {+ await fetch(`${baseUrl}/opportunities?brandId=${brandId}`);+ expect(rpcCalls.map((c) => c.name)).not.toContain('audit_competitor_cited_prompts');});it('needs a brand', async () => {diff --git a/server/src/routes/audits.js b/server/src/routes/audits.jsindex a007a8e27..088e7dc07 100644--- a/server/src/routes/audits.js+++ b/server/src/routes/audits.js@@ -34,7 +34,12 @@ import {} from '../lib/audit/history.js';import { readAllRpcRows } from '../lib/rpc-pages.js';import { AUTHORITY_CATEGORIES, buildOverview, primaryPages } from '../lib/audit/overview.js';-import { aiVisitsByPage, buildOpportunities, opportunityKey } from '../lib/audit/opportunities.js';+import {+ aiVisitsByPage,+ buildOpportunities,+ competitorGapPages,+ opportunityKey,+} from '../lib/audit/opportunities.js';import { ENGINE_THRESHOLDS } from '../config/action-engine.js';import { runSignals } from '../lib/audit/engine.js';import { evaluateLlmSignals } from '../lib/audit/llm-signals.js';@@ -553,9 +558,59 @@ router.get('/overview', async (req, res) => {}});-/** The window the Opportunities inbox counts AI visits over. */+/** The window the Opportunities inbox counts AI visits and citations over. */const OPPORTUNITY_VISITS_DAYS = 30;+/**+ * Pages closest to the prompts a competitor is cited on and the brand never+ * is, over the last 30 days (empty without competitor domains).+ */+async function competitorOpportunityPages(brandId, sitePages) {+ const [{ data: competitors, error: compErr }, { data: domains, error: domErr }] =+ await Promise.all([+ supabaseAdmin.from('competitors').select('name, domain').eq('brand_id', brandId),+ supabaseAdmin.from('brand_domains').select('domain').eq('brand_id', brandId),+ ]);+ if (compErr) throw compErr;+ if (domErr) throw domErr;++ const competitorNames = new Map();+ for (const c of competitors ?? []) {+ const host = normStoredDomain(c.domain);+ if (host) competitorNames.set(host, c.name);+ }+ const ownHosts = (domains ?? []).map((d) => normStoredDomain(d.domain)).filter(Boolean);+ if (competitorNames.size === 0 || ownHosts.length === 0) return [];++ const { data: prompts, error } = await supabaseAdmin.rpc('audit_competitor_cited_prompts', {+ p_brand_id: brandId,+ p_since: new Date(Date.now() - OPPORTUNITY_VISITS_DAYS * 24 * 60 * 60 * 1000).toISOString(),+ p_own_domains: ownHosts,+ p_competitor_domains: [...competitorNames.keys()],+ });+ // A server deployed before migration 00166 lists the other sources without this one.+ if (error?.code === 'PGRST202') return [];+ if (error) throw error;++ const gaps = (prompts ?? []).filter((p) => Number(p.own_results) === 0);+ if (gaps.length === 0) return [];+ const { data: targetUrls, error: targetErr } = await supabaseAdmin+ .from('prompt_target_urls')+ .select('prompt_id, url')+ .in(+ 'prompt_id',+ gaps.map((p) => p.prompt_id),+ );+ if (targetErr) throw targetErr;++ return competitorGapPages({+ prompts: gaps,+ targetUrls: targetUrls ?? [],+ sitePages,+ competitorNames,+ });+}+const isoDay = (ms) => new Date(ms).toISOString().slice(0, 10);/**@@ -617,7 +672,7 @@ router.get('/opportunities', async (req, res) => {),supabaseAdmin.from('site_pages')- .select('url, title, ga_sessions, ai_citations')+ .select('url, title, h1, description, ga_sessions, ai_citations').eq('brand_id', brandId),readBrandAudits(brandId),supabaseAdmin@@ -627,11 +682,13 @@ router.get('/opportunities', async (req, res) => {]);for (const read of [gaPages, sitePages, audits, prefs]) if (read.error) throw read.error;+ const competitorPages = await competitorOpportunityPages(brandId, sitePages.data ?? []);const { rows, excluded } = buildOpportunities({primaryHost: normStoredDomain(primary),gaPages: gaPages.data ?? [],sitePages: sitePages.data ?? [],auditPages: buildAuditPages(audits.data),+ competitorPages,prefs: prefs.data ?? [],staleDays: ENGINE_THRESHOLDS.detection.auditMaxAgeDays,});diff --git a/supabase/migrations/00166_audit_competitor_cited_prompts.sql b/supabase/migrations/00166_audit_competitor_cited_prompts.sqlnew file mode 100644index 000000000..090e4b7c3--- /dev/null+++ b/supabase/migrations/00166_audit_competitor_cited_prompts.sql@@ -0,0 +1,109 @@+-- Site Audit Opportunities (SA-004): prompts where a competitor is cited and+-- the brand isn't.+--+-- The Opportunities inbox lists, for each such prompt, the brand page closest+-- to it, as a page worth auditing. This read gives the prompts: per active+-- prompt of the brand, over the window, how many answers cited a competitor's+-- domain and how many cited one of the brand's own, and which competitor+-- domains were cited. Only prompts with a competitor citation are returned;+-- the caller keeps the ones the brand was never cited on.+--+-- Domains match as the citation tables store them (citation_urls.domain:+-- lowercase, no "www."), a domain or any of its subdomains. The caller passes+-- the brand's and its competitors' hosts the same way.+--+-- The window's cited URLs are matched once each, through their domain's+-- suffixes ("blog.example.com", "example.com", "com"), and only the+-- citations of matched URLs are joined to their answers. Matching every+-- citation against every domain with LIKE took 2.7 s warm and 13.5 s cold+-- for 30 days of the largest brand (796k citations, 118k distinct URLs);+-- this takes 1.4 s warm, with the same rows. plpgsql with force_custom_plan+-- so it is planned with the brand and window in hand.++create or replace function public.audit_competitor_cited_prompts(+ p_brand_id uuid,+ p_since timestamptz,+ p_own_domains text[],+ p_competitor_domains text[]+) returns table (+ prompt_id uuid,+ prompt_text text,+ results bigint,+ competitor_results bigint,+ own_results bigint,+ competitor_domains text[]+)+language plpgsql+stable+set search_path = public+set work_mem = '64MB'+set plan_cache_mode = 'force_custom_plan'+as $$+begin+ return query+ with sought as (+ select d as domain, true as own from unnest(p_own_domains) d+ union all+ select d, false from unnest(p_competitor_domains) d+ ),+ urls as (+ select distinct prc.url_id+ from prompt_result_citations prc+ where prc.brand_id = p_brand_id+ and prc.created_at >= p_since+ ),+ -- Each cited URL on the brand's or a competitor's domain, or a subdomain.+ matched as (+ select u.url_id, s.domain, s.own+ from urls u+ join citation_urls cu on cu.id = u.url_id+ cross join lateral (+ select array_to_string((string_to_array(cu.domain, '.'))[i:], '.') as suffix+ from generate_series(1, cardinality(string_to_array(cu.domain, '.'))) i+ ) sx+ join sought s on s.domain = sx.suffix+ ),+ answers as (+ select pr.id, pr.prompt_id+ from prompt_results pr+ join prompts p on p.id = pr.prompt_id and p.is_active+ where pr.brand_id = p_brand_id+ and pr.created_at >= p_since+ ),+ -- The answers' citations of matched URLs, with the domain each matched.+ hits as (+ select a.prompt_id, a.id, m.domain, m.own+ from prompt_result_citations prc+ join matched m on m.url_id = prc.url_id+ join answers a on a.id = prc.prompt_result_id+ where prc.brand_id = p_brand_id+ and prc.created_at >= p_since+ ),+ answer_counts as (+ select a.prompt_id, count(*) as results from answers a group by a.prompt_id+ ),+ per_prompt as (+ select h.prompt_id,+ count(distinct h.id) filter (where not h.own) as competitor_results,+ count(distinct h.id) filter (where h.own) as own_results,+ array_agg(distinct h.domain order by h.domain) filter (where not h.own) as competitor_domains+ from hits h+ group by h.prompt_id+ )+ select pp.prompt_id, p.text, ac.results,+ pp.competitor_results, pp.own_results, pp.competitor_domains+ from per_prompt pp+ join answer_counts ac on ac.prompt_id = pp.prompt_id+ join prompts p on p.id = pp.prompt_id+ where pp.competitor_results > 0+ order by pp.competitor_results desc, pp.prompt_id;+end;+$$;++-- Service-role only, like the other server reads.+revoke all on function public.audit_competitor_cited_prompts(uuid, timestamptz, text[], text[])+ from public, anon, authenticated;+grant execute on function public.audit_competitor_cited_prompts(uuid, timestamptz, text[], text[])+ to service_role;++notify pgrst, 'reload schema';diff --git a/supabase/schema.sql b/supabase/schema.sqlindex 8a7bc8483..a7afdcb8f 100644--- a/supabase/schema.sql+++ b/supabase/schema.sql@@ -22342,3 +22342,116 @@ create policy site_audit_page_prefs_member_select on public.site_audit_page_prefnotify pgrst, 'reload schema';+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00166_audit_competitor_cited_prompts.sql+-- ─────────────────────────────────────────────────────────────────────────+-- Site Audit Opportunities (SA-004): prompts where a competitor is cited and+-- the brand isn't.+--+-- The Opportunities inbox lists, for each such prompt, the brand page closest+-- to it, as a page worth auditing. This read gives the prompts: per active+-- prompt of the brand, over the window, how many answers cited a competitor's+-- domain and how many cited one of the brand's own, and which competitor+-- domains were cited. Only prompts with a competitor citation are returned;+-- the caller keeps the ones the brand was never cited on.+--+-- Domains match as the citation tables store them (citation_urls.domain:+-- lowercase, no "www."), a domain or any of its subdomains. The caller passes+-- the brand's and its competitors' hosts the same way.+--+-- The window's cited URLs are matched once each, through their domain's+-- suffixes ("blog.example.com", "example.com", "com"), and only the+-- citations of matched URLs are joined to their answers. Matching every+-- citation against every domain with LIKE took 2.7 s warm and 13.5 s cold+-- for 30 days of the largest brand (796k citations, 118k distinct URLs);+-- this takes 1.4 s warm, with the same rows. plpgsql with force_custom_plan+-- so it is planned with the brand and window in hand.++create or replace function public.audit_competitor_cited_prompts(+ p_brand_id uuid,+ p_since timestamptz,+ p_own_domains text[],+ p_competitor_domains text[]+) returns table (+ prompt_id uuid,+ prompt_text text,+ results bigint,+ competitor_results bigint,+ own_results bigint,+ competitor_domains text[]+)+language plpgsql+stable+set search_path = public+set work_mem = '64MB'+set plan_cache_mode = 'force_custom_plan'+as $$+begin+ return query+ with sought as (+ select d as domain, true as own from unnest(p_own_domains) d+ union all+ select d, false from unnest(p_competitor_domains) d+ ),+ urls as (+ select distinct prc.url_id+ from prompt_result_citations prc+ where prc.brand_id = p_brand_id+ and prc.created_at >= p_since+ ),+ -- Each cited URL on the brand's or a competitor's domain, or a subdomain.+ matched as (+ select u.url_id, s.domain, s.own+ from urls u+ join citation_urls cu on cu.id = u.url_id+ cross join lateral (+ select array_to_string((string_to_array(cu.domain, '.'))[i:], '.') as suffix+ from generate_series(1, cardinality(string_to_array(cu.domain, '.'))) i+ ) sx+ join sought s on s.domain = sx.suffix+ ),+ answers as (+ select pr.id, pr.prompt_id+ from prompt_results pr+ join prompts p on p.id = pr.prompt_id and p.is_active+ where pr.brand_id = p_brand_id+ and pr.created_at >= p_since+ ),+ -- The answers' citations of matched URLs, with the domain each matched.+ hits as (+ select a.prompt_id, a.id, m.domain, m.own+ from prompt_result_citations prc+ join matched m on m.url_id = prc.url_id+ join answers a on a.id = prc.prompt_result_id+ where prc.brand_id = p_brand_id+ and prc.created_at >= p_since+ ),+ answer_counts as (+ select a.prompt_id, count(*) as results from answers a group by a.prompt_id+ ),+ per_prompt as (+ select h.prompt_id,+ count(distinct h.id) filter (where not h.own) as competitor_results,+ count(distinct h.id) filter (where h.own) as own_results,+ array_agg(distinct h.domain order by h.domain) filter (where not h.own) as competitor_domains+ from hits h+ group by h.prompt_id+ )+ select pp.prompt_id, p.text, ac.results,+ pp.competitor_results, pp.own_results, pp.competitor_domains+ from per_prompt pp+ join answer_counts ac on ac.prompt_id = pp.prompt_id+ join prompts p on p.id = pp.prompt_id+ where pp.competitor_results > 0+ order by pp.competitor_results desc, pp.prompt_id;+end;+$$;++-- Service-role only, like the other server reads.+revoke all on function public.audit_competitor_cited_prompts(uuid, timestamptz, text[], text[])+ from public, anon, authenticated;+grant execute on function public.audit_competitor_cited_prompts(uuid, timestamptz, text[], text[])+ to service_role;++notify pgrst, 'reload schema';+diff --git a/web/messages/en.json b/web/messages/en.jsonindex 1a19e819a..eca67663b 100644--- a/web/messages/en.json+++ b/web/messages/en.json@@ -2135,6 +2135,7 @@"targetedNotCited": "Targeted, not cited","neverAudited": "Never audited","staleAudit": "Audited {days} days ago",+ "competitorCited": "{name}{others, plural, =0 {} other { +#}} cited on {prompts, plural, one {# prompt} other {# prompts}}","pinned": "Pinned"},"auditSelected": "Audit selected",@@ -2150,7 +2151,7 @@"hideExcluded": "Hide","restore": "Restore","restoredToast": "{count, plural, one {# page restored} other {# pages restored}}",- "empty": "No pages to audit right now. Opportunities come from Google Analytics (valuable pages without AI traffic) and from your site's pages that were never audited or not in {days} days.",+ "empty": "No pages to audit right now. Opportunities come from Google Analytics (valuable pages without AI traffic), from prompts where a competitor is cited and you aren't, and from your site's pages that were never audited or not in {days} days.","showMore": "Show more","prefsFailed": "Couldn't update the pages"},diff --git a/web/src/app/[locale]/(dashboard)/dashboard/audit/_opportunities-table.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/audit/_opportunities-table.test.tsxindex f299587ce..9315a3cc9 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/audit/_opportunities-table.test.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/audit/_opportunities-table.test.tsx@@ -120,6 +120,25 @@ describe('OpportunitiesTable', () => {expect(rows()[1].querySelectorAll('a')[1].getAttribute('href')).toBe('/dashboard/audit/a1');});+ it('names the competitor cited on the prompts closest to a page', () => {+ render([+ opportunity({+ reasons: [+ { type: 'competitor_cited', prompts: 3, competitors: ['Crema Club', 'Northline'] },+ ],+ }),+ opportunity({+ key: 'k2',+ path: '/gifts',+ reasons: [{ type: 'competitor_cited', prompts: 1, competitors: ['Crema Club'] }],+ }),+ ]);+ expect(rows().map((r) => cells(r)[2])).toEqual([+ 'Crema Club +1 cited on 3 prompts',+ 'Crema Club cited on 1 prompt',+ ]);+ });+it('selects pages not being audited, one by one or all shown', () => {const running = opportunity({ key: 'run', path: '/run', running: true, lastAudit: null });render([opportunity(), blog, running], {diff --git a/web/src/app/[locale]/(dashboard)/dashboard/audit/_opportunities-table.tsx b/web/src/app/[locale]/(dashboard)/dashboard/audit/_opportunities-table.tsxindex d48461371..732afb67e 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/audit/_opportunities-table.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/audit/_opportunities-table.tsx@@ -52,6 +52,16 @@ function ReasonChips({ row }: { row: AuditOpportunity }) {return <Chip key="never">{t('neverAudited')}</Chip>;case 'stale_audit':return <Chip key="stale">{t('staleAudit', { days: reason.days })}</Chip>;+ case 'competitor_cited':+ return (+ <Chip key="competitor">+ {t('competitorCited', {+ name: reason.competitors[0] ?? '',+ others: Math.max(0, reason.competitors.length - 1),+ prompts: reason.prompts,+ })}+ </Chip>+ );}};return (diff --git a/web/src/lib/actions/audits.ts b/web/src/lib/actions/audits.tsindex 36a26ef2d..d258f8891 100644--- a/web/src/lib/actions/audits.ts+++ b/web/src/lib/actions/audits.ts@@ -284,7 +284,14 @@ export type OpportunityReason =citationState: 'cited' | 'targeted_not_cited' | 'not_targeted' | null;}| { type: 'never_audited' }- | { type: 'stale_audit'; days: number };+ | { type: 'stale_audit'; days: number }+ | {+ type: 'competitor_cited';+ /** Prompts a competitor is cited on and the brand never is, closest to this page. */+ prompts: number;+ /** The competitors cited on them, by name. */+ competitors: string[];+ };/** A page worth auditing (SA-004). */export interface AuditOpportunity {