Skip to content

Open source alternative to Semrush and Ahrefs

Count bare brand names for domain-named AI Visibility brands (EVE-404) (#906)

Carter Smithb77bb5b1ba1d
diff --git a/src/server/features/ai-visibility/services/aiVisibilityMatching.test.ts b/src/server/features/ai-visibility/services/aiVisibilityMatching.test.ts
index 30a4d0c5..6a04b03d 100644
--- a/src/server/features/ai-visibility/services/aiVisibilityMatching.test.ts
+++ b/src/server/features/ai-visibility/services/aiVisibilityMatching.test.ts
@@ -38,6 +38,16 @@ describe("brand mentions", () => {
});
});
+ it.each([
+ ["acmeride.com", true],
+ ["shop.acmeride.com", false],
+ ])("bare name mentions a brand named %s: %s", (domain, mentioned) => {
+ const text = "- [Acmeride](https://www.acmeride.com/): Connects directly.";
+ expect(matchAiBrand(answer(text), { name: domain, domain }).mentioned).toBe(
+ mentioned,
+ );
+ });
+
it("returns original UTF-16 offsets through emoji and both Unicode accent forms", () => {
const text = "🔎 Café and Café are the same spelling.";
const spans = aiMentionSpans(text, ["Café"]);
diff --git a/src/server/features/ai-visibility/services/aiVisibilityMatching.ts b/src/server/features/ai-visibility/services/aiVisibilityMatching.ts
index dff1952f..882b7b89 100644
--- a/src/server/features/ai-visibility/services/aiVisibilityMatching.ts
+++ b/src/server/features/ai-visibility/services/aiVisibilityMatching.ts
@@ -1,4 +1,5 @@
import { sort } from "remeda";
+import { parse } from "tldts";
import type { AiBrand } from "@/shared/ai-visibility";
import type { ParsedAiAnswer } from "../providers/dataforseoEvidence";
@@ -61,6 +62,22 @@ export function aiMentionSpans(
);
}
+/** "www.acme.com" → "acme"; subdomains and non-domains → null. */
+function rootDomainName(value: string) {
+ const { hostname, domain, domainWithoutSuffix } = parse(value);
+ return hostname?.replace(/^www\./, "") === domain
+ ? domainWithoutSuffix
+ : null;
+}
+
+/** Answer text naming the brand. A brand named "acme.com" is written "Acme". */
+export function aiBrandNames(brand: Pick<AiBrand, "name" | "domain">) {
+ const stem = rootDomainName(brand.name);
+ return stem && stem === rootDomainName(brand.domain)
+ ? [brand.name, brand.domain, stem]
+ : [brand.name, brand.domain];
+}
+
/**
* A brand is mentioned when its name or domain appears in the answer prose,
* and cited when the answer cites a page on its domain. A generic brand name
@@ -70,7 +87,7 @@ export function matchAiBrand(
answer: ParsedAiAnswer,
brand: Pick<AiBrand, "name" | "domain">,
) {
- const spans = aiMentionSpans(answer.answerText, [brand.name, brand.domain]);
+ const spans = aiMentionSpans(answer.answerText, aiBrandNames(brand));
return {
mentioned: spans.length > 0,
cited: answer.citations.some((citation) =>
diff --git a/src/server/features/ai-visibility/services/aiVisibilityResults.ts b/src/server/features/ai-visibility/services/aiVisibilityResults.ts
index b3e43de3..2ac88051 100644
--- a/src/server/features/ai-visibility/services/aiVisibilityResults.ts
+++ b/src/server/features/ai-visibility/services/aiVisibilityResults.ts
@@ -23,7 +23,11 @@ import {
import type { ObservationWithPrompt } from "../repositories/AiVisibilityRepository";
import { markdownToText } from "../providers/dataforseoEvidence";
import { AiVisibilityError } from "./aiVisibilityErrors";
-import { aiMentionSpans, ownedAiDomain } from "./aiVisibilityMatching";
+import {
+ aiBrandNames,
+ aiMentionSpans,
+ ownedAiDomain,
+} from "./aiVisibilityMatching";
export function aiRunView(
run: RunRow,
@@ -266,7 +270,7 @@ function answerView(
.filter((b) => b.mentioned)
.map((b) => ({
domain: b.domain,
- spans: aiMentionSpans(answerText, [b.name, b.domain]).filter(
+ spans: aiMentionSpans(answerText, aiBrandNames(b)).filter(
(span) => span.end <= limit,
),
}))