Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
When submitting Search results through Autocomplete, submit
callback is called on these events:
Enter
key press.
Submit button
click.
Keyword
click.
By default submit
checks if query/keyword length satisfies minQueryLength
, sends Search Submit event
to Nosto Analytics, and sends Search request to the Nosto Search API.
In the usual scenario, you want to render Search Results on submit, so you should override submit
function:
To disable submit
pass undefined
value.
\
Setting nostoAnalytics: true
will enable Nosto Analytics tracking. Tracking results can be seen in the Nosto Dashboard under Search & Categories -> Analytics page.
❗Note: you should additionally add click events on your search results page according to Nosto Tech Docs with type: serp || category
according to the results page type.❗
\
By default we send pageview
events to existing GA tag, found in shop site. To send pageview
events with correct search information, a minimal configuration is needed in googleAnalytics
property.
serpPath
- Search query url parameter name
queryParamName
- Search query url parameter name
enabled
- Enable Google Analytics
For example, if search results URL is https://examplenostoshop.com/search-results?query=shoes, then configuration should be:
To disable Google Analytics, set googleAnalytics: false
.
Nosto Autocomplete library is designed to simplify the implementation of Search Autocomplete functionality by providing: 🌟 Autocomplete products, keywords and history visualization. 🔄 Automatic bindings to Nosto Search API. 🧩 Autocomplete component state management. 📊 Nosto Analytics out of the box, Google Analytics support. 🏗️ Default Autocomplete components and templates. 🎮 Keyboard navigation.
You can install the Nosto Autocomplete
library via npm:
The Nosto Autocomplete library can be imported and used in various ways, depending on your preferred framework or template language. Some of the supported import methods include:
Base
import { autocomplete } from "@nosto/autocomplete"
Mustache
import { autocomplete, fromMustacheTemplate, defaultMustacheTemplate } from "@nosto/autocomplete/mustache"
Liquid
import { autocomplete, fromLiquidTemplate, defaultLiquidTemplate } from "@nosto/autocomplete/liquid"
Preact
import { autocomplete, Autocomplete } from "@nosto/autocomplete/preact"
React
import { autocomplete, Autocomplete } from "@nosto/autocomplete/react"
Choose the import method that aligns with your project's requirements and technology stack.
❗Do not combine multiple imports as it will fetch multiple bundles.❗
The library handles events through dataset
properties to avoid handling logic in a template. These data-*
attributes are used:
data-ns-hit
- this attribute should be used on clickable keyword
, product
, history
list elements. Stringified unmodified JSON object (product, keyword or history hit) from the search response should be provided as a value. You will need to escape it in Liquid and Mustache templates.
This attribute handles submit keyword/history as search, redirect to product, analytics (if enabled) request.
data-ns-remove-history
- should be used to delete history entries in the autocomplete.
To make an element delete a single history entry when clicked, add data-ns-remove-history={hit.item}
to an element.
To delete all history entries, add data-ns-remove-history="all"
to clear button.
Template examples: Mustache, Liquid, React/Preact
Mustache is based on logic-less templates which can be enhanced with helpers, e.g toJson
, imagePlaceholder
, showListPrice
in example template.
Once the autocomplete component binds to input via inputSelector
and dropdownSelector
, it then renders autocomplete provided in a render
function. It is called on input focus and change events, and renders a dropdown element with the current search result state:
if input is empty and history entries exist, it renders dropdown with history list,
if input is not empty and it passes minQueryLength
rule, it render dropdown with keywords and products.
Render can be adjusted to the desired framework. Moreover, the library provides helpers for Mustache/Liquid template languages.
Examples
Suppose index.html
is
List of autocomplete
initialization examples:
Liquid
Example below uses fromLiquidTemplate
helper which renders string template. Library provides default autocomplete template via defaultLiquidTemplate
and default css for default template:
The template also can be loaded from a file. The library includes a default template, equivalent to string template in above example:
Mustache Mustache template is rendered similarly as Liquid template in the above example:
Or from a file:
React/Preact
One way to initialize autocomplete in a React app, is to call autocomplete
from the useEffect
on component mount, using default <Autocomplete />
component and styles:
The Preact solution does not differ from React a lot:
After installation, you can import and use the library in your JavaScript or TypeScript files. Library attaches to existing search input element. Once a search input is loaded, autocomplete
function can initialize event listeners for the Search Autocomplete.
❗autocomplete
should be called once.❗
inputSelector
Yes
N/A
Input element to attach the autocomplete to
dropdownSelector
Yes
N/A
Dropdown element to attach the autocomplete to (empty container's selector should be provided)
render
Yes
N/A
Function to render the dropdown
fetch
Yes
N/A
Function to fetch the search state
submit
No
Search API request
Function to submit the search
minQueryLength
No
2
Minimum length of the query before searching (applied on typing in autocomplete and used in default submit
implementation)
historyEnabled
No
true
Enables search history component
historySize
No
5
Max number of history items to show
nostoAnalytics
No
true
Enable Nosto Analytics
googleAnalytics
No
{ serpPath: "search", queryParamName: "query", enabled: true }
Google Analytics configuration. Set to false
to disable
hitDecorators
No
N/A
Decorate each search hit before rendering
See to see more detailed documentation of the library.