Search widget
The widget is a drop-in search UI for any website. One script tag gives you:
- autocomplete under your search boxes: matching items with images and prices, suggested searches, categories, and an “Ask AI” shortcut;
- a results page with filters, sorting, paging and AI answers, which keeps its state in the URL so results can be shared and the back button works;
- related items on product and article pages, and trending items anywhere, such as the home page;
- analytics: clicks, views and conversions are reported for you.
It adapts to Arabic (right to left), works on phones, and is keyboard and screen-reader accessible. It’s the same widget the WordPress plugin uses.
Add it to your site
Section titled “Add it to your site”-
Configure it and load the script before the closing
</body>tag, with your app ID and search key:<script>window.shoofConfig = {appId: "my-store",apiKey: "shf_pub_…",resultsUrl: "/search",queryParam: "q",};</script><script src="https://api.shoofai.co/widget/shoof.js" defer></script> -
Autocomplete attaches to every input matching
inputSelector. By default that’sinput[name="s"],input.shoof-searchand[data-shoof-input]:<form action="/search"><input type="search" name="q" data-shoof-input placeholder="Search…" /></form>Pressing Enter goes to
resultsUrlwith the query inqueryParam. -
The results page. On your search page, add an element with
data-shoof-results. It reads the query from the URL:<div data-shoof-results></div> -
Related items. On an item’s page, add an element with
data-shoof-relatedset to the record’s ID:<div data-shoof-related="sku-1-en" data-limit="4" data-title="You may also like"></div> -
Trending items. Anywhere, such as the home page, add an element with
data-shoof-trending, set to a record type to show only that type:<div data-shoof-trending="product" data-limit="4" data-title="Trending now"></div>It stays empty until something trends.
Data attributes
Section titled “Data attributes”| Attribute | On | Does |
|---|---|---|
data-shoof-input |
<input> |
Adds autocomplete (with the default inputSelector). |
data-shoof-results |
any element | Mounts the results page. |
data-search-box="false" |
the results element | Hides the results page’s own search box. |
data-filters='{"type":["product"]}' |
the results element | Filters for this block only. |
data-rule-contexts="sale,mobile" |
the results element | Rule contexts for this block. |
data-shoof-related="<id>" |
any element | Mounts related items for that record. |
data-shoof-trending="<type>" |
any element | Mounts trending items, of that type if set. |
data-limit, data-title |
the related or trending element | How many items, and a heading. |
Options
Section titled “Options”Set them on window.shoofConfig, or pass them to Shoof.init().
| Option | Type | Default | Does |
|---|---|---|---|
appId |
string | required | Your application ID. |
apiKey |
string | required | A search key, or a secured key. |
host |
string | https://api.shoofai.co |
The API host. |
lang |
string | the page’s <html lang> |
The language of the UI and of searches. Without one, the UI is in English and searches cover every language. |
currency |
string | ISO code for prices without their own currency, such as AED. |
|
inputSelector |
string | see above | The inputs to enhance with autocomplete. |
resultsUrl |
string | the form’s action |
Where Enter takes the visitor. |
queryParam |
string | s |
The query string parameter for the query. |
fallbackUrl |
string | Your site’s own search, with {q} for the query, such as /?s={q}. When Shoof AI can’t answer, or you’re out of searches, the results page sends the visitor there instead of saying search is unavailable. An optional {reason} in it becomes limit when you’re out of searches and error otherwise. |
|
autocompleteHits |
number | 6 | Items in the dropdown. |
answers |
boolean | true |
Offer AI answers, when the app has them on. |
autoAnswer |
boolean | true |
Show the answer straight away for searches that look like questions. With false, visitors ask for it. |
primaryType |
string | The main record type, such as product. The results page then has a tab for it and one for everything else, and the dropdown lists them separately. |
|
filters |
object | Filters applied to every request. | |
ruleContexts |
string[] | Rule contexts sent with every search. | |
i18n |
object | Your own wording for any UI text. | |
theme |
object | CSS variables, such as { accent: "#0a7" }. |
|
renderHit |
function | (hit, ctx) => HTMLElement: your own markup for each result. |
|
userToken |
string | random | The visitor’s ID for analytics. See Events. |
consent |
boolean | true |
Whether the visitor agreed to analytics storage. See consent. |
reportErrors |
boolean | true |
Tell Shoof AI about errors the widget catches in your pages. See error reports. |
tokenCookie |
string | Also keep the visitor’s ID in this cookie, for searches your server makes for them. | |
view |
string | The ID of the item this page shows. Sends a view event once per session. | |
clicks |
object | Report clicks on results your page rendered itself: { queryId, items: [{ id, url, position }] }. |
Start it yourself
Section titled “Start it yourself”Without window.shoofConfig, nothing starts. Call Shoof.init() when you’re
ready, for example in a single-page app:
const shoof = Shoof.init({ appId: "my-store", apiKey: "shf_pub_…", answers: true });
shoof.autocomplete("#site-search");shoof.results(document.querySelector("#results"), { hitsPerPage: 24, searchBox: false });await shoof.related("#related", "sku-1-en", { limit: 4, title: "Similar items" });await shoof.trending("#trending", { type: "product", limit: 4 });Shoof.init() returns:
| Method | Does |
|---|---|
autocomplete(target) |
Enhances the input(s) matching a selector, or an element. |
results(target, options?) |
Mounts the results page. Options: searchBox, hitsPerPage, syncUrl (default true), query, filters, ruleContexts. |
related(target, id, options?) |
Mounts related items. Options: limit, title. |
trending(target, options?) |
Mounts trending items. Options: limit, title, type. |
trackConversion(objectId, value?, currency?, type?, orderId?) |
Reports a purchase (type conversion, the default) or an add to cart (add_to_cart). |
trackView(objectId) |
Reports a view of an item’s page, once per session. |
setConsent(granted) |
The visitor gave or withdrew consent. |
client |
The underlying ShoofClient. |
With window.shoofConfig, the started widget is window.shoof, and a
shoof:ready event fires on document with it as detail.
Track conversions
Section titled “Track conversions”Clicks on results are reported automatically. To learn which searches lead to sales, report add-to-carts and purchases too:
// On "Add to cart":shoof.trackConversion("sku-1-en", 140, "AED", "add_to_cart");
// On the order confirmation page, once per item:shoof.trackConversion("sku-1-en", 140, "AED", "conversion", "order-10521");The widget attributes each conversion to the search the visitor clicked the item from, if that was in the same browser session in the last 30 minutes. Passing the order ID groups the items of one order for bought together.
Theming
Section titled “Theming”The widget’s look comes from CSS variables. Set them with theme (without the
--shoof- prefix), or in your own stylesheet, on :root or .shoof-root.
Yours win over the defaults wherever your stylesheet loads:
:root { --shoof-accent: #0a7f5a; --shoof-radius: 4px; --shoof-font: "Inter", sans-serif;}| Variable | Default |
|---|---|
--shoof-accent |
#2f5bea |
--shoof-accent-fg |
#fff |
--shoof-bg |
#fff |
--shoof-fg |
#1b1d22 |
--shoof-muted |
#676d7c |
--shoof-border |
#e3e5ea |
--shoof-hover |
#f3f5f9 |
--shoof-mark |
#fff1a8 (highlighted matches) |
--shoof-radius |
10px |
--shoof-shadow |
the dropdown’s shadow |
--shoof-font |
inherit |
--shoof-chevron |
the arrow on selects, such as Sort (a url() image) |
Every element has a shoof- class (.shoof-ac-item, .shoof-price,
.shoof-thumb…) for finer changes.
Your own result markup
Section titled “Your own result markup”renderHit replaces the card for each result:
Shoof.init({ appId: "my-store", apiKey: "shf_pub_…", renderHit: (hit, ctx) => { const a = document.createElement("a"); a.href = hit.url; a.className = "my-card"; a.textContent = `${hit.title} – ${ctx.formatPrice(hit.price, hit.currency)}`; return a; },});Text and translations
Section titled “Text and translations”The widget ships in English and Arabic and picks the language from lang.
Override any text with i18n. {q} is the query and {n} a number:
Shoof.init({ appId: "my-store", apiKey: "shf_pub_…", i18n: { placeholder: "Search 2,000 products…", noResults: "Nothing matches “{q}”", related: "Customers also viewed", "facet.brand": "Maker", "type.post": "Recipe", },});Some of the keys: placeholder, askAi, aiAnswer, followUp, noResults,
noResultsHint, resultsCount, sortBy, relevance, priceAsc, priceDesc,
newest, filters, clearAll, inStock, outOfStock, related, trending,
viewAll, error, limit, limitHint. Facets are facet.<field>, record types type.<type> (singular) and
types.<type> (plural).
Consent
Section titled “Consent”The widget keeps an anonymous visitor ID in localStorage (shoof:user), so
analytics count each visitor once and ranking can learn from clicks. If your site
asks for consent first, start with consent: false: the ID then lasts only for
the browser session until the visitor agrees.
const shoof = Shoof.init({ appId: "my-store", apiKey: "shf_pub_…", consent: false });
// From your consent banner:onConsentChange((accepted) => shoof.setConsent(accepted));Error reports
Section titled “Error reports”When the widget catches an error of its own, such as your renderHit throwing or
a result it can’t draw, it still shows what it can and also tells Shoof AI, so
the error can be fixed. Without this, it would be visible only in a visitor’s
console.
A report has the error’s message and stack, the part of the widget that failed
and the widget’s version. The site it came from is the Origin the browser
sends. It never has the visitor’s ID, what they searched for or your page’s
address, and web addresses in the message lose everything after a ? or #.
Shoof AI writes each report to its log and stores nothing.
Failed requests are not reported, because the API already saw them, and neither are a visitor’s network failing or a request being cancelled. Each distinct error is sent once, and a page sends five at most.
To turn it off, set reportErrors: false.
Shoof.init({ appId: "my-store", apiKey: "shf_pub_…", reportErrors: false });What the widget shows
Section titled “What the widget shows”The widget reads your app’s public config and follows it: facets come from your app’s facet settings, and the AI answer button and related items are hidden when the app or its plan has them off. Change them in the dashboard, and the widget follows within a minute.