Add PostHog events for Ask AI and docs search#1493
Conversation
- 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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Validated that these are working on dev |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ 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.
| } | ||
| return !prev; | ||
| }); | ||
| }, []); |
There was a problem hiding this comment.
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.
Reviewed by Cursor Bugbot for commit 81c0353. Configure here.
There was a problem hiding this comment.
Risk HIGH: Adds PostHog analytics event tracking to the Ask AI sidebar, mobile modal, docs search, and chat stream components.
Reasons
- Multiple
.tsxfiles incomponents/are modified (AiChatButton.tsx,AskAiContext.tsx,ui/Autocomplete.tsx), triggering HIGH risk per classification rules - A
.tsfile inhooks/(useChatStream.ts) is also modified with new tracking logic components/ui/Autocomplete.tsxis 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.tsxdo not introduce duplicate events (there are multipledocs_search_result_selectedtracking points for click, keyboard, and keyboard_enter paths) - Check that the
onStateChangehandler in the autocompleteuseMemocorrectly usesprevState— this is a new parameter being destructured that needs to be supported by thecreateAutocompleteAPI - Review the
hasTrackedOpenRefpattern for potential stale ref issues in theuseMemodependency array (empty deps[]) - Confirm Bugbot's 2 flagged issues have been addressed or are false positives
- Consider whether the
toggleSidebarcallback's use of the state updater function with side effects (posthog.trackinsidesetIsOpen) follows React best practices
Sent by Cursor Automation: Docs PR classifier
| } = useInkeepSettings(); | ||
|
|
||
| const openWithPrompt = useCallback((prompt: string) => { | ||
| posthog.track("ask_ai_mobile_modal_opened", { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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>
|
@cjbell we should be set here now |




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}-clientnaming convention.Search events tracked:
docs-search-opened-client- When search is opened via hotkey (/orCmd+K) or by typing a querydocs-search-result-selected-client- When a user selects a search result, with properties:result_type:doc,endpoint, orask_aipath: The path of the selected result (for docs/endpoints)query: The search queryquery_length: Length of the search queryselection_method:keyboard,keyboard_enter, or click (no value)device_type:mobileordesktop(for Ask AI selections)Ask AI events tracked:
ask-ai-sidebar-opened-client- When the sidebar is opened, withsourceproperty (buttonorsearch)ask-ai-sidebar-closed-client- When the sidebar is closedask-ai-session-switched-client- When switching between chat sessions, withmessage_countask-ai-message-sent-client- When a message is sent, with properties:message_length: Length of the messageis_first_message: Whether this is the first message in the sessionis_new_session: Whether this is a new sessionconversation_length: Number of messages in the conversationask-ai-stream-stopped-client- When the user stops the AI streamask-ai-mobile-modal-opened-client- When the mobile modal is opened (via search or floating button)Implementation details:
posthog.track()function fromlib/posthog.tswhich was already set up but unusedLinear Issue: KNO-13817