Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions src/layouts/search.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,25 @@
const searchClient = {
...baseClient,
search(requests) {
if (exactMatch) {
requests = requests.map((request) => {
const params = { ...(request.params || {}) };
if (params.query) {
params.typoTolerance = false;
params.removeWordsIfNoResults = 'none';
params.optionalWords = [];
}
return { ...request, params };
});
}
requests = requests.map((request) => {
const params = { ...(request.params || {}) };
// Keep empty "root" queries out of Algolia Search analytics. These
// fire on initial page load and during facet-only browsing (no term
// typed). They still return results, but `analytics: false` stops
// them being recorded as a searched term — otherwise they show up as
// "<empty search>" and dominate the report and skew average CTR.
if (!params.query) {
params.analytics = false;
}
// Exact match: require every term, exact spelling, no optional-word
// fallback. Only meaningful when there's an actual query.
if (exactMatch && params.query) {
params.typoTolerance = false;
params.removeWordsIfNoResults = 'none';
params.optionalWords = [];
}
return { ...request, params };
});
return baseClient.search(requests);
},
};
Expand All @@ -81,6 +89,12 @@
const search = instantsearch({
searchClient,
indexName,
// Enable click/conversion analytics. Reuses the global `aa`
// (search-insights) already initialised in algolia-script.hbs, sets
// clickAnalytics on queries, and attaches a queryID so the click
// events we send below are attributed to the search that produced them.
// https://www.algolia.com/doc/api-reference/widgets/insights/js/
insights: true,
routing: {
stateMapping: {
// Convert UI state to URL parameters
Expand Down Expand Up @@ -137,18 +151,23 @@
instantsearch.widgets.hits({
container: '.results',
templates: {
item: (hit, { html, components }) => {
item: (hit, { html, components, sendEvent }) => {
// Record a click event attributed to the current query (queryID)
// so Algolia can report CTR/CVR for the results page. Uses
// navigator.sendBeacon under the hood, so it survives the
// navigation triggered by the link.
const onHitClick = () => sendEvent('click', hit, 'Hit Clicked');
const isExternal = hit.objectID.startsWith('https');
if (isExternal) {
return html`
<div class="card">
<h2 class="ais-Heading">
<a href="${hit.objectID}" target="_blank">
<a href="${hit.objectID}" target="_blank" onClick=${onHitClick}>
${components.Highlight({ attribute: 'title', hit })}
</a>
</h2>
<p>
<a href="${hit.objectID}">
<a href="${hit.objectID}" onClick=${onHitClick}>
${components.Snippet({ attribute: 'intro', hit })}
</a>
</p>
Expand All @@ -164,10 +183,10 @@
return html`
<div class="card">
<h2 class="ais-Heading">
<a href="${hit.objectID}">${components.Highlight({ attribute: 'title', hit })}</a>
<a href="${hit.objectID}" onClick=${onHitClick}>${components.Highlight({ attribute: 'title', hit })}</a>
</h2>
<p>
<a href="${hit.objectID}">${components.Snippet({ attribute: 'intro', hit })}</a>
<a href="${hit.objectID}" onClick=${onHitClick}>${components.Snippet({ attribute: 'intro', hit })}</a>
</p>
<div class="aa-ItemContentRow">
<div class="aa-ItemContentTitle result-type">${hit.type}</div>
Expand Down
Loading