
Before you begin
This guide requires the Algolia Personalization feature and InstantSearch.js.This feature isn’t available on every plan.
Refer to your pricing plan to see if it’s included.
Implementation guide
This guide shows you how to display products in five different carousels: “Popular,” “Best rated,” “On sale,” “These might interest you,” and “Gifts for Black Friday.” You need these three files:index.html
src/main.js
src/style.css
Indices to display the carousels
You need two Algolia indices containing the product dataset:- A primary index, with a custom ranking on the
popularity
attribute of the products. Name this indexe_commerce_transformed
. - A replica of the
e_commerce_transformed
index, with a custom ranking on thepopularity
attribute. Name this indexe_commerce_transformed_rating_desc
. Use this index for the “best rated” carousel.
e_commerce_transformed
index, located in the src/
folder.
Adapt the HTML
In yourindex.html
file, add a container for each carousel.
HTML
Create a render function
In thesrc/main.js
file, create a renderCarousel
function that targets the containers, and replaces them with the hits you want to display:
- Target the carousel container.
- Add a
<ul>
tag in the container when you render the carousel for the first time. - Inside the
<ul>
element, add<li>
tags for each product you want to display.
JavaScript
connectHits
function to tell InstantSearch to use the renderCarousel
function to render hits.
JavaScript
index
widget for each of the five carousels and add them to the page with the addWidgets
method.
Here is the framework, to be filled in by the code in the following sections.
JavaScript
Create the carousels
This tutorial offers five examples of carousels. Some apply different sorting strategies. Others require Personalization or Rules. They each contain the code that you place in the framework described in the previous section.Most popular products
This carousel displays the most popular products. The code targets thee_commerce_transformed
index that sorts results by popularity. The code limits the number of results to 8
, and displays the carousel in the correct container: #carousel-most-popular
. You also need to provide the indexID
property, because other carousels target the same index.
JavaScript
Best rated products
This carousel displays best rated products. The code is similar to the carousel that displays the most popular products. The main difference is the index you target. You have to target the replica index callede_commerce_transformed_rating_desc
, which ranks products on the popularity
attribute.
JavaScript
On sale items
This carousel displays a list of products that are on sale. To do this, it uses theonSale
attribute.
Like the first two carousels, it sets up the query.
The unique part here is that the rendering code in the creating a render function section conditions the display with onSale=true
.
JavaScript
These might interest you
This carousel displays a list of products personalized to each user’s taste. To do this, it leverages Algolia’s Personalization feature. Please make sure your plan has access to Personalization before trying to implement this. For Personalization to work, you need to configure two extra search parameters for the carousel:enablePersonalization
to enable Personalization for this search.userToken
to specify the user you personalize for.
JavaScript
Gifts for Black Friday
This carousel displays products that users can offer as gifts on Black Friday. To accomplish this, you need to create a Rule, either in the dashboard or with the API.Using the dashboard
To create this Rule in the dashboard, follow these steps:- Select the Rules section from the left sidebar menu in the Algolia dashboard.
- Under the heading Rules, select the index you are adding a Rule to.
- Select Create your first rule or New rule. In the dropdown, click on the Manual Editor option.
- In the Condition(s) section, click on Context, enter the text “carousel_black_friday”.
-
In the Consequence(s) section:
- Click the Add consequence button and select Add Query Parameter.
-
In the input field that appears, enter the following JSON search parameter.
JSON
- Click the Add consequence button and select Pin an item.
-
Find the product ‘1319385’, click ‘2’, and press
Enter
. -
Find the product ‘5197004’, click ‘3’, and press
Enter
. -
Find the product ‘5327500’, click ‘4’, and press
Enter
. -
Find the product ‘4826902’, click ‘7’, and press
Enter
. -
Find the product ‘8945804’, click ‘8’, and press
Enter
. -
Find the product ‘4374300’, click ‘12’, and press
Enter
.
- Don’t forget to save your changes.
Using an API client
If you use the API to create your Rule, use thesaveRule
method with the following Rule structure:
JSON
carousel_black_friday
Rule with the correct context. Do this using the ruleContexts
parameter.
JavaScript
Next steps
- Add more carousels. Now that you know how to set up content carousels, you can create a variety of carousels for your project. This helps improve user engagement with your content.
- Add dynamic carousels. The current solution defined the list of carousels directly in the code, which means that, if you want to change the list, you need to change the code. The dynamic carousels solution lets you change the list of carousels from the dashboard without needing to change the code.