Skip to content

Commit

Permalink
Fix duplicate search analytics event captures (#535)
Browse files Browse the repository at this point in the history
This pr adds the following:
1. prevents unnecessary analytics event captures when user erases query
& user navigates through results with arrow keys
2. changes docs page legacy banner prose (v.1.0 -> v1.0)
  • Loading branch information
ben-fornefeld authored Jan 13, 2025
2 parents 87b1a3f + 8992302 commit 7f57364
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion apps/web/src/components/LegacyBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function LegacyBanner() {
<AlertCircle className="h-4 w-4 text-brand-400/80" />
<span>
You are reading a{' '}
<span className="text-brand-400/90">legacy (pre v.1.0)</span>{' '}
<span className="text-brand-400/90">legacy (pre v1.0)</span>{' '}
document.
</span>
</div>
Expand Down
11 changes: 8 additions & 3 deletions apps/web/src/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ function useAutocomplete({ close }: { close: () => void }) {
>({})

const captureSearchEvent = useDebounceCallback(
(query: string, results_count: number) =>
(query: string, results_count: number) => {
// return when length <= one, because this occurs when query is
// erased & last event on debounce stack is let through with 1 char
if (query.length <= 1) return

posthog.capture('searched docs', {
query,
results_count,
}),
})
},
500
)

Expand Down Expand Up @@ -95,7 +100,7 @@ function useAutocomplete({ close }: { close: () => void }) {
onStateChange({ state }) {
setAutocompleteState(state)

if (state.query) {
if (state.query && state.status === 'loading') {
captureSearchEvent(
state.query,
state.collections[0]?.items.length || 0
Expand Down

0 comments on commit 7f57364

Please sign in to comment.