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(insights): use the same denominator for brand and competitor averages (#478)

Gökhan Geyik77f414d9825d
diff --git a/web/src/lib/actions/tracking.ts b/web/src/lib/actions/tracking.ts
index 660668856..8e58d9840 100644
--- a/web/src/lib/actions/tracking.ts
+++ b/web/src/lib/actions/tracking.ts
@@ -1244,13 +1244,23 @@ export async function getCompetitorComparison(
? prevWin.brand_sum_visibility / prevWin.brand_row_count
: null;
+ // Competitor averages use the SAME denominator as the brand's — every
+ // filtered result row, not just the rows where the competitor happened to
+ // appear. A competitor absent from a response scores 0 for it, exactly
+ // like the brand does. Dividing by the appearance count instead graded
+ // competitors only on their best runs and could rank a rarely-seen
+ // competitor above a frequently-mentioned brand.
const curCompAvg = new Map<string, number>();
- for (const c of curWin?.by_competitor ?? []) {
- if (c.row_count > 0) curCompAvg.set(c.competitor_id, c.sum_visibility / c.row_count);
+ if (curWin && curWin.brand_row_count > 0) {
+ for (const c of curWin.by_competitor) {
+ curCompAvg.set(c.competitor_id, c.sum_visibility / curWin.brand_row_count);
+ }
}
const prevCompAvg = new Map<string, number>();
- for (const c of prevWin?.by_competitor ?? []) {
- if (c.row_count > 0) prevCompAvg.set(c.competitor_id, c.sum_visibility / c.row_count);
+ if (prevWin && prevWin.brand_row_count > 0) {
+ for (const c of prevWin.by_competitor) {
+ prevCompAvg.set(c.competitor_id, c.sum_visibility / prevWin.brand_row_count);
+ }
}
const brandName = (brand?.name as string) ?? 'Your Brand';
@@ -1284,7 +1294,9 @@ export async function getCompetitorComparison(
];
for (const c of agg.by_competitor) {
- const avg = c.row_count > 0 ? Math.round(c.sum_visibility / c.row_count) : 0;
+ // Same-denominator rule as the window averages above (agg.brand_row_count
+ // is guaranteed > 0 by the early return).
+ const avg = Math.round(c.sum_visibility / agg.brand_row_count);
entries.push({
name: competitorDisplayName(c.name, c.competitor_id),
avgVisibilityScore: avg,
@@ -1342,7 +1354,12 @@ export async function getCompetitorComparison(
row[brandName] = bp && bp.count > 0 ? Math.round(bp.totalScore / bp.count) : 0;
for (const [compName, pm] of compByProvider) {
const cp = pm.get(provider);
- row[compName] = cp && cp.count > 0 ? Math.round(cp.totalScore / cp.count) : 0;
+ // Same-denominator rule: divide by the provider bucket's total run
+ // count (the brand's, which spans every filtered row), not the
+ // competitor's appearance count. Competitor rows are a subset of the
+ // brand's, so bp always exists when cp does.
+ const denom = bp?.count ?? 0;
+ row[compName] = cp && denom > 0 ? Math.round(cp.totalScore / denom) : 0;
}
return row;
});
diff --git a/web/src/lib/mcp/data.ts b/web/src/lib/mcp/data.ts
index 7657a9afe..46a5a73f5 100644
--- a/web/src/lib/mcp/data.ts
+++ b/web/src/lib/mcp/data.ts
@@ -998,7 +998,11 @@ export async function getCompetitorComparisonFor(
const competitors = comp.by_competitor.map((c) => ({
competitor_id: c.competitor_id,
name: c.name && c.name.trim() !== '' ? c.name : c.competitor_id,
- avg_visibility_score: c.row_count > 0 ? Math.round(Number(c.sum_visibility) / c.row_count) : 0,
+ // Same denominator as the brand's average (every filtered row, absence
+ // counts as 0) — mirrors getCompetitorComparison in actions/tracking.ts
+ // so the MCP surface ranks brands the same way the Insights page does.
+ avg_visibility_score:
+ comp.brand_row_count > 0 ? Math.round(Number(c.sum_visibility) / comp.brand_row_count) : 0,
total_mentions: Number(c.total_mentions),
total_citations: Number(c.total_citations),
appearance_count: c.row_count,