Skip to main content
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.
facetName
string
The facet attribute to get recommendations for (optional, for facet-specific trending).
facetValue
string
The facet value to get recommendations for (required if facetName is provided).
limit
number
Number of recommendations to retrieve.
threshold
number
Threshold for the recommendations confidence score (between 0 and 100).
queryParameters
object
Additional search parameters to send to the Algolia API.
fallbackParameters
object
Search parameters to use when not enough recommendations are available.
escapeHTML
boolean
default:"true"
Whether to escape HTML tags from items string values.
transformItems
function
Function to transform the items before rendering.
(items: Hit[], { results: RecommendResponse }) => Hit[]
templates
object
Templates to use for the widget.
cssClasses
object
CSS classes to add to the widget elements.

Examples

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>
      `;
    }
  }
});
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>';
    }
  }
});

Transform items to add badges

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>
      `;
    }
  }
});
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.