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(audit): match the trend to a primary domain stored with a path, port or uppercase scheme (#900)

nimbly155b46690c2c1
diff --git a/server/src/lib/audit/host.js b/server/src/lib/audit/host.js
new file mode 100644
index 000000000..c0952bd14
--- /dev/null
+++ b/server/src/lib/audit/host.js
@@ -0,0 +1,26 @@
+/**
+ * Host normalization for matching site audits to a brand's domain.
+ *
+ * Both sides of the comparison must go through the same normalization, or a
+ * stored domain such as `example.com/` never equals the host of an audit of
+ * `https://example.com`.
+ */
+
+/** Lowercased hostname of a URL with a leading www. stripped; the port is ignored (null on failure). */
+export function normHost(u) {
+ try {
+ return new URL(u).hostname.replace(/^www\./i, '').toLowerCase();
+ } catch {
+ return null;
+ }
+}
+
+/**
+ * Normalized host of a stored brand domain. Stored domains are free text: a
+ * bare host, or one with a scheme (any case), path, trailing slash or port.
+ */
+export function normStoredDomain(domain) {
+ if (!domain) return null;
+ const value = String(domain).trim();
+ return normHost(/^https?:\/\//i.test(value) ? value : `https://${value}`);
+}
diff --git a/server/src/lib/audit/host.test.js b/server/src/lib/audit/host.test.js
new file mode 100644
index 000000000..e4504dc32
--- /dev/null
+++ b/server/src/lib/audit/host.test.js
@@ -0,0 +1,53 @@
+import { describe, expect, it } from 'vitest';
+import { normHost, normStoredDomain } from './host.js';
+
+describe('normHost', () => {
+ it('lowercases, strips a leading www. and ignores path, query and fragment', () => {
+ expect(normHost('https://www.Example.com/Pricing?x=1#top')).toBe('example.com');
+ });
+
+ it('ignores the port', () => {
+ expect(normHost('https://example.com:8443/')).toBe('example.com');
+ });
+
+ it('returns null for something that is not a URL', () => {
+ expect(normHost('example.com')).toBeNull();
+ expect(normHost('')).toBeNull();
+ expect(normHost(undefined)).toBeNull();
+ });
+});
+
+describe('normStoredDomain', () => {
+ // The trend endpoint keeps an audit when normHost(audit url) equals this value, so every way a
+ // brand's domain can be stored has to reduce to the host an audit of that site reduces to.
+ const auditedHost = normHost('https://example.com/some/page');
+
+ it.each([
+ 'example.com',
+ 'Example.COM',
+ 'www.example.com',
+ 'example.com/',
+ 'example.com/en',
+ 'example.com:443',
+ 'example.com:8080/en?x=1',
+ 'https://example.com',
+ 'HTTPS://example.com',
+ 'http://www.Example.com/x',
+ 'https://www.Example.com/x/',
+ ' example.com/ ',
+ ])('reduces stored domain %j to the audited host', (stored) => {
+ expect(normStoredDomain(stored)).toBe(auditedHost);
+ });
+
+ it('does not match a different host', () => {
+ expect(normStoredDomain('https://example.org/')).not.toBe(auditedHost);
+ expect(normStoredDomain('notexample.com')).not.toBe(auditedHost);
+ });
+
+ it('returns null when there is no usable domain', () => {
+ expect(normStoredDomain(null)).toBeNull();
+ expect(normStoredDomain(undefined)).toBeNull();
+ expect(normStoredDomain('')).toBeNull();
+ expect(normStoredDomain('not a domain')).toBeNull();
+ });
+});
diff --git a/server/src/routes/audits.js b/server/src/routes/audits.js
index 5454fcd11..a0e66e235 100644
--- a/server/src/routes/audits.js
+++ b/server/src/routes/audits.js
@@ -23,6 +23,7 @@ import {
PlanLimitError,
} from '../lib/plan-guard.js';
import { buildAuditContext } from '../lib/audit/context.js';
+import { normHost, normStoredDomain } from '../lib/audit/host.js';
import { runSignals } from '../lib/audit/engine.js';
import { evaluateLlmSignals } from '../lib/audit/llm-signals.js';
import { evaluateBrandEntity } from '../lib/audit/external-signals.js';
@@ -242,15 +243,6 @@ router.get('/quota', async (req, res) => {
}
});
-/** Lowercased host of a URL with a leading www. stripped (null on failure). */
-function normHost(u) {
- try {
- return new URL(u).host.replace(/^www\./i, '').toLowerCase();
- } catch {
- return null;
- }
-}
-
// GET /api/audits/trend?brandId=… — completed audits of the brand's PRIMARY
// domain over time (score + category scores), for the hub trend chart.
// Registered before /:id so "trend" isn't matched as an audit id.
@@ -270,12 +262,7 @@ router.get('/trend', async (req, res) => {
.eq('brand_id', brandId);
const primary =
(domainRows ?? []).find((d) => d.is_primary)?.domain ?? (domainRows ?? [])[0]?.domain ?? null;
- const primaryHost = primary
- ? primary
- .replace(/^https?:\/\//, '')
- .replace(/^www\./i, '')
- .toLowerCase()
- : null;
+ const primaryHost = normStoredDomain(primary);
const { data: rows } = await supabaseAdmin
.from('site_audits')