Skip to content

Commit

Permalink
feat: Plausible search (#4625)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Sep 7, 2023
1 parent 31ed96d commit 15015f7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
5 changes: 1 addition & 4 deletions frontend/src/component/common/Search/Search.test.tsx
Expand Up @@ -21,10 +21,7 @@ const testDisplayComponent = (
);

test('should read saved query from local storage', async () => {
const { value, setValue } = createLocalStorage(
'Search:localStorageId:v1',
{}
);
const { setValue } = createLocalStorage('Search:localStorageId:v1', {});
setValue({
query: 'oldquery',
});
Expand Down
Expand Up @@ -58,7 +58,9 @@ export const SearchDescription: VFC<ISearchDescriptionProps> = ({
{filter.values.join(',')}
</StyledCode>{' '}
in {filter.header}. Options:{' '}
{filter.options.slice(0, 10).join(', ')}
{[...new Set(filter.options)]
.slice(0, 10)
.join(', ')}
</p>
))}
</>
Expand Down
Expand Up @@ -13,6 +13,7 @@ import {
SearchInstructions,
StyledCode,
} from './SearchInstructions/SearchInstructions';
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';

const StyledPaper = styled(Paper)(({ theme }) => ({
position: 'absolute',
Expand Down Expand Up @@ -55,13 +56,12 @@ interface SearchSuggestionsProps {

const quote = (item: string) => (item.includes(' ') ? `"${item}"` : item);

const randomIndex = (arr: any[]) => Math.floor(Math.random() * arr.length);

export const SearchSuggestions: VFC<SearchSuggestionsProps> = ({
getSearchContext,
onSuggestion,
savedQuery,
}) => {
const { trackEvent } = usePlausibleTracker();
const searchContext = getSearchContext();

const filters = getFilterableColumns(searchContext.columns)
Expand Down Expand Up @@ -116,7 +116,14 @@ export const SearchSuggestions: VFC<SearchSuggestionsProps> = ({
<StyledBox>
<StyledHistory />
<StyledCode
onClick={() => onSuggestion(savedQuery || '')}
onClick={() => {
onSuggestion(savedQuery || '');
trackEvent('search-filter-suggestions', {
props: {
eventType: 'saved query',
},
});
}}
>
<span>{savedQuery}</span>
</StyledCode>
Expand Down Expand Up @@ -146,7 +153,14 @@ export const SearchSuggestions: VFC<SearchSuggestionsProps> = ({
searchableColumnsString={
searchableColumnsString
}
onClick={onSuggestion}
onClick={suggestion => {
onSuggestion(suggestion);
trackEvent('search-filter-suggestions', {
props: {
eventType: 'filter',
},
});
}}
/>
}
/>
Expand All @@ -159,9 +173,16 @@ export const SearchSuggestions: VFC<SearchSuggestionsProps> = ({
show="Combine filters and search: "
/>
<StyledCode
onClick={() =>
onSuggestion(selectedFilter + ' ' + suggestedTextSearch)
}
onClick={() => {
onSuggestion(
selectedFilter + ' ' + suggestedTextSearch
);
trackEvent('search-filter-suggestions', {
props: {
eventType: 'search and filter',
},
});
}}
>
<span key={selectedFilter}>{selectedFilter}</span>{' '}
<span>{suggestedTextSearch}</span>
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/hooks/usePlausibleTracker.ts
Expand Up @@ -43,7 +43,8 @@ export type CustomEvents =
| 'strategy-add'
| 'playground'
| 'feature-type-edit'
| 'strategy-variants';
| 'strategy-variants'
| 'search-filter-suggestions';

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

0 comments on commit 15015f7

Please sign in to comment.