fix search issue analytics tracking

Signed-off-by: Emma Indal <emma.indahl@gmail.com>
This commit is contained in:
Emma Indal
2023-12-18 16:24:45 +01:00
parent f52c4306ba
commit 6f280fa01a
3 changed files with 64 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-search-react': patch
---
Capture analytics even when number of results is not available, since the total result count is not something that is always available for all search engines and configurations.
@@ -479,4 +479,61 @@ describe('SearchContext', () => {
});
});
});
it('captures analytics events even if number of results does not exist', async () => {
const analyticsApiMock = {
captureEvent: jest.fn(),
} satisfies typeof analyticsApiRef.T;
searchApiMock.query.mockResolvedValue({
results: [],
});
const { result } = renderHook(() => useSearch(), {
wrapper: ({ children }) => {
const configApiMock = new MockConfigApi({});
return (
<TestApiProvider
apis={[
[configApiRef, configApiMock],
[searchApiRef, searchApiMock],
[analyticsApiRef, analyticsApiMock],
]}
>
<SearchContextProvider initialState={initialState}>
{children}
</SearchContextProvider>
</TestApiProvider>
);
},
});
await waitFor(() => {
expect(result.current).toEqual(expect.objectContaining(initialState));
});
const term = 'term';
await act(async () => {
result.current.setTerm(term);
});
await waitFor(() => {
expect(searchApiMock.query).toHaveBeenLastCalledWith({
term: 'term',
types: ['*'],
filters: {},
});
expect(analyticsApiMock.captureEvent).toHaveBeenCalledWith({
action: 'search',
subject: 'term',
value: undefined,
context: {
extension: 'App',
pluginId: 'root',
routeRef: 'unknown',
},
});
});
});
});
@@ -138,9 +138,9 @@ const useSearchContextValue = (
pageLimit,
pageCursor,
});
if (term && resultSet.numberOfResults !== undefined) {
if (term) {
analytics.captureEvent('search', term, {
value: resultSet.numberOfResults,
value: result.value?.numberOfResults ?? undefined,
});
}
return resultSet;