Skip to content

Commit 83804c7

Browse files
heiskrpeterbePeter Bengtsson
authored
Create search result event (#22307)
* Create search result event * Update Search.tsx * actually send it * remove comment * Apply suggestions from code review Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com> * add 'required' to schema Co-authored-by: Peter Bengtsson <mail@peterbe.com> Co-authored-by: Peter Bengtsson <peterbe@github.com>
1 parent cd74b07 commit 83804c7

4 files changed

Lines changed: 77 additions & 3 deletions

File tree

components/Search.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export function Search({
8888
case 'Enter':
8989
// look for a link in the given hit, then visit it
9090
if (results === null || activeHit === 0 || !results.length) break
91-
window.location.href = results[activeHit - 1]?.url
91+
onGotoResult(results[activeHit - 1]?.url, activeHit)
9292
break
9393
}
9494
}
@@ -159,6 +159,20 @@ export function Search({
159159
evt.preventDefault()
160160
}
161161

162+
function onGotoResult(url: string, index: number) {
163+
if (results) {
164+
sendEvent({
165+
type: EventType.searchResult,
166+
search_result_query: Array.isArray(query) ? query[0] : query,
167+
search_result_index: index,
168+
search_result_total: results.length,
169+
search_result_rank: (results.length - index) / results.length,
170+
search_result_url: url,
171+
})
172+
}
173+
document.location.href = url
174+
}
175+
162176
const SearchResults = (
163177
<>
164178
<div
@@ -176,6 +190,7 @@ export function Search({
176190
results={results}
177191
activeHit={activeHit}
178192
setActiveHit={setActiveHit}
193+
onGotoResult={onGotoResult}
179194
/>
180195
</div>
181196
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
@@ -244,12 +259,14 @@ function ShowSearchResults({
244259
results,
245260
activeHit,
246261
setActiveHit,
262+
onGotoResult,
247263
}: {
248264
isOverlay: boolean
249265
isLoading: boolean
250266
results: SearchResult[] | null
251267
activeHit: number
252268
setActiveHit: (index: number) => void
269+
onGotoResult: (url: string, index: number) => void
253270
}) {
254271
const { t } = useTranslation('search')
255272

@@ -286,7 +303,14 @@ function ShowSearchResults({
286303
onMouseEnter={() => setActiveHit(index)}
287304
>
288305
<div className={cx('py-3 px-3', isActive && 'color-border-secondary')}>
289-
<a className="no-underline color-text-primary" href={url}>
306+
<a
307+
className="no-underline color-text-primary"
308+
href={url}
309+
onClick={(event) => {
310+
event.preventDefault()
311+
onGotoResult(url, index)
312+
}}
313+
>
290314
{/* Breadcrumbs in search records don't include the page title. These fields may contain <mark> elements that we need to render */}
291315
<div
292316
className={'d-block opacity-60 text-small pb-1'}

components/lib/events.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export enum EventType {
3838
exit = 'exit',
3939
link = 'link',
4040
search = 'search',
41+
searchResult = 'searchResult',
4142
navigate = 'navigate',
4243
survey = 'survey',
4344
experiment = 'experiment',
@@ -58,6 +59,11 @@ type SendEventProps = {
5859
link_url?: string
5960
search_query?: string
6061
search_context?: string
62+
search_result_query?: string
63+
search_result_index?: number
64+
search_result_total?: number
65+
search_result_rank?: number
66+
search_result_url?: string
6167
navigate_label?: string
6268
survey_token?: string // Honeypot, doesn't exist in schema
6369
survey_vote?: boolean
@@ -260,7 +266,7 @@ export default function initializeEvents() {
260266
initPrintEvent()
261267
// survey event in ./survey.js
262268
// experiment event in ./experiment.js
263-
// search event in ./search.js
269+
// search and search_result event in ./search.js
264270
// redirect event in middleware/record-redirect.js
265271
// preference event in ./display-tool-specific-content.js
266272
}

lib/hydro.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const SCHEMAS = {
88
exit: 'docs.v0.ExitEvent',
99
link: 'docs.v0.LinkEvent',
1010
search: 'docs.v0.SearchEvent',
11+
searchResult: 'docs.v0.SearchResultEvent',
1112
navigate: 'docs.v0.NavigateEvent',
1213
survey: 'docs.v0.SurveyEvent',
1314
experiment: 'docs.v0.ExperimentEvent',

lib/schema-event.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,48 @@ const searchSchema = {
257257
},
258258
}
259259

260+
const searchResultSchema = {
261+
type: 'object',
262+
additionalProperties: false,
263+
required: [
264+
'type',
265+
'context',
266+
'search_result_query',
267+
'search_result_index',
268+
'search_result_total',
269+
'search_result_rank',
270+
'search_result_url',
271+
],
272+
properties: {
273+
context,
274+
type: {
275+
type: 'string',
276+
pattern: '^searchResult$',
277+
},
278+
search_result_query: {
279+
type: 'string',
280+
description: 'The query the user searched for.',
281+
},
282+
search_result_index: {
283+
type: 'number',
284+
description: 'The order position of the user selected search result.',
285+
},
286+
search_result_total: {
287+
type: 'number',
288+
description: 'The total number of search results we returned for the query.',
289+
},
290+
search_result_rank: {
291+
type: 'number',
292+
description:
293+
'The rank score of the order position of the search result, example: `(total - index) / total`.',
294+
},
295+
search_result_url: {
296+
type: 'string',
297+
description: 'The destination url of the search result the user selected.',
298+
},
299+
},
300+
}
301+
260302
const navigateSchema = {
261303
type: 'object',
262304
additionalProperties: false,
@@ -425,6 +467,7 @@ export default {
425467
exitSchema,
426468
linkSchema,
427469
searchSchema,
470+
searchResultSchema,
428471
navigateSchema,
429472
surveySchema,
430473
experimentSchema,

0 commit comments

Comments
 (0)