Skip to content

feat(traffic): only persist platform-attributable AI visits (referrer or utm), drop unknowns - #310

Merged
gkhngyk merged 1 commit into
mainfrom
feature/traffic-attributable-only
Jun 27, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/traffic-attributable-only

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Only persist an AI-traffic event when it is attributable to a concrete AI platform, so the dashboard's null-backed "Unknown" bucket stops growing. The root cause: the snippet derived the platform from the referrer host only, so a visit detected via the utm_source fallback (no AI referrer) sent an empty platform → stored as null → "Unknown".

Changes

  • server/src/public/t.js — resolve a concrete platform from either signal:

    • the referrer host matches a known AI domain (normalized to that canonical host, so subdomains like search.chatgpt.com collapse to a labeled platform), or
    • when there's no AI referrer, a known utm_source (chatgpt/openai → chatgpt.com, perplexity → perplexity.ai, claude → claude.ai, gemini → gemini.google.com, copilot → copilot.microsoft.com).

    The beacon now sends the resolved platform (s: pf) instead of the bare referrer host. If neither signal resolves a platform, the snippet returns early and nothing is sent. (A bare utm_medium=ai with no resolvable source no longer stores a null row.)

  • server/src/routes/traffic.js — defensively skip inserts with an empty source_platform (returns 204 without writing), so older cached snippets can't keep writing null rows.

  • web/.../traffic/_charts.tsx — add labs.google ("Google Labs") and bing.com ("Bing") to the label/color maps and note they must stay in sync with the t.js detection hosts. Kept unknown so historical pre-feat(traffic): only persist platform-attributable AI visits (referrer or utm), drop unknowns #288 null rows still render.

Verification — added a standalone simulation of the t.js resolution covering 15 cases (referrer, www/subdomain normalization, each utm source, medium-only, non-AI utm, Google AI Overview, Bing /ck/a vs organic, direct) — all pass. yarn lint / typecheck / tests green on both web and server; node --check passes on t.js.

Related issue

Closes #288

Notes

  • No DB migration. Historical null rows are left in place (they keep rendering as "Unknown" until they age out of the window); only new writes are gated.
  • Cache: t.js is CDN + browser cached for hours, so already-installed snippets keep sending the old shape until cache expiry / a Cloudflare purge — the server-side empty-platform guard covers that window.
  • Out of scope (per issue): Google AI Overview clicks arrive with referrer = google.com and no utm, so they're indistinguishable from organic Google and aren't captured. The optional "single shared source of truth across t.js + dashboard" was left out (the two run in different packages — server static asset vs web app); a sync comment is added in _charts.tsx instead.

Type of change

  • feat — New feature

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

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

Updates the AI-traffic tracking pipeline to only record visits when they can be attributed to a specific AI platform, preventing new null-platform rows from inflating the dashboard’s “Unknown” bucket.

Changes:

  • Update the tracking snippet (t.js) to resolve a concrete platform from referrer-domain matching or known utm_source values, and to drop unattributable events client-side.
  • Add a server-side guard to skip inserts when source_platform is empty, protecting against older cached snippets that still send empty platform values.
  • Extend the dashboard platform label/color maps to include labs.google and bing.com, with a sync note to keep mappings aligned with detection.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
server/src/public/t.js Resolve and send a concrete platform (s) and avoid sending unattributable events.
server/src/routes/traffic.js Skip persisting events with an empty source_platform to prevent new “Unknown” rows.
web/src/app/[locale]/(dashboard)/dashboard/traffic/_charts.tsx Add labels/colors for labs.google and bing.com and document the sync requirement with detection.
Files not reviewed (1)
  • server/src/public/t.js: Generated file

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

Comment thread server/src/routes/traffic.js Outdated
Comment on lines +115 to +118
const sourcePlatform = (body.s || '').slice(0, 255);
if (!sourcePlatform) {
return res.status(204).end();
}
… or utm)

Drop AI-traffic events that can't be attributed to a concrete platform,
so the dashboard no longer shows a null-backed "Unknown" bucket.

- t.js: resolve the platform from the AI referrer host (normalized to a
  known domain) or, when there's no referrer, from a known utm_source
  (chatgpt/openai, perplexity, claude, gemini, copilot). Send the resolved
  platform instead of the bare referrer host (which was empty for
  utm-detected visits, producing null rows). If nothing resolves, don't send.
- traffic.js: defensively skip inserts with an empty source_platform so
  older cached snippets can't write null rows.
- _charts.tsx: add labs.google + bing.com labels/colors; note the list must
  stay in sync with the t.js detection hosts. Keep "unknown" for historical
  pre-#288 rows.

Google AI Overview clicks (referrer google.com, no utm) remain
indistinguishable from organic Google and are out of scope.
@gkhngyk
gkhngyk force-pushed the feature/traffic-attributable-only branch from 8abb62b to 1424236 Compare June 27, 2026 15:24
@gkhngyk
gkhngyk merged commit f395b5a into main Jun 27, 2026
3 checks passed
@gkhngyk
gkhngyk deleted the feature/traffic-attributable-only branch June 27, 2026 16:37
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(traffic): only persist platform-attributable AI visits (referrer or utm), drop unknowns

2 participants