Skip to content

Add PostHog events for Ask AI and docs search#1493

Open
samseely wants to merge 3 commits into
mainfrom
cursor/posthog-events-search-ask-ai-c43c
Open

Add PostHog events for Ask AI and docs search#1493
samseely wants to merge 3 commits into
mainfrom
cursor/posthog-events-search-ask-ai-c43c

Conversation

@samseely

@samseely samseely commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Description

This PR instruments both the docs search and Ask AI experiences with PostHog events to help understand how customers interact with these features.

All events follow the {noun}-{verb}-client naming convention.

Search events tracked:

  • docs-search-opened-client - When search is opened via hotkey (/ or Cmd+K) or by typing a query
  • docs-search-result-selected-client - When a user selects a search result, with properties:
    • result_type: doc, endpoint, or ask_ai
    • path: The path of the selected result (for docs/endpoints)
    • query: The search query
    • query_length: Length of the search query
    • selection_method: keyboard, keyboard_enter, or click (no value)
    • device_type: mobile or desktop (for Ask AI selections)

Ask AI events tracked:

  • ask-ai-sidebar-opened-client - When the sidebar is opened, with source property (button or search)
  • ask-ai-sidebar-closed-client - When the sidebar is closed
  • ask-ai-session-switched-client - When switching between chat sessions, with message_count
  • ask-ai-message-sent-client - When a message is sent, with properties:
    • message_length: Length of the message
    • is_first_message: Whether this is the first message in the session
    • is_new_session: Whether this is a new session
    • conversation_length: Number of messages in the conversation
  • ask-ai-stream-stopped-client - When the user stops the AI stream
  • ask-ai-mobile-modal-opened-client - When the mobile modal is opened (via search or floating button)

Implementation details:

  • Uses the existing posthog.track() function from lib/posthog.ts which was already set up but unused
  • Events are tracked at the appropriate interaction points in the components
  • TypeScript type checking and linting both pass

Linear Issue: KNO-13817

Open in Web Open in Cursor 

- Track search events: opened (hotkey/query), result selected (doc/endpoint/ask_ai)
- Track Ask AI sidebar events: opened, closed, session switched
- Track Ask AI chat events: message sent, stream stopped
- Track mobile Ask AI modal events: opened from search or floating button

Each event includes relevant metadata like query length, result type,
selection method, message length, and conversation context.

Co-authored-by: Sam Seely <samseely@gmail.com>
@vercel

vercel Bot commented Jun 17, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docs Ready Ready Preview, Comment Jun 17, 2026 4:55pm

Request Review

@samseely samseely requested a review from cjbell June 17, 2026 16:04
@samseely samseely marked this pull request as ready for review June 17, 2026 16:04
@samseely

Copy link
Copy Markdown
Contributor Author

Validated that these are working on dev

@cursor cursor Bot 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.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 81c0353. Configure here.

Comment thread components/ui/Autocomplete.tsx
}
return !prev;
});
}, []);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Side effect inside React state updater function

Low Severity

posthog.track is called inside the setIsOpen state updater function in toggleSidebar. React state updater functions are expected to be pure — in development with Strict Mode, React may invoke them twice, which would fire duplicate tracking events. The tracking call belongs outside the updater, using the previous state value from a ref or a separate check.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 81c0353. Configure here.

@cursor cursor Bot 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.

Risk HIGH: Adds PostHog analytics event tracking to the Ask AI sidebar, mobile modal, docs search, and chat stream components.

Reasons

  • Multiple .tsx files in components/ are modified (AiChatButton.tsx, AskAiContext.tsx, ui/Autocomplete.tsx), triggering HIGH risk per classification rules
  • A .ts file in hooks/ (useChatStream.ts) is also modified with new tracking logic
  • components/ui/Autocomplete.tsx is a shared/reusable UI component with 99 lines added, which triggers the shared component HIGH risk rule
  • Total of 165 additions across 4 TypeScript/React files that affect core interactive features (search and AI chat)
  • Cursor Bugbot flagged 2 potential issues in its review that should be evaluated

Notes

  • Verify that the PostHog tracking calls in Autocomplete.tsx do not introduce duplicate events (there are multiple docs_search_result_selected tracking points for click, keyboard, and keyboard_enter paths)
  • Check that the onStateChange handler in the autocomplete useMemo correctly uses prevState — this is a new parameter being destructured that needs to be supported by the createAutocomplete API
  • Review the hasTrackedOpenRef pattern for potential stale ref issues in the useMemo dependency array (empty deps [])
  • Confirm Bugbot's 2 flagged issues have been addressed or are false positives
  • Consider whether the toggleSidebar callback's use of the state updater function with side effects (posthog.track inside setIsOpen) follows React best practices
Open in Web View Automation 

Sent by Cursor Automation: Docs PR classifier

Comment thread components/AiChatButton.tsx Outdated
} = useInkeepSettings();

const openWithPrompt = useCallback((prompt: string) => {
posthog.track("ask_ai_mobile_modal_opened", {

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.

I want us to align on a convention for event names. Elsewhere we use {noun}-{verb}-client so would propose we do the same here but with docs as the suffix?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ope sorry my bad. should have noticed that. another case for a skill for this stuff, though i supposed we'd need it available in all of our repos? i'll work with cursor on this.

i don't think we should use docs in the event name because that is already in url data. good w that?

so yes it'd be noun-verb-client for all of these

Changed all event names from snake_case to kebab-case with -client suffix:
- docs_search_opened → docs-search-opened-client
- docs_search_result_selected → docs-search-result-selected-client
- ask_ai_sidebar_opened → ask-ai-sidebar-opened-client
- ask_ai_sidebar_closed → ask-ai-sidebar-closed-client
- ask_ai_session_switched → ask-ai-session-switched-client
- ask_ai_message_sent → ask-ai-message-sent-client
- ask_ai_stream_stopped → ask-ai-stream-stopped-client
- ask_ai_mobile_modal_opened → ask-ai-mobile-modal-opened-client

Co-authored-by: Sam Seely <samseely@gmail.com>
Set hasTrackedOpenRef.current = true after firing the hotkey event
to prevent onStateChange from firing a second event with trigger: query
when the user subsequently types a search query.

Co-authored-by: Sam Seely <samseely@gmail.com>
@samseely

Copy link
Copy Markdown
Contributor Author

@cjbell we should be set here now

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.

3 participants