Skip to content

feat(docs-next): send a HockeyStack goal for each docs search - #340

Closed
Jesswinters wants to merge 3 commits into
mainfrom
vigilante/docs-search-hockeystack
Closed

Jesswinters wants to merge 3 commits into
mainfrom
vigilante/docs-search-hockeystack

Conversation

@Jesswinters

@Jesswinters Jesswinters commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Merge order

Independent; merges on its own.

Summary

Marketing wants to see what readers search for on coder.com/docs in HockeyStack. The search is a dialog, not a form submit, so HockeyStack cannot capture it on its own. This sends the goal HockeyStack already has set up, HockeyStack.goal('Docs Search', { search_term }), once per search.

Design

A new hook, useSearchGoal in packages/docs-next/src/search-analytics.ts, is used by both search dialogs: HybridSearchDialog, which production uses because apps/docs/vercel.json sets DOCS_SEMANTIC_SEARCH=1, and DocsSearchDialog, the keyword-only fallback. The hook sends the goal when typing has paused for 1 second, or at once when a result is selected by click or Enter. It never sends the same trimmed term twice in a row, so a select after the pause, or a re-render, does not count again. Clearing the input resets that, so searching the same term again later counts as a new search. The page URL is not sent, because HockeyStack records it on its own.

Security's review of this PR (#340 (comment)) set the conditions for a send, and every one is checked on every send, including each debounced prefix: the dialog is open, so a term typed and then dismissed is never sent; the search returned at least one result for that exact term; the reader has accepted analytics consent; and the term passes one shared filter, searchGoalTerm. That filter rejects an empty or whitespace-only term, a run of 20 or more characters without spaces (/[A-Za-z0-9_\-.+\/=]{20,}/), an email address, and anything over 100 characters, which is dropped and never truncated. Consent is window.Osano.cm.getConsent().ANALYTICS === "ACCEPT", and it fails closed: a missing Osano, a getConsent that throws, a denied choice and a choice not yet made all send nothing. HockeyStack itself only loads after marketing consent, so the analytics check also covers a reader who accepted marketing and declined analytics. The call is guarded by a typeof window.HockeyStack?.goal === "function" check and a try/catch, so a tag that has not loaded or has changed shape never throws.

No new script, origin or CSP change: this calls the HockeyStack tag that coder.com's tag manager already loads under the existing analytics policy.

Decisions
  • Debounce plus select, not per keystroke. Fumadocs runs a query on every keystroke after a 100 ms debounce, which is too short to count as "one search". A 1-second pause gives about one goal per search. Selecting a result sends at once, so a quick type-and-Enter is still counted before the page changes.
  • Dedupe on the last term sent. Without it, selecting a result after the pause, or a re-render, would send a second goal for the same search.
  • One hook for both dialogs. Production uses the hybrid dialog, but the keyword dialog is used wherever DOCS_SEMANTIC_SEARCH is off, so both are wired to the same hook.
  • Trimmed term is sent. This follows the snippet in the request (search_term: query.trim()).
  • Zero-result terms are not sent, and no zero-results event exists. Pasted secrets and junk almost never match a page. If Marketing wants failed searches, send a flag or count, never the term.
  • The 20-character rule is Security's, unchanged. It also drops some real searches, such as workspace-proxy-configuration or a full environment variable name, and a test pins that. Security suggested requiring a digit in the run if that is too lossy.
  • Earlier prefixes can already have gone out. A term that fails the filter while typing is never sent in any later form, but a safe prefix such as abcdefghij may already have been sent. That comes with the debounce.
  • No retry if the tag is late. If HockeyStack has not loaded when the timer fires, the term counts as sent and is not retried, as before.
  • Osano's API is used as the engine's own bootstrap uses it. I did not verify getConsent() beyond that (ARCHITECTURE.md says so).
  • "Show more" rows. onSelect calls the send for every item. Once fix(search): rank matching sections and expand grouped results #288 merges, its "Show more" action rows would call it too. That is safe, because the term still has to pass the filter and have results, but the handler could skip action rows.

Still open with Security: HockeyStack's vendor review (SOC 2, DPA), its retention period and who has dashboard access, and where the CSP origins for /docs were approved. coder.com's next.config.js already carries the same 19 analytics origins in its /docs policy as ANALYTICS_SCRIPT_ORIGINS here.

QA

  1. Open the preview's docs home page with the browser console open (Chrome: View > Developer > JavaScript Console; Firefox: Tools > Browser Tools > Web Developer Tools > Console).
  2. If window.HockeyStack is undefined in the console (analytics off on the preview), paste window.HockeyStack = { goal: (...a) => console.log("goal", ...a) } first, and then window.Osano = { cm: { getConsent: () => ({ ANALYTICS: "ACCEPT" }) } }. Nothing is sent without that consent. On production, where the real tag and consent banner are loaded, use the HockeyStack dashboard instead.
  3. Press Ctrl+K (Cmd+K on macOS), type workspace proxy at normal speed, then stop. About 1 second later, the console shows one line: goal Docs Search {search_term: 'workspace proxy'}. Nothing is logged while you type.
  4. Wait a few seconds. No second line appears.
  5. Press Enter to open the highlighted result. No second line appears for the same term.
  6. Open search again, type (spaces only), and wait. Nothing is logged.
  7. Type agent, then press Enter at once, before 1 second passes. One line for agent is logged straight away.
  8. Type zzzzqqqq (no results) and wait. Nothing is logged.
  9. Type abcdefghijklmnopqrstuvwxy (25 characters, no spaces) and wait. Nothing is logged. The same happens for me@example.com.
  10. Type workspace proxy, then press Escape before 1 second passes. Nothing is logged.
  11. Paste window.Osano = { cm: { getConsent: () => ({ ANALYTICS: "DENY" }) } }, then repeat step 3. Nothing is logged.

Not verified in Firefox.

Pages to compare

The preview needs docs-pr-preview.yml run on this branch, and its alias needs a Vercel login.

What to check Production Preview
One Docs Search goal per search https://coder.com/docs/ Preview alias from the docs-pr-preview.yml run on vigilante/docs-search-hockeystack

Testing

  • Tests in search-dialog.test.tsx render the dialog with a stubbed window.HockeyStack and window.Osano and cover: one goal for the settled term after several keystrokes, an immediate goal on Enter with no duplicate after the delay, no goal for an empty or whitespace-only search, a zero-result search, results left over from an earlier term, a cancelled search (including that reopening with the same term sends again), consent denied, not chosen, absent and getConsent throwing, and HockeyStack absent, with the wrong shape, or throwing. hybrid-search.test.tsx covers the debounce, a zero-result search and a dismissed search through the hybrid dialog, and search-analytics.test.ts covers the shared term filter: a token, an email, a 101-character term, and a 100-character term that is sent.
  • Against the previous hook the behavioural tests fail: the filter, zero-result, stale-result, cancelled and consent tests, and the wrong-shape tests once a window error listener catches the timer's throw. The tests for a normal term, an immediate send on Enter, the 100-character term and an absent tag pass on both by design.
  • Run one at a time under a memory cap: docs-next 511 pass, docs-config 106, apps/docs 386 (one skipped), docs-next typecheck clean, oxfmt and oxlint clean on the changed files. No browser was available, so the QA steps above are unrun, and actionlint was not run.

Upstream

No. This is Coder's own analytics, not a Fumadocs feature.


Generated by Coder Agents on behalf of @Jesswinters.

🤖 Built with AI assistance.


  • To see the specific tasks where the Asana app for GitHub is being used, see below:

The docs search is a dialog, not a form submit, so HockeyStack cannot see
what readers search for on its own. Both search dialogs now send the
`Docs Search` goal with the trimmed term as `search_term`, once per search:
after typing pauses for a second, or at once when a result is selected,
and never twice for the same term. Empty searches send nothing, and the
call is skipped until the HockeyStack tag has loaded.

Assisted-by: AI
@Jesswinters Jesswinters self-assigned this Nov 13, 2026
@Jesswinters
Jesswinters marked this pull request as ready for review November 13, 2026 16:57
@Jesswinters
Jesswinters requested a review from a team as a code owner November 13, 2026 16:57

@nickvigilante nickvigilante left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The code looks right to me, and I couldn't find a correctness bug. One question needs an answer before merge, plus a few smaller notes.

Before merge: is sending raw search terms to HockeyStack OK with Security? This sends whatever readers type into search to a third party. People paste API tokens, email addresses, internal hostnames and error messages into docs search. No new CSP origin is added, so the AGENTS.md sign-off rule doesn't strictly apply, but it's still a new kind of reader input leaving the site. I'd like Lisa or JD to confirm it's fine. If they want it narrowed, two cheap options are capping search_term at around 100 characters, or skipping terms that look like secrets (for example a long run of characters with no spaces).

Smaller notes

  • A pause mid-word counts as a search: typing "work", pausing a second, then finishing "workspace" sends two goals. That's a reasonable trade-off for a 1-second debounce, but it's worth telling marketing that the term list will include some partial words.
  • The dedupe only resets when the input is cleared. If the dialog keeps its text between opens, closing and reopening search on the same term won't count again. Minor.
  • Nit: "a search before HockeyStack has loaded does not throw" doesn't check that window.HockeyStack is undefined. It relies on the previous test's t.after having removed it. An assert.equal(window.HockeyStack, undefined) at the start would make that explicit.
  • Nit: the body has an Asana link but no Linear link section, which AGENTS.md asks for.

What I checked and found fine

  • Selecting a result still navigates. In fumadocs-ui 16.15.11, SearchDialog runs router.push and closes the dialog before it calls the onSelect prop, so the new callback adds the goal without replacing navigation in either dialog. In the hybrid dialog, the {...props} spread comes after onSelect, but SharedProps carries no onSelect, so nothing overrides it.
  • search-analytics.ts imports only React, so it adds nothing to the first-load bundle.
  • There's no CSP change, and the call does nothing until the tag has loaded, so it stays silent with DOCS_ANALYTICS off or before consent.
  • The tests cover the debounce, an immediate send on Enter with no duplicate, whitespace-only input, and the not-loaded case through both dialogs, with a shared stub in test/hockeystack.ts.

@Jesswinters

Copy link
Copy Markdown
Collaborator Author

@jdomeracki-coder and/or @Lisa-Fiander can one of you confirm that Security is okay with sending raw search terms to HockeyStack?

This sends whatever readers type into search to a third party. People paste API tokens, email addresses, internal hostnames and error messages into docs search. No new CSP origin is added, so the AGENTS.md sign-off rule doesn't strictly apply, but it's still a new kind of reader input leaving the site. I'd like Lisa or JD to confirm it's fine. If they want it narrowed, two cheap options are capping search_term at around 100 characters, or skipping terms that look like secrets (for example a long run of characters with no spaces).

@Lisa-Fiander

Copy link
Copy Markdown

Hey @Jesswinters, a few things to cover and follow up on.

From my understanding, search terms reach HockeyStack as free text, alongside whatever visitor and company identification its tag performs, and they're visible to everyone with HockeyStack dashboard access and exportable from it. The cookie consent banner (Osano) covers tracking readers who accept marketing cookies, but readers don't expect what they type into search to be sent along with it, such as tokens (including Coder API tokens), email addresses, internal hostnames and error messages with secrets in them.

One reason to land all of this in this PR rather than as a follow-up: an archived version embeds whatever the engine built, and per apps/docs/README.md a version only picks up a change when it's republished. If this merges without the fixes and the docs are republished, every published version carries the unfiltered hook, and fixing it later means republishing all of them again.

Required before merge

  1. Only send terms that returned at least one result. Pasted secrets and junk almost never match a docs page, so this removes most of the risk by itself. If marketing wants to know what readers fail to find, send a zero-results flag or count, never the term.

  2. Skip terms that look like secrets or emails. Don't send if the term contains a run of 20+ characters with no spaces (/[A-Za-z0-9_\-.+\/=]{20,}/), which catches Coder API tokens, JWTs, cloud keys and long hashes, or contains an email address (/\S+@\S+\.\S+/). Run this on every send, not just the final term: the 1-second debounce fires per pause, so a term that is typed or edited slowly goes out as a series of growing prefixes, each its own event. Worth knowing that this rule also drops some legitimate searches, for example workspace-proxy-configuration or a full env var name, so either accept that or tighten it by requiring a digit in the run.

  3. Skip terms longer than 100 characters rather than truncating them, since the first 100 characters of a pasted log or error message can still contain a complete secret. Real searches are rarely that long, so this mainly catches pasted logs and documents.

  4. Don't send cancelled searches. SearchProvider in fumadocs-ui 16.15.14 renders the dialog once isOpen !== undefined and keeps it mounted after it closes, and useDocsSearch's search state and the hook's pending timer both survive that, so a term typed and then dismissed with Escape is still sent a second later. Gating the effect on open fixes it.

  5. Tests for each of the above: a token-shaped string, an email, an over-length term, a zero-result search and a cancelled search all send nothing.

Not blocking, but worth doing here

Guard the call: check typeof window.HockeyStack?.goal === "function" and wrap it in try/catch. The tag is loaded as hockeystack@latest, so its shape can change without any change on our side. To be fair about the impact, fumadocs calls the consumer's onSelect after router.push and onOpenChange(false), so a throw here doesn't break navigation; it loses the goal, skips the return-focus bookkeeping and logs an uncaught error. The existing test only covers HockeyStack being absent, not present with the wrong shape.

Questions

Vendor review: has HockeyStack been through our vendor security review (SOC 2 / DPA)? I couldn't find it. Please also note its retention period and who has dashboard access.

Consent category: HockeyStack loads on marketing consent only, and as a non-Google tag it ignores Consent Mode, so a reader who accepted marketing but declined analytics still has their searches captured. Should search capture also require analytics consent? This predates the PR, but the PR is what adds free text to it. If we want that without changing the GTM container, the hook can check window.Osano?.cm?.getConsent()?.ANALYTICS === "ACCEPT" before sending.

CSP sign-off: ARCHITECTURE.md says widening script-src for the analytics origins is a security decision that needs sign-off before the flag is turned on anywhere, and it is on for docs: the publish workflow's site table has analytics on for that site. Can you link where that was approved? While we're there, can you confirm coder.com's next.config.js /docs policy actually carries ANALYTICS_SCRIPT_ORIGINS, since that's step 1 of the four switches and lives outside this repo. If it all matches coder.com's existing tag setup, I'm happy to record it as accepted.

Scope

I've confirmed this only fires on coder.com/docs for a reader who accepted marketing cookies. Previews, local builds and the offline export don't load the tag: the offline export is pinned three ways (turbo never passes DOCS_ANALYTICS to its build, its source names none of the analytics exports, and analytics:check fails in CI on any marker in out/), and apps/docs/vercel.json sets no DOCS_ANALYTICS and ships the tags-off CSP.

Please keep DOCS_ANALYTICS off for the in-product docs too: searches inside a customer's Coder deployment are their data, and their users never saw coder.com's consent banner.

Main's search dialogs now run on experimental_useSearch and take their results as data, so the conflicting dialog edits take main's version.
The goal wiring is restored in the next commit.

Assisted-by: AI
…eted searches

The goal sent every typed term to HockeyStack, where it is visible and exportable, so pasted tokens, emails and logs could leave the reader's browser.
It now sends a term only while the dialog is open, once the term has results, when it passes one shared filter (no run of 20 or more characters without spaces, no email, at most 100 characters, never truncated), and when Osano reports analytics consent as ACCEPT, failing closed otherwise.
The filter runs on every send, because the debounce sends slowly typed terms as growing prefixes.
The call is guarded against a HockeyStack tag of another shape, and a dismissed search clears its pending send.

Assisted-by: AI
@nickvigilante
nickvigilante marked this pull request as draft November 24, 2026 22:53
@Jesswinters

Copy link
Copy Markdown
Collaborator Author

From @nickvigilante:

We agreed that the risk of sending data to HockeyStack exceeded the benefit of tracking it there. We're currently evaluating alternatives, and I've involved Cedric, who built the new docs engine's hybrid search into the mix.

This branch is waiting to be deployed

1 waiting deployment
docs-preview — 86da17dd Waiting Nov 24, 2026 by nickvigilante via preview / deploy preview #125
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.

4 participants