Skip to content

feat(analytics): say why a valuable page gets no AI traffic - #787

Merged
gkhngyk merged 1 commit into
mainfrom
feature/page-opportunity-citation-state
Aug 25, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/page-opportunity-citation-state

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Detection (#724) finds the pages that carry commercial weight and receive no AI referral. It cannot say why, and the three reasons need three different actions:

state what it means what the customer does about it
cited answers already cite the page, and it still earns no visit a click-through problem — the answer names you and the reader does not come
targeted_not_cited a prompt you track points at this page and no answer cites it a genuine visibility loss on a query you already care about
not_targeted nothing you track points here at all a coverage gap — the most common finding on a large site

Measured on the connected property before writing any of it: 5 of 16 open findings are cited — one of them 104 times in 28 days. The card called all sixteen "pages AI engines aren't sending traffic to", which is true, and for those five points at the wrong fix.

The classification is deterministic arithmetic over data we already hold: own-domain citations from citation_urls, targeting from prompt_target_urls. No model runs in this path, and nothing new is fetched.

targeted means a prompt carries this URL as a target, not that a prompt covers the page's topic. Topical coverage is not something SQL can decide, and inventing it would put a made-up reason on a finding a customer is about to act on.

Two repairs travel with it

The reads were standing on the 1000-row cap. Detection selected 28 days of ga_page_stats and ga_ai_traffic_stats un-paginated, and PostgREST truncates that silently (#427/#450/#464). ga-sync keeps up to PAGE_DAILY_LIMIT = 5000 pages a day, so a property with more than ~36 pages taking traffic on an average day overflows it; today's only property produces 743 rows in the window and fits, which is the only reason nothing has gone wrong yet.

The failure is worse than missing findings. The AI-traffic half truncates the same way, and a page missing from that lookup is raised as receiving no AI traffic when it received some — a finding that is simply false, on the second customer who connects a property. Aggregation moves into the RPC (page × day collapses to one row per page) and the caller pages through what is left, ordered by landing page: range without an order is a promise the database never made.

Case is matched on, never rewritten. The first version lowercased the path, and the live run caught it immediately: a real slug, /blog/YmVzdC10b2, became /blog/ymvzdc10b2 — a URL that does not exist on the site. The finding would have carried it to the surface, where the customer told to fix that page would click it and get a 404. The RPC now groups on the lowercased path and reports the spelling Analytics observed, so /Pricing and /pricing stay one page and the one shown resolves.

Percent-decoding has to be total, not correct-on-good-input

12.7% of the cited URLs on brands' own domains are percent-encoded (2,647 of 20,896) — non-ASCII paths, which is most of this customer base. Matching them against GA's decoded paths without decoding would classify a page that is cited as one nothing points to, which is the exact error this PR exists to remove.

But real citation URLs in the database contain sequences that decode to invalid UTF-8 (0x80, found by running the decoder over them). convert_from raises on those, and an exception inside the aggregate would abort detection for the whole brand — so url_decode_safe returns the input unchanged rather than failing.

Ranking stays in JavaScript on purpose

The exclusion list (isExcludedPath) is shared with the prompt suggestion generator (#705), and a second copy of it inside the RPC would drift from this one the first time either is extended. So the RPC aggregates and joins, and scoring, floors and exclusions stay where they were. The cost is that every page travels, which is what the paged read exists to survive.

Related issue

Part of #719 — the citation half of the detection layer, and the acceptance criterion that "low AI visibility" distinguishes not covered by any prompt from covered and not cited. Clustering, LLM enrichment and the acted-on half of the lifecycle stay open: one connected property and sixteen findings is not the volume those are for.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

Migration 00075 is already applied to the hosted database, and it is additive — no deploy ordering to respect.

Automated — cd server && yarn test (404 pass; 33 in page-opportunities.test.js, including the three states, the string-valued bigint columns PostgREST returns, and the case-preservation contract).

The revenue, transactions and key_events branches still cannot be exercised against real data — the only connected property reports none of them — so they stay pinned by tests.

Verified live against the connected property:

  • 16 findings, 5 cited and 11 not_targeted, in under a second. The top one is cited 104 times in the window and has never been visited from an assistant.
  • Re-running changes nothing and leaves first_detected_at intact.
  • select public.normalize_page_path('https://www.Example.com/Blog/YmVzdC10b2/?utm=1#x') → /Blog/YmVzdC10b2, and public.normalize_page_path('/a%80b') → /a%80b (the undecodable one, returned rather than raising).

targeted_not_cited has no live instance today: both targeted pages are also cited, so they classify as cited. That branch is held by tests until a first real one appears.

On the surface — open AI Traffic Analytics on a brand with a mapped property. The findings table gains an AI visibility column: Cited with the citation count, Not cited with how many prompts target the page, or No prompt. A finding raised before this landed shows an em dash rather than a guess, until the next nightly run classifies it.

yarn typecheck, yarn lint (7 pre-existing warnings, 0 errors), yarn format:check and yarn build all pass from web/.

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/)
  • supabase/schema.sql regenerated via bash supabase/build-schema.sh
  • Changes are focused — one concern per PR

Detection (#724) finds pages that carry commercial weight and receive no
AI referral. It could not say why, and the three reasons need different
actions: answers already cite the page, a prompt we track points at it
and no answer cites it, or nothing points at it at all. On the only
connected property, 5 of 16 open findings are cited — one of them 104
times in 28 days — and the card told all 16 the same story.

The classification is deterministic and joins nothing new: own-domain
citations come from citation_urls, targeting from prompt_target_urls. No
model runs in this path.

Two repairs travel with it, both in the same RPC:

- The reads were un-paginated selects over 28 days of ga_page_stats and
  ga_ai_traffic_stats, which PostgREST truncates at 1000 rows. ga-sync
  keeps up to 5000 pages a day, so a property with more than ~36 pages
  taking traffic on an average day overflows it; today's only property
  produces 743 rows and fits. The AI half truncating is worse than
  missing findings — a page that did receive AI traffic falls out of the
  lookup and is raised as a finding that is false. Aggregation moves into
  SQL and the caller pages through the rest.

- Paths are matched case-insensitively but reported as Analytics spelled
  them. Folding the case turned a real slug, /blog/YmVzdC10b2, into a URL
  that 404s, which the finding would then have shown to the customer told
  to fix that page.

Percent-decoding is total by design: 12.7% of own-domain cited URLs are
encoded, and some decode to invalid UTF-8, which would abort the run.
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Aug 25, 2026
@gkhngyk
gkhngyk merged commit f88548c into main Aug 25, 2026
5 checks passed
@gkhngyk
gkhngyk deleted the feature/page-opportunity-citation-state branch August 25, 2026 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant