Skip to content

fix(traffic): count a single-page app's not-found route as a broken AI link - #1247

Merged
gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:fix/traffic-soft-404
Oct 20, 2026
Merged

gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:fix/traffic-soft-404

Conversation

@cihan-gyk

@cihan-gyk cihan-gyk commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Follow-up promised in #1192's review: soft 404s in Broken AI links.

A single-page app answers every path with 200 and shows its own "Page not found" route, so the browser's status says the page works. Broken AI links never listed such a page:

  • the snippet read the title only when the browser reported no status (Safari);
  • a 2xx always counted as working.

Snippet (server/src/lib/tracking-script.js)

  • The visit's beacon goes at once, as before this PR, so the visits counted don't depend on the page loading (review of 2026-10-20).
  • When the title didn't read not found (and the status is a 2xx or unknown), the snippet reads it again 1.5 s after load. If a single-page app now shows its not-found page, it sends {k: "nf", u, s, a, st} for the address it landed on.
  • With a 2xx status, nf is 1 only for the not-found page's own title, a stricter pattern: "404", "Page not found | Acme", "Acme – 404 Not Found", and not "How we fixed our 404s". Without a status, the old pattern stays.

/track (server/src/routes/traffic.js) keeps not_found_title with no status or a 2xx; a 3xx–5xx status alone decides. A k: "nf" body marks the latest pageview of that address, IP and browser in the last 10 minutes, with the same status, as a not-found title. It counts no visit.

Migration 00200 (ai_traffic_broken_pages, ai_traffic_broken_links; the rest is 00186's)

  • A page with a 2xx and a not-found title is missing, status soft404, once at least two visits saw it. A reload continues a visit, so it doesn't count again. The beacon is public, so one visit, stray or made up, lists nothing.
  • Its 2xx doesn't count as the page working again, and the nightly page read can't clear it: that read gets the app's shell with a 200 too.
  • ai_traffic_broken_links reads those pageviews for the visits.

Web: TrafficBrokenLink.status gains 'soft404', shown on the page and in the export as "Shows a not-found page (200)". An unknown status still reads as a 404.

Deploy: any order works. Before 00200, such rows count as working, as today; before the web, soft404 reads as a 404. Applying 00200 first lists the first soft 404s as soon as the new snippet sends them.

Numbering: main has 00198 (#1239) and 00199 (#1131), so this migration is 00200 (renumbered at gkhngyk's request). Open PRs: 00188 #1210, 00190 #1225, 00191 #1226, 00194 #1229, 00195 #1230, 00196 #1234, 00197 #1243. schema.sql is rebuilt from main plus 00200.

Related issue

Follow-up to #1192 (review of 2026-10-19).

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

Unit tests

  • tracking-script.test.js runs the snippet in a VM with fake timers and listeners:
    • it sends the visit at once, even before load, and the k: "nf" mark only when the title turns not found after load, with the address the visit landed on;
    • there is no second check after a 404, or after a title that already read not found;
    • with a 2xx, a not-found title gives nf: 1 and "How we fixed our 404s" gives nf: 0.
  • traffic-track.test.js:
    • nf is kept with a 2xx or no status, and dropped with a 3xx–5xx;
    • k: "nf" marks the latest matching pageview (same address, IP, browser and status, last 10 minutes) without inserting a row, and does nothing for a 404 or with no match.
  • traffic-broken-links.test.ts and traffic-export.test.ts: soft404 rows and their label.

Dev DB (00200 applied; each case in a rolled-back transaction, as the service role)

Case status
A route that answers 200 with "Page not found" soft404
A page that answers 404 404
A soft 404 whose later pageviews have a normal title not listed (fixed)
A page with a 2xx and a normal title not listed

In the browser: two temporary visits to /app/old-route with a 200 and a not-found title show in AI Referral Traffic › Pages › Broken AI links as "Shows a not-found page (200)". I checked it in light and dark mode, at 1440 and 1024 px, then deleted the rows.

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

Also: web yarn test (1793) and yarn build; server npm run lint, npm test (1842); bash supabase/build-schema.sh.

🤖 Generated with Claude Code

…I link

A single-page app answers every path with 200 and shows its own "Page
not found" route, so Broken AI links never listed it: the snippet read
the title only when the browser reported no status, and a 2xx counted
as working.

The snippet now sends its beacon a moment after load (or as the visitor
leaves), so the app has set its title, keeping the landing address and
time, and with a 2xx reads the title with a stricter pattern: the
not-found page's own title ("404", "Page not found | Acme"), not a page
about 404s. /track keeps not_found_title with a 2xx. 00198 lists such a
page as broken with status soft404, doesn't let its 2xx count as
working, and keeps the nightly page read from clearing it, since that
read gets the app's shell with a 200 too. The page and the export say
"Shows a not-found page (200)".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 20, 2026
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk this is the soft-404 follow-up from #1192: a single-page app's 200 "Page not found" route now shows in Broken AI links as "Shows a not-found page (200)".

Two things worth a look:

  • The snippet's beacon now waits 1.5 s after load (or goes on pagehide / hidden), so the app has set its title. A visitor who leaves within that time is still counted, from pagehide. The landing URL and time are kept, so a router that moves on doesn't change the row.
  • The 2xx title pattern is stricter than the no-status one: it needs the not-found page's own title, the words standing alone between separators ("404", "Page not found | Acme"), so "How we fixed our 404s" stays an ordinary page.

00198 only redefines ai_traffic_broken_pages and ai_traffic_broken_links from 00186. Any deploy order works; 00198 first lists soft 404s as soon as the new snippet sends them.

@gkhngyk

gkhngyk commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Heads-up on the migration number: 00198 (#1239) and 00199 (#1131) are now on main, so please take 00200 for the soft-404 migration and rebuild schema.sql.

… to 00200

main now has 00198 (ansvisor#1239) and 00199 (ansvisor#1131), so the soft-404 migration
moves from 00198 to 00200, with the comments that name it, and
schema.sql is rebuilt from the migrations.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk done in c9876d4: merged main, renamed the migration to 00200_ai_traffic_soft_404.sql (and the comments that named 00198), and rebuilt schema.sql from the migrations. Only schema.sql had conflicted. Server 1900 and web 1798 tests + build pass, and the PR is MERGEABLE again.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good catch on single-page apps, and the stricter title pattern for a 2xx reads right. Main has 00198 and 00199 now, so this takes 00200 (see my note above). After the rename it only conflicts in schema.sql.

  1. One beacon is enough to list a working page, and nothing clears it. The beacon is public: anyone with the tracking code can send st: 200, nf: 1 for any URL.

    • A 404 row is cleared once the nightly page read finds the page working. A soft 404 skips that check by design, so the row stays until another AI visitor lands on the same address with a normal title.
    • On a cited page it's worse. The detector counts a cited page from its first visit, so one beacon, stray or made up, raises a high-impact broken_citation_target with an Action Center action. The page also shows in the weekly digest.

    Please let ai_traffic_broken_pages count a soft 404 only after at least two visits saw it (brokenLinkMinVisits is 3). Add a test with one soft-404 beacon on a cited page.

  2. The visit beacon now waits on every page. It goes at load plus 1.5 s, or when the visitor leaves, instead of at once. AI visits are the number this snippet exists for. A visitor who closes the tab before load now depends on pagehide or visibilitychange delivering the beacon. Most browsers do, but this changes every customer's counts. Please say how you checked it, or compare one brand's daily visits for a few days on each side of the deploy.

Smaller:

  • The title pattern is English only, so "Seite nicht gefunden" or "Página no encontrada" reads as a working page. Fine for now; worth a line in the comment.

Measured on prod as authenticated, on the largest traffic brand, then rolled back. Every one of its 10,949 pageviews was given a status, with 1 in 20 paths soft-404ing.

Read 00186 This PR
ai_traffic_broken_links, all time 1.5 s 3.0 s
ai_traffic_broken_links, 30 days 3.9 s 4.4 s
ai_traffic_pages, all time 0.8 s 0.8 s
ai_traffic_broken_pages, all time 0.5 s 0.5 s
  • The rise comes from 159 broken pages instead of 48, mostly in the similar-page match.
  • A member of another org gets null.
  • The migration replaces only 00186's two functions, and prod's bodies match 00186, so nothing later is undone.

…isits

The beacon is public, so one visit with a 200 and a not-found title,
stray or made up, listed a working page as broken, nothing cleared it,
and on a cited page it raised broken_citation_target. 00200 now counts a
soft 404 only once at least two visits saw it (a reload continues one).

The snippet sends the visit's beacon at once again, so the visits
counted don't depend on the page loading. When the title didn't read
not found, it reads it again a moment after load and, if a single-page
app now shows its not-found page, sends k: "nf" for the address it
landed on; /track marks that visit's pageview (same address, IP and
browser in the last ten minutes) instead of counting another. The title
patterns are English only, as the comment now says.

Review on ansvisor#1247.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk thanks, both points are fixed in f9e02f3:

1. Two visits. 00200 counts a soft 404 only once at least two visits saw the page: count(distinct coalesce(visit_of, id)) over its soft-404 pageviews, so a reload doesn't count again. Checked on the dev DB with a cited own page (5 citations), each case in a rolled-back transaction:

Soft-404 pageviews ai_traffic_broken_links
1 visit not listed
1 visit + a reload in it not listed
2 visits listed, soft404, 5 citations

So one beacon, stray or made up, lists nothing, and nothing reaches broken_citation_target or the digest.

2. The visit beacon doesn't wait any more. Rather than argue the delay was safe, I took it out. The visit's beacon goes at once, as before this PR, so the counts can't change. When the title didn't read not found (with a 2xx or no status), the snippet reads it again 1.5 s after load. If a single-page app now shows its not-found page, it sends {k: "nf", u, s, a, st}, and /track marks the latest pageview of that address, IP and browser in the last 10 minutes, with the same status, without inserting a row. A visitor who leaves before that mark is sent loses only the soft-404 flag, never the visit.

Checked end to end with the branch's API: a visit beacon stored the row with not_found_title = false, the mark flipped that same row, and the row count stayed 1 (temp row deleted). Unit tests cover:

  • the visit sent before load;
  • no second check after a 404 or a title that already said not found;
  • the mark's lookup filters;
  • a 404 mark doing nothing.

Smaller: the comment now says both patterns are English only.

Server 1902 tests pass; schema.sql rebuilt.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, both points are fixed:

  • One beacon no longer lists a page. A soft 404 now needs two distinct visits, counted by visit_of.
  • The visit beacon goes at once again. The nf follow-up only marks that visit's own pageview: same address, IP and browser within ten minutes, a 2xx or no status, not already marked. It never adds a visit.

Tested with #1244 on main: server 1,919 and web 1,893 tests pass, and tsc, lint and Prettier are clean.

00200 is applied to production. On the largest traffic brand's real data, ai_traffic_broken_links takes 20 ms over all time and 8 ms over 30 days. Prod has almost no status rows yet, so the synthetic numbers from my last review are the ones to watch as the new snippet spreads. I pushed a merge commit with schema.sql rebuilt.

@gkhngyk
gkhngyk merged commit 248aee8 into ansvisor:main Oct 20, 2026
5 checks passed
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.

2 participants