Using the JavaScript Library
Search
For client-side/frontend integrations, Nosto's JavaScript library can be used to simplify the integration. It provides a programming interface to access the Search & Categories API.
For the most basic search the fields
parameter should be provided to specify what product/keyword fields should be returned. Both products
and keywords
can be used separately and together, depending on the use case. For all parameters, see the reference.
The api.search
function also accepts the following options:
Option | Default | Description |
---|---|---|
|
| Automatically redirect if search returns a redirect |
|
| Track search query by provided type |
The function automatically loads session parameters required for personalization & segments in the background.
In order to request custom fields, add the entries "customFields.key"
and "customFields.value"
to the requested product fields. This changes the example above like this
Search page
For a search page in most cases the facets
parameter should be provided.
Also redirect
& track
should be enabled to automatically track searches to Nosto analytics & redirect if API returns a redirect request.
isKeyword
should be set to true
if search is submitted by clicking a keyword, suggested in the autocomplete.
Autocomplete
track
should be enabled to automatically track searches to Nosto analytics.
Category page
For category pages in most cases the facets
parameter should be provided. Additionally categoryId (for Shopify) and categoryPath should be also provided.
Furthermore redirect
& track
should be enabled to automatically track searches to Nosto analytics & redirect if API returns a redirect request.
Session parameters
For some of the search features to work properly, such as personalised results and segments, the search function needs to be able to access information about the user's session from the front-end.
The search function of the JS API already includes session state automatically.
To get all session data the following snippet can be used:
The results of this function should be passed to search query sessionParams parameter. In case search is called from backend, it should pass this data to backend (e.g. using form data).
The function accepts the following options:
Option | Default | Description |
---|---|---|
|
| Maximum execution time in |
|
| Maximum cache time |
Analytics
Tracking search events to analytics can be divided into three parts: search
, search submit
, search product click
. These are user behaviours that should be tracked:
search submit (
type = serp
)faceting, paginating, sorting (
type = serp
) or (type = category
)autocomplete input (
type = autocomplete
)search results product click (
type = serp
), (type = autocomplete
) or (type = category
)autocomplete keyword click (
type = autocomplete
)category merchandising results (
type = category
)
JS API library provides tracking helpers for all of these cases.
Error Handling
On errors from api.search
calls the error object contains a status field that has the HTTP response status which can be used to determine the cause of the error in addition to the error message. The ranges of the status codes used for errors are 400-499 and 500-599. For details on these error codes, go to HTTP response status codes
Search
User actions that lead to search results should be tracked with api.recordSearch()
after search request is done:
search submit (
type = serp
) - user submits search query in autocomplete component and is landed to SERPfaceting, paginating, sorting (
type = serp
) or (type = category
) - user adjusts current search results by filtering (e.g. brand), selecting search page, sorting resultsautocomplete input (
type = autocomplete
) - user sees partial results while typing in search inputcategory merchandising results (
type = category
) - user sees specific category results when category is selected (category merchandising must be implemented)
You don't need to execute api.recordSearch()
if you call api.search(query, { track: 'serp'|'autocomplete'|'category'})
function from JS API, becauseapi.search()
already calls api.recordSearch()
when track
option is provided.
Parameter | Description |
---|---|
type | Search type: |
request | |
result | |
options (optional) | Record search options. Currently is accepts:
|
Example:
Keyword click tracking
Example:
Category results tracking
Example:
The tracking metadata is primarily taken from the third parameter. The full request and full result objects must be provided in the api.recordSearch
call instead of partials.
Search form submit
Search queries are categorised into two groups: organic and non-organic searches.
In order to mark a search query as an organic search you need to call api.recordSearchSubmit(query: string)
. You should call it on search input submit only, before search request is sent.
Organic search - is a search query submitted through search input and which lead to SERP (search engine results page). Following faceting, paginating, sorting queries on organic query is also counted as organic.
Search product click
Product clicks should be tracked in autocomplete component, SERP, category page with api.recordSearchClick()
by providing component (type), where click occurred, and clicked product data:
Parameter | |
---|---|
type | Search type: |
hit | Object containing |
Example:
⚠️ Event Tracking Requirements ⚠️
When tracking events, adherence to the following criteria is essential for capturing detailed and valid data:
query
parameter:The
query
string is an essential component for event tracking.If present, include
products.sort
to track sorting behavior.If applicable, incorporate
products.filter
.
searchResults
parameter:products.hits
array containing objects with aproductId
is mandatory.products.total
number to identify if the search has results.For accurate pagination tracking,
products.from
andproduct.size
must be included.For identifying if the request was autocorrected include
products.fuzzy
.For category requests, either
products.categoryId
orproducts.categoryPath
is mandatory.
💡 Tip: In case of API integration, use this example GraphQL partial query to integrate with the API and retrieve the necessary response data for precise event tracking.
🚩 Search Form Submit
Bear in mind that search queries are split between organic and non-organic searches. To classify a search as organic, it is crucial to invoke api.recordSearchSubmit()
upon the search input submission, before the actual search request is dispatched. This step is pivotal in ensuring the seamless tracking of organic searches through to the SERP.
🔍 Accurate Click Tracking
Tracking product clicks is fundamental for understanding user interaction. Use api.recordSearchClick()
to monitor this actions correctly, specifying the type
and relevant hit data.
Last updated