Repository navigation
feat(analytics): say why a valuable page gets no AI traffic - #787
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
citedtargeted_not_citednot_targetedMeasured 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 fromprompt_target_urls. No model runs in this path, and nothing new is fetched.targetedmeans 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_statsandga_ai_traffic_statsun-paginated, and PostgREST truncates that silently (#427/#450/#464).ga-synckeeps up toPAGE_DAILY_LIMIT = 5000pages 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:
rangewithout 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/Pricingand/pricingstay 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_fromraises on those, and an exception inside the aggregate would abort detection for the whole brand — sourl_decode_safereturns 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 featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
Migration
00075is already applied to the hosted database, and it is additive — no deploy ordering to respect.Automated —
cd server && yarn test(404 pass; 33 inpage-opportunities.test.js, including the three states, the string-valuedbigintcolumns PostgREST returns, and the case-preservation contract).The
revenue,transactionsandkey_eventsbranches 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:
citedand 11not_targeted, in under a second. The top one is cited 104 times in the window and has never been visited from an assistant.first_detected_atintact.select public.normalize_page_path('https://www.Example.com/Blog/YmVzdC10b2/?utm=1#x')→/Blog/YmVzdC10b2, andpublic.normalize_page_path('/a%80b')→/a%80b(the undecodable one, returned rather than raising).targeted_not_citedhas no live instance today: both targeted pages are also cited, so they classify ascited. 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:
Citedwith the citation count,Not citedwith how many prompts target the page, orNo 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:checkandyarn buildall pass fromweb/.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)supabase/schema.sqlregenerated viabash supabase/build-schema.sh