mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-22 19:07:54 +01:00
feat: track interaction with search (#7526)
Now we will start tracking how users interact with search box. Whether they open it manually or use CTRL K
This commit is contained in:
parent
72615cc6d5
commit
57c1a6edd5
@ -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;
|
||||
@ -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?.();
|
||||
@ -136,6 +139,7 @@ export const Search = ({
|
||||
preventDefault: true,
|
||||
},
|
||||
() => {
|
||||
setUsedHotkey(true);
|
||||
if (document.activeElement === searchInputRef.current) {
|
||||
searchInputRef.current?.blur();
|
||||
} else {
|
||||
@ -143,6 +147,27 @@ export const Search = ({
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
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();
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user