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(visibility): count names that start or end outside ASCII letters (#890)

Gökhan Geyikd0b88b3ba4cb
diff --git a/server/src/lib/response-parser.js b/server/src/lib/response-parser.js
index 95e5ee857..0cd0215ff 100644
--- a/server/src/lib/response-parser.js
+++ b/server/src/lib/response-parser.js
@@ -14,14 +14,36 @@ function stripUrls(text) {
return cleaned;
}
+const WORD_CHAR = /[\p{L}\p{N}_]/u;
+// Scripts written without spaces between words (Chinese, Japanese, Thai…) and
+// Korean, whose particles attach to the noun: a name there is always flanked
+// by other letters, so a boundary check would never let it match.
+const UNSPACED_SCRIPT =
+ /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Script=Thai}\p{Script=Lao}\p{Script=Khmer}\p{Script=Myanmar}]/u;
+
+/**
+ * Case-insensitive regex matching a name as a whole word (#871).
+ *
+ * `\b` only knows ASCII word characters, so a name starting or ending with an
+ * accented letter ("Ümit") or punctuation ("C++") never matched. The boundary
+ * is now a Unicode letter/number lookaround, applied only on a side where the
+ * name ends in a word character of a space-separated script.
+ */
+function nameRegex(term, flags) {
+ const escaped = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+ const needsBoundary = (ch) => WORD_CHAR.test(ch) && !UNSPACED_SCRIPT.test(ch);
+ const chars = [...term];
+ const before = needsBoundary(chars[0]) ? '(?<![\\p{L}\\p{N}_])' : '';
+ const after = needsBoundary(chars[chars.length - 1]) ? '(?![\\p{L}\\p{N}_])' : '';
+ return new RegExp(`${before}${escaped}${after}`, `${flags}u`);
+}
+
/**
* Count case-insensitive occurrences of a term in text.
*/
function countOccurrences(text, term) {
if (!term) return 0;
- const escaped = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
- const regex = new RegExp(`\\b${escaped}\\b`, 'gi');
- return (text.match(regex) || []).length;
+ return (text.match(nameRegex(term, 'gi')) || []).length;
}
/**
@@ -91,8 +113,7 @@ export function countBrandMentions(text, brand) {
*/
function firstOccurrenceIndex(text, term) {
if (!term) return -1;
- const escaped = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
- const match = new RegExp(`\\b${escaped}\\b`, 'i').exec(text);
+ const match = nameRegex(term, 'i').exec(text);
return match ? match.index : -1;
}
diff --git a/server/src/lib/response-parser.test.js b/server/src/lib/response-parser.test.js
index 1c97b0294..3287b97ed 100644
--- a/server/src/lib/response-parser.test.js
+++ b/server/src/lib/response-parser.test.js
@@ -362,4 +362,58 @@ describe('response-parser', () => {
expect(result.mentionPosition).toBe(2);
});
});
+
+ describe('names outside ASCII word characters (#871)', () => {
+ const count = (text, brandName) => countBrandMentions(text, { brandName, domains: [] });
+
+ it('matches names that start or end with an accented letter', () => {
+ expect(count('I like Émile Studio. Émile Studio is great.', 'Émile Studio')).toBe(2);
+ expect(count('Ümit ve Eda, Ümit en çok bilinen.', 'Ümit')).toBe(2);
+ expect(count('Çınar Bankası ve Çınar Bankası şubeleri', 'Çınar Bankası')).toBe(2);
+ });
+
+ it('matches names that start or end with punctuation', () => {
+ expect(count('I like Acme!. Acme! is great.', 'Acme!')).toBe(2);
+ expect(count('I like C++. C++ is great.', 'C++')).toBe(2);
+ expect(count('Acme, Inc. and Acme, Inc. again', 'Acme, Inc.')).toBe(2);
+ });
+
+ it('still ignores a name inside a longer word, in any Latin script', () => {
+ expect(count('Acmegroup and SuperAcme are different.', 'Acme')).toBe(0);
+ expect(count('Ümitler and Tümit are different.', 'Ümit')).toBe(0);
+ expect(count('Acmeé is not Acme.', 'Acme')).toBe(1);
+ });
+
+ it('matches names in scripts written without spaces between words', () => {
+ expect(count('我推荐星云科技,星云科技的服务很好。', '星云科技')).toBe(2);
+ expect(count('하늘랩은 좋은 회사입니다. 하늘랩의 제품', '하늘랩')).toBe(2);
+ });
+
+ it('keeps the plain ASCII behaviour', () => {
+ expect(count('I like Acme. Acme is great.', 'Acme')).toBe(2);
+ expect(count("Acme's pricing beats acme-lite.", 'Acme')).toBe(2);
+ });
+
+ it('positions a non-ASCII name among its competitors', () => {
+ const text = 'Eda öne çıkıyor, ardından Ümit ve İpek geliyor.';
+ const result = computeMentionPosition(text, { brandName: 'Ümit', domains: [] }, [
+ { id: 'c1', name: 'Eda', domain: null },
+ { id: 'c2', name: 'İpek', domain: null },
+ ]);
+ expect(result.mentionPosition).toBe(2);
+ expect(result.mentionedEntityCount).toBe(3);
+ expect(result.competitorPositions.get('c2')).toBe(3);
+ });
+
+ it('counts non-ASCII competitor names in parseResponse', () => {
+ const result = parseResponse(
+ { text: 'İpek ve Eda, İpek daha ucuz.', citations: [] },
+ { brandName: 'Acme', domains: [] },
+ 'neutral',
+ [{ id: 'c1', name: 'İpek', domain: null }],
+ );
+ expect(result.competitorMentions[0].mention_count).toBe(2);
+ expect(result.competitorMentions[0].mention_position).toBe(1);
+ });
+ });
});
diff --git a/server/src/scripts/backfill-mention-counts.js b/server/src/scripts/backfill-mention-counts.js
new file mode 100644
index 000000000..515a7f01b
--- /dev/null
+++ b/server/src/scripts/backfill-mention-counts.js
@@ -0,0 +1,213 @@
+/**
+ * Recount brand and competitor mentions on historical prompt_results (#871).
+ *
+ * Names starting or ending with a character outside ASCII [A-Za-z0-9_]
+ * ("Ümit", "İpek", "Acme, Inc.", "星云科技") were never matched, so
+ * their answers were stored with zero mentions. This re-runs parseResponse
+ * over the stored answer text and rewrites what it derives:
+ *
+ * - mention_count, visibility_score, mention_position, mentioned_entity_count
+ * - mention_count, visibility_score and mention_position of each existing
+ * competitor_mentions entry whose competitor still exists
+ *
+ * Sentiment is not re-analysed: an answer that now counts as mentioning the
+ * brand keeps the 'neutral' it was stored with, because re-running the model
+ * over thousands of answers is a separate decision.
+ *
+ * Run: `node src/scripts/backfill-mention-counts.js [--brand <id>] [--dry-run]`
+ *
+ * Scope: brands of active or trialing organizations whose own name or any
+ * competitor name starts or ends outside [A-Za-z0-9_] — every other brand's
+ * counts are unchanged by the fix. --brand narrows to one brand regardless.
+ *
+ * Idempotent: rows whose recomputed values equal the stored ones are skipped.
+ * Afterwards, refresh each changed brand's rollups with
+ * `INSIGHTS_BACKFILL_BRAND=<id> node src/scripts/backfill-insights-daily.js`;
+ * the ids are printed at the end.
+ */
+import 'dotenv/config';
+import supabaseAdmin from '../config/supabase.js';
+import { parseResponse } from '../lib/response-parser.js';
+
+const BATCH_SIZE = 250;
+const BATCH_PAUSE_MS = 250;
+const CONCURRENCY = 20;
+
+const args = process.argv.slice(2);
+const dryRun = args.includes('--dry-run');
+const brandArg = args.includes('--brand') ? args[args.indexOf('--brand') + 1] : null;
+
+const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
+
+/** Retry transient network failures (connection resets on long walks). */
+async function withRetry(fn, label) {
+ for (let attempt = 1; ; attempt++) {
+ try {
+ return await fn();
+ } catch (err) {
+ if (attempt >= 4) throw err;
+ console.warn(` retry ${attempt}/3 after error in ${label}: ${err.message}`);
+ await sleep(attempt * 2000);
+ }
+ }
+}
+
+/** A name the old ASCII `\b` matcher could not match. */
+const isAffectedName = (name) => Boolean(name) && !/^\w[\s\S]*\w$|^\w$/.test(name);
+
+async function brandsInScope() {
+ let query = supabaseAdmin.from('brands').select('id, name, organization_id');
+ if (brandArg) {
+ query = query.eq('id', brandArg);
+ } else {
+ const { data: orgs, error: orgErr } = await supabaseAdmin
+ .from('organizations')
+ .select('id')
+ .in('subscription_status', ['active', 'trialing']);
+ if (orgErr) throw new Error(orgErr.message);
+ query = query.in(
+ 'organization_id',
+ (orgs ?? []).map((o) => o.id),
+ );
+ }
+ const { data: brands, error } = await query;
+ if (error) throw new Error(error.message);
+ return brands ?? [];
+}
+
+async function brandContext(brand) {
+ const [{ data: domains }, { data: competitors }] = await Promise.all([
+ supabaseAdmin.from('brand_domains').select('domain').eq('brand_id', brand.id),
+ supabaseAdmin.from('competitors').select('id, name, domain').eq('brand_id', brand.id),
+ ]);
+ return {
+ brandInfo: { brandName: brand.name, domains: (domains ?? []).map((d) => d.domain) },
+ competitors: competitors ?? [],
+ };
+}
+
+/** The row's new values, or null when nothing changes. */
+function recount(row, brandInfo, competitors) {
+ const metrics = parseResponse(
+ { text: row.response ?? '', citations: Array.isArray(row.citations) ? row.citations : [] },
+ brandInfo,
+ row.sentiment ?? 'neutral',
+ competitors,
+ );
+ const fresh = new Map(metrics.competitorMentions.map((m) => [m.competitor_id, m]));
+ const entries = Array.isArray(row.competitor_mentions) ? row.competitor_mentions : [];
+ const competitorMentions = entries.map((entry) => {
+ const m = fresh.get(entry.competitor_id);
+ if (!m) return entry;
+ return {
+ ...entry,
+ mention_count: m.mention_count,
+ visibility_score: m.visibility_score,
+ mention_position: m.mention_position,
+ };
+ });
+
+ const next = {
+ mention_count: metrics.mentionCount,
+ visibility_score: metrics.visibilityScore,
+ mention_position: metrics.mentionPosition,
+ mentioned_entity_count: metrics.mentionedEntityCount,
+ competitor_mentions: competitorMentions,
+ };
+ const unchanged =
+ next.mention_count === row.mention_count &&
+ next.visibility_score === row.visibility_score &&
+ next.mention_position === row.mention_position &&
+ next.mentioned_entity_count === row.mentioned_entity_count &&
+ JSON.stringify(competitorMentions) === JSON.stringify(entries);
+ return unchanged ? null : next;
+}
+
+async function backfillBrand(brand) {
+ const { brandInfo, competitors } = await brandContext(brand);
+ let updated = 0;
+ let skipped = 0;
+
+ // Offset paging over a stable ordering: updates never change which rows
+ // the filter selects, so pages stay consistent across the walk.
+ for (let offset = 0; ; offset += BATCH_SIZE) {
+ const { data: rows, error } = await withRetry(
+ () =>
+ supabaseAdmin
+ .from('prompt_results')
+ .select(
+ 'id, response, citations, sentiment, mention_count, visibility_score, mention_position, mentioned_entity_count, competitor_mentions',
+ )
+ .eq('brand_id', brand.id)
+ .not('response', 'is', null)
+ .order('created_at', { ascending: false })
+ .order('id', { ascending: true })
+ .range(offset, offset + BATCH_SIZE - 1),
+ 'page fetch',
+ );
+ if (error) throw new Error(error.message);
+ if (!rows?.length) break;
+
+ for (let i = 0; i < rows.length; i += CONCURRENCY) {
+ const chunk = rows.slice(i, i + CONCURRENCY);
+ await Promise.all(
+ chunk.map(async (row) => {
+ const next = recount(row, brandInfo, competitors);
+ if (!next) {
+ skipped++;
+ return;
+ }
+ if (!dryRun) {
+ const { error: updateErr } = await withRetry(
+ () => supabaseAdmin.from('prompt_results').update(next).eq('id', row.id),
+ 'row update',
+ );
+ if (updateErr) throw new Error(updateErr.message);
+ }
+ updated++;
+ }),
+ );
+ }
+
+ console.log(` ${brand.name}: ${updated} changed, ${skipped} unchanged`);
+ if (rows.length < BATCH_SIZE) break;
+ await sleep(BATCH_PAUSE_MS);
+ }
+
+ return updated;
+}
+
+const candidates = await brandsInScope();
+const brands = [];
+for (const brand of candidates) {
+ if (brandArg || isAffectedName(brand.name)) {
+ brands.push(brand);
+ continue;
+ }
+ const { data: comps } = await supabaseAdmin
+ .from('competitors')
+ .select('name')
+ .eq('brand_id', brand.id);
+ if ((comps ?? []).some((c) => isAffectedName(c.name))) brands.push(brand);
+}
+
+console.log(`Recounting mentions for ${brands.length} brand(s)${dryRun ? ' — DRY RUN' : ''}`);
+
+const changed = [];
+let total = 0;
+for (const brand of brands) {
+ const n = await backfillBrand(brand);
+ total += n;
+ if (n > 0) changed.push(brand);
+}
+
+console.log(`Done. ${total} row(s) ${dryRun ? 'would be ' : ''}updated.`);
+if (changed.length && !dryRun) {
+ console.log('Refresh the rollups for the changed brands:');
+ for (const brand of changed) {
+ console.log(
+ ` INSIGHTS_BACKFILL_BRAND=${brand.id} node src/scripts/backfill-insights-daily.js # ${brand.name}`,
+ );
+ }
+}
+process.exit(0);