Skip to content

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

fix(reports): show Low sample instead of a % change on a handful of AI visits (#1169)

Cihanf7dee3c3b4af
diff --git a/web/messages/en.json b/web/messages/en.json
index ff47ed9ce..dd9e87faa 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -3945,6 +3945,9 @@
},
"topicPerformance": "Topic Performance",
"aiTraffic": "AI Traffic",
+ "aiTrafficNew": "new",
+ "aiTrafficLowSample": "Low sample",
+ "aiTrafficLowSampleHint": "Fewer than 30 visits in this period or the one before, so a % change would mislead.",
"shoppingVisibility": "Shopping Visibility",
"auditScore": "Site Audit Score",
"auditedOn": "Audited on",
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/reports/[id]/_report-pdf.tsx b/web/src/app/[locale]/(dashboard)/dashboard/reports/[id]/_report-pdf.tsx
index c9383d608..d00c39f24 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/reports/[id]/_report-pdf.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/reports/[id]/_report-pdf.tsx
@@ -544,6 +544,13 @@ export function ReportPdfDocument({ report }: { report: Report }) {
</Text>
<View style={{ marginBottom: 6 }}>
<DeltaText value={payload.aiTraffic.change} />
+ {/* No % on a handful of visits, as on the AI Traffic page. */}
+ {payload.aiTraffic.changeNote === 'new' && (
+ <Text style={[styles.delta, { color: GREEN }]}>new</Text>
+ )}
+ {payload.aiTraffic.changeNote === 'lowSample' && (
+ <Text style={[styles.delta, { color: MUTED }]}>Low sample</Text>
+ )}
</View>
</View>
{payload.aiTraffic.platformBreakdown.map((p) => (
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/reports/[id]/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/reports/[id]/page.tsx
index 0ad8f2b57..5bc0d1873 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/reports/[id]/page.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/reports/[id]/page.tsx
@@ -649,6 +649,20 @@ export default function ReportDetailPage() {
{t('aiTraffic')}
<span className="text-2xl font-bold">{payload.aiTraffic.totalVisits}</span>
<Delta value={payload.aiTraffic.change} />
+ {/* No % on a handful of visits, as on the AI Traffic page. */}
+ {payload.aiTraffic.changeNote === 'new' && (
+ <span className="text-xs font-medium text-emerald-600 dark:text-emerald-400">
+ {t('aiTrafficNew')}
+ </span>
+ )}
+ {payload.aiTraffic.changeNote === 'lowSample' && (
+ <span
+ className="text-xs font-medium text-muted-foreground"
+ title={t('aiTrafficLowSampleHint')}
+ >
+ {t('aiTrafficLowSample')}
+ </span>
+ )}
</CardTitle>
</CardHeader>
<CardContent className="grid gap-6 sm:grid-cols-2">
diff --git a/web/src/lib/actions/report-traffic-change.test.ts b/web/src/lib/actions/report-traffic-change.test.ts
new file mode 100644
index 000000000..800d48c1a
--- /dev/null
+++ b/web/src/lib/actions/report-traffic-change.test.ts
@@ -0,0 +1,119 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+/**
+ * The report's AI traffic change follows the AI Traffic page's rule
+ * (AT-003): a % only when both periods have at least 30 visits. A report
+ * showed "+62400%" for 1,250 visits against 2 the week before.
+ */
+
+type Call = [string, ...unknown[]];
+
+let current = 0;
+let previous = 0;
+const inserted: Record<string, unknown>[] = [];
+
+function answer(table: string, calls: Call[]) {
+ if (table === 'ai_traffic_logs') {
+ const select = calls.find(([m]) => m === 'select');
+ if ((select?.[2] as { head?: boolean } | undefined)?.head) {
+ // The previous period ends where the report starts (an exclusive end).
+ return {
+ data: null,
+ count: calls.some(([m]) => m === 'lt') ? previous : current,
+ error: null,
+ };
+ }
+ return { data: [], error: null };
+ }
+ if (table === 'brands')
+ return { data: { id: 'b1', name: 'Acme', shopping_mode_enabled: false }, error: null };
+ if (table === 'reports') return { data: { id: 'report-1' }, error: null };
+ return { data: [], error: null };
+}
+
+function query(table: string) {
+ const calls: Call[] = [];
+ const builder: Record<string, unknown> = {};
+ for (const method of [
+ 'select',
+ 'eq',
+ 'is',
+ 'in',
+ 'gte',
+ 'lte',
+ 'lt',
+ 'order',
+ 'range',
+ 'limit',
+ ]) {
+ builder[method] = (...args: unknown[]) => {
+ calls.push([method, ...args]);
+ return builder;
+ };
+ }
+ builder.insert = (row: Record<string, unknown>) => {
+ inserted.push(row);
+ return builder;
+ };
+ builder.single = async () => answer(table, calls);
+ builder.maybeSingle = async () => answer(table, calls);
+ builder.then = (resolve: (a: unknown) => unknown) => resolve(answer(table, calls));
+ return builder;
+}
+
+const client = {
+ from: (table: string) => query(table),
+ rpc: async () => ({ data: [], error: null }),
+ auth: {
+ getSession: async () => ({ data: { session: { access_token: 'tok', user: { id: 'u1' } } } }),
+ },
+};
+vi.mock('@/lib/supabase/server', () => ({ createClient: async () => client }));
+vi.mock('next/cache', () => ({ revalidatePath: vi.fn() }));
+vi.mock('@/config/api', () => ({ API_BASE_URL: 'http://api.test' }));
+
+const { createReport } = await import('./reports');
+
+beforeEach(() => {
+ inserted.length = 0;
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async () => new Response(JSON.stringify({ summary: '' }), { status: 200 })),
+ );
+});
+
+async function aiTraffic(now: number, before: number) {
+ current = now;
+ previous = before;
+ inserted.length = 0;
+ await createReport('b1', {
+ dateFrom: '2026-10-11T00:00:00.000Z',
+ dateTo: '2026-10-17T23:59:59.999Z',
+ sections: ['aiTraffic'],
+ });
+ return (inserted[0].payload as { aiTraffic: Record<string, unknown> }).aiTraffic;
+}
+
+describe("the report's AI traffic change", () => {
+ it('gives a % once both periods have 30 visits', async () => {
+ expect(await aiTraffic(45, 30)).toMatchObject({ totalVisits: 45, change: 50 });
+ expect((await aiTraffic(45, 30)).changeNote).toBeUndefined();
+ });
+
+ it('says Low sample instead of a % below 30 visits on either side', async () => {
+ // The live case: 1,250 visits against 2 read "+62400%".
+ expect(await aiTraffic(1250, 2)).toMatchObject({ change: null, changeNote: 'lowSample' });
+ expect(await aiTraffic(12, 40)).toMatchObject({ change: null, changeNote: 'lowSample' });
+ });
+
+ it('says new when there were no visits before, and nothing with none at all', async () => {
+ expect(await aiTraffic(5, 0)).toMatchObject({ change: null, changeNote: 'new' });
+ const none = await aiTraffic(0, 0);
+ expect(none.change).toBeNull();
+ expect(none.changeNote).toBeUndefined();
+ });
+
+ it('keeps a change that rounds to nothing as 0%', async () => {
+ expect(await aiTraffic(1000, 1000)).toMatchObject({ change: 0 });
+ });
+});
diff --git a/web/src/lib/actions/reports.ts b/web/src/lib/actions/reports.ts
index be44f2b67..6ee46a03a 100644
--- a/web/src/lib/actions/reports.ts
+++ b/web/src/lib/actions/reports.ts
@@ -22,6 +22,7 @@ import { getShoppingKpis } from '@/lib/actions/shopping';
import { type SourceCategory } from '@/lib/citations/classify';
import { stripEntityMarkup } from '@/lib/entity-markup';
import { countTrafficLogs, scanTrafficLogs } from '@/lib/traffic-scan';
+import { LOW_SAMPLE_VISITS } from '@/lib/traffic-change';
import {
getReportTemplate,
ALL_REPORT_SECTIONS,
@@ -129,8 +130,17 @@ export interface ReportTopicPerf {
export interface ReportAiTraffic {
totalVisits: number;
- /** Percent change vs the previous window; null when the previous window had no visits. */
+ /**
+ * Percent change vs the previous window, or null: no visits before, or too
+ * few on either side for a % to mean anything (see changeNote).
+ */
change: number | null;
+ /**
+ * Why there's no %, as the AI Traffic page says it (AT-003): 'new' — visits
+ * now and none before; 'lowSample' — fewer than 30 visits in this period
+ * or the one before. Absent on reports created before it.
+ */
+ changeNote?: 'new' | 'lowSample';
platformBreakdown: { platform: string; visits: number }[];
topPages: { url: string; visits: number }[];
}
@@ -503,9 +513,18 @@ async function getTrafficSnapshot(
if (u) byPage.set(u, (byPage.get(u) ?? 0) + 1);
}
+ // A % on a handful of visits says more about chance than about a trend:
+ // 1,250 visits against 2 the week before read "+62400%". As on the AI
+ // Traffic page, a % only when both periods have LOW_SAMPLE_VISITS.
+ const lowSample = totalVisits < LOW_SAMPLE_VISITS || prevVisits < LOW_SAMPLE_VISITS;
+ const changeNote: ReportAiTraffic['changeNote'] =
+ prevVisits === 0 ? (totalVisits > 0 ? 'new' : undefined) : lowSample ? 'lowSample' : undefined;
+
return {
totalVisits,
- change: prevVisits > 0 ? round1(((totalVisits - prevVisits) / prevVisits) * 100) : null,
+ change:
+ prevVisits > 0 && !lowSample ? round1(((totalVisits - prevVisits) / prevVisits) * 100) : null,
+ ...(changeNote ? { changeNote } : {}),
platformBreakdown: [...byPlatform.entries()]
.map(([platform, visits]) => ({ platform, visits }))
.sort((a, b) => b.visits - a.visits)