Skip to content

Commit

Permalink
feat: track interaction with search (#7526)
Browse files Browse the repository at this point in the history
Now we will start tracking how users interact with search box. Whether
they open it manually or use CTRL K
  • Loading branch information
sjaanus committed Jul 3, 2024
1 parent 31e215a commit 42eaeb5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
25 changes: 25 additions & 0 deletions frontend/src/component/common/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useOnClickOutside } from 'hooks/useOnClickOutside';
import { useSavedQuery } from './useSavedQuery';
import { useOnBlur } from 'hooks/useOnBlur';
import { SearchHistory } from './SearchSuggestions/SearchHistory';
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';

interface ISearchProps {
id?: string;
Expand Down Expand Up @@ -110,9 +111,11 @@ export const Search = ({
debounceTime = 200,
...rest
}: ISearchProps) => {
const { trackEvent } = usePlausibleTracker();
const searchInputRef = useRef<HTMLInputElement>(null);
const searchContainerRef = useRef<HTMLInputElement>(null);
const [showSuggestions, setShowSuggestions] = useState(false);
const [usedHotkey, setUsedHotkey] = useState(false);
const hideSuggestions = () => {
setShowSuggestions(false);
onBlur?.();
Expand All @@ -136,13 +139,35 @@ export const Search = ({
preventDefault: true,
},
() => {
setUsedHotkey(true);
if (document.activeElement === searchInputRef.current) {
searchInputRef.current?.blur();
} else {
searchInputRef.current?.focus();
}
},
);

useEffect(() => {
if (!showSuggestions) {
return;
}
if (usedHotkey) {
trackEvent('search-opened', {
props: {
eventType: 'hotkey',
},
});
} else {
trackEvent('search-opened', {
props: {
eventType: 'manual',
},
});
}
setUsedHotkey(false);
}, [showSuggestions]);

useKeyboardShortcut({ key: 'Escape' }, () => {
if (searchContainerRef.current?.contains(document.activeElement)) {
searchInputRef.current?.blur();
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/hooks/usePlausibleTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export type CustomEvents =
| 'feature-lifecycle'
| 'command-bar'
| 'new-in-unleash-click'
| 'new-in-unleash-dismiss';
| 'new-in-unleash-dismiss'
| 'search-opened';

export const usePlausibleTracker = () => {
const plausible = useContext(PlausibleContext);
Expand Down

0 comments on commit 42eaeb5

Please sign in to comment.