Displays trending items recommendations using Algolia’s Recommend API.
Usage
import instantsearch from 'instantsearch.js';
import { trendingItems } from 'instantsearch.js/es/widgets';
const search = instantsearch({
indexName: 'instant_search',
searchClient
});
search.addWidgets([
trendingItems({
container: '#trending-items'
})
]);
search.start();
Parameters
container
string | HTMLElement
required
CSS selector or HTMLElement to insert the widget.
The facet attribute to get recommendations for (optional, for facet-specific trending).
The facet value to get recommendations for (required if facetName is provided).
Number of recommendations to retrieve.
Threshold for the recommendations confidence score (between 0 and 100).
Additional search parameters to send to the Algolia API.
Search parameters to use when not enough recommendations are available.
Whether to escape HTML tags from items string values.
Function to transform the items before rendering.(items: Hit[], { results: RecommendResponse }) => Hit[]
Templates to use for the widget.
Template for each trending item. Receives the hit data.
Template to display when there are no results.
Template for the widget header. Receives { items, cssClasses }.
Template to wrap all items. Receives { items, templates, cssClasses }.
CSS classes to add to the widget elements.
CSS class for the root element.
CSS class for the root when there are no results.
CSS class for the list element.
Examples
Global trending items
trendingItems({
container: '#trending-items',
limit: 6,
templates: {
item(hit, { html }) {
return html`
<div>
<img src="${hit.image}" alt="${hit.name}" />
<h3>${hit.name}</h3>
<p>$${hit.price}</p>
</div>
`;
}
}
});
Trending in a category
trendingItems({
container: '#trending-items',
facetName: 'category',
facetValue: 'Electronics',
limit: 4,
templates: {
header: 'Trending in Electronics',
item(hit) {
return `
<article>
<img src="${hit.image}" />
<h4>${hit.name}</h4>
<span class="price">$${hit.price}</span>
</article>
`;
}
}
});
With header and count
trendingItems({
container: '#trending-items',
templates: {
header({ items }) {
return `<h2>Trending Now (${items.length})</h2>`;
},
item(hit) {
return `
<div class="trending-product">
<img src="${hit.image}" />
<div>${hit.name}</div>
<div class="price">$${hit.price}</div>
</div>
`;
}
}
});
With empty state
trendingItems({
container: '#trending-items',
templates: {
item(hit) {
return `<div class="product">${hit.name}</div>`;
},
empty() {
return '<p>No trending items available right now.</p>';
}
}
});
trendingItems({
container: '#trending-items',
transformItems(items) {
return items.map((item, index) => ({
...item,
trendingRank: index + 1,
badge: index < 3 ? 'Hot' : 'Trending'
}));
},
templates: {
item(hit) {
return `
<div>
<span class="badge">${hit.badge} #${hit.trendingRank}</span>
<h4>${hit.name}</h4>
</div>
`;
}
}
});
Specific brand trending
trendingItems({
container: '#trending-items',
facetName: 'brand',
facetValue: 'Apple',
limit: 5,
templates: {
header: 'Trending Apple Products',
item(hit) {
return `<div>${hit.name} - $${hit.price}</div>`;
}
}
});
With threshold
trendingItems({
container: '#trending-items',
threshold: 80,
limit: 8,
templates: {
item(hit) {
return `<div class="item">${hit.name}</div>`;
}
}
});
HTML output
<div class="ais-TrendingItems">
<ol class="ais-TrendingItems-list">
<li class="ais-TrendingItems-item">
<!-- Item template content -->
</li>
<!-- More items -->
</ol>
</div>
Notes
- Requires Algolia Recommend to be enabled on your account.
- The widget uses the Trending Items model.
- Can show global trends or facet-specific trends.
- Both
facetName and facetValue must be provided together.
- Results are based on user behavior and item popularity.