Skip to content

feat(citations): Competitor Gaps — domains that cite competitors but not us, + per-competitor source map - #326

Merged
gkhngyk merged 1 commit into
mainfrom
feature/citations-competitor-gaps
Jul 1, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/citations-competitor-gaps

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a Competitor Gaps view to the Citations page: the third-party domains AI engines use as sources around competitors but never around us (an outreach list), and per competitor, which domains feed their visibility.

Server action — getCitationGaps (web/src/lib/actions/citations.ts)

Computes response-level weighted co-occurrence from the same prompt_results data the overview reads — no LLM/scraper/external calls, no new writes. For each answer it looks at the set of cited domains, whether any competitor was mentioned, and whether we were present (brand mentioned OR one of our domains cited):

  • Gap domain = a third-party domain that appears in answers where a competitor is present and we're absent, and never appears in an answer where we're present.
  • Each co-occurrence is weighted by 1 / distinct sources in the answer, so a focused 2-source answer counts more than a 20-source one. A small threshold (≥2 competitor answers) cuts noise.
  • Also returns a per-competitor source map (domains feeding each competitor, with an alsoCitesUs flag), the competitor list, and a lowVisibility flag.
  • Selects only the needed columns (citations, competitor_mentions, mention_count) scoped to the date window — same shape/cost as getCitationsOverview.

UI (web/.../citations/page.tsx)

A new Competitor Gaps tab next to Domains / URLs, reusing the existing filter bar, with a segmented control:

  • Gap list — Domain · Source type · Competitor answers · Which competitors (chips) · Strength bar.
  • By competitor — competitor selector → Domain · Source type · Answers feeding · Also cites us? · Strength.
  • Low-visibility notice ("your visibility is low, so this list may be broad…") and a methodology tooltip.
  • Lazy-loaded: the gaps query only fires when the tab is opened, and re-fires when the shared filters change while it's active — so the default Domains/URLs load is unchanged.

Non-goals

Sentence/span-level attribution — the stored startIndex/endIndex are placeholders for web-scraped platforms, and both needs here act on domains, not sentences.

Related issue

Closes #300

Type of change

  • feat — New feature

How to test

  1. Brand with tracked competitors (with domains) + citation history → Citations → Competitor Gaps → Gap list: only domains that cite a competitor and never cite/mention the brand, ranked by strength.
  2. A domain citing both the brand and a competitor does not appear in the gap list.
  3. By competitor → pick a competitor: domains feeding it, with a correct "Also cites us?" badge.
  4. A 20-source multi-brand answer contributes less per-domain strength than a focused 2-source answer.
  5. No server action fires against any AI provider/scraper; load time comparable to Domains/URLs.
  6. Low-visibility brand shows the explanatory notice instead of a misleadingly huge list.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

… us + per-competitor source map

Add a getCitationGaps server action that derives, from the same
prompt_results data the citations overview reads, response-level weighted
co-occurrence between cited domains, competitor mentions and our presence.
No LLM/scraper calls, no new writes, minimal column select scoped to the
date window.

- Gap list: third-party domains that cite >=1 competitor in answers where
  we're absent and never appear in an answer where we're present, weighted
  by 1 / distinct sources per answer, thresholded to cut noise.
- By competitor: domains feeding a chosen competitor, with an 'Also cites
  us?' flag distinguishing exclusive vs overlapping sources.
- Low-visibility notice, methodology tooltip.

UI: a new Competitor Gaps tab on /dashboard/citations next to Domains/URLs
(segmented Gap list / By competitor), reusing the existing filter bar and
lazy-loaded only when the tab is opened.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new “Competitor Gaps” feature to the Citations dashboard, computing and displaying third-party domains that co-occur with competitors while never co-occurring with the brand, plus a per-competitor “source map” view.

Changes:

  • Added a new server action getCitationGaps that aggregates response-level, weighted co-occurrence data from prompt_results.
  • Added a new “Competitor Gaps” tab on the Citations page with “Gap list” and “By competitor” views, including lazy-loading behavior.
  • Refactored Citations page filter-to-API mapping into a shared apiFilters memo.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
web/src/lib/actions/citations.ts Adds getCitationGaps server action plus related result types/constants.
web/src/app/[locale]/(dashboard)/dashboard/citations/page.tsx Adds UI for the new tab, tables, and lazy-loading fetch logic wired to existing filters.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +459 to +462
let query = supabase
.from('prompt_results')
.select('id, model_used, region, created_at, citations, competitor_mentions, mention_count')
.eq('brand_id', brandId);
Comment on lines +1142 to +1161
// Lazy-load Competitor Gaps only when its tab is active; re-fetch when the
// shared filters change while it's open.
useEffect(() => {
if (sourceTab !== 'gaps' || !activeBrandId) return;
let cancelled = false;
setGapsLoading(true);
getCitationGaps(activeBrandId, apiFilters)
.then((res) => {
if (!cancelled) setGaps(res);
})
.catch(() => {
if (!cancelled) setGaps(null);
})
.finally(() => {
if (!cancelled) setGapsLoading(false);
});
return () => {
cancelled = true;
};
}, [sourceTab, activeBrandId, apiFilters]);
@gkhngyk
gkhngyk merged commit b2b72ef into main Jul 1, 2026
5 checks passed
@gkhngyk
gkhngyk deleted the feature/citations-competitor-gaps branch July 1, 2026 09:13
gkhngyk added a commit that referenced this pull request Jul 2, 2026
…rom domain-list flags (#327)

Follow-up to #326 (Copilot review):
- getCitationGaps selected model_used / region / created_at but never read
  them from the rows (they're only used in the WHERE clause). Trim the
  select to id, citations, competitor_mentions, mention_count.
- The Gaps tab fetch was keyed off apiFilters, which includes the
  exclude-own / competitor-only / own-only domain-list flags that
  getCitationGaps doesn't apply — so toggling them triggered a no-op
  refetch. Split out a gapFilters memo (date/platform/region/topic/prompt
  only) and key the gaps effect off it.
@gkhngyk gkhngyk mentioned this pull request Jul 6, 2026
5 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(citations): Competitor Gaps — domains that cite competitors but not us, + per-competitor source map

2 participants