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 four files:index.htmlsrc/main.jssrc/carousel.jssrc/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
popularityattribute of the products. Name this indexe_commerce_transformed. - A replica of the
e_commerce_transformedindex, with a custom ranking on thepopularityattribute. Name this indexe_commerce_transformed_rating_desc. Use this index for the “best rated” carousel.
e_commerce_transformed index, located in the src/ folder.
In addition to the two indices used to store the product dataset, the dynamic solution uses a third Algolia index called e_commerce_transformed_config to define the content of the carousels and their order on the page. With this index, you manage your carousels directly in the dashboard without needing to change your frontend code. You can download the JSON of the e_commerce_transformed_config index, located in the src/ folder.
Configure each carousel
To dynamically generate the carousels, you can store each carousel’s configuration in thee_commerce_transformed_config index. For this demo, the index has five records, one for each carousel. Each record has attributes that define the title of the carousel, the index in indexName, and any query parameters in configure pertinent to the carousel.
The carousel section walks you through four typical carousels. They all rely on the records in the e_commerce_transformed_config index. Here’s an example record:
JSON
order as a custom ranking attribute. Doing so orders the shelves on the frontend. You learn about the other attributes as you go through the tutorial.
Adapt the HTML
In yourindex.html file, add the following div as a placeholder for your dynamically generated carousels.
HTML
Render the carousels
This section explains how to render the carousels dynamically, by creating a newindex widget for each carousel.
- Use
src/main.jsto render the carousel widgets. - Use
src/carousel.jsto define the carousel widget.
Define how to display the carousels
Define the display logic for every carousel insrc/carousel.js. This file creates the widget that you append to the carousel-container defined in index.html.
Create a renderCarousel function that targets the containers and appends them with the hits you want to display:
- Target the carousel container.
- Create an
<h2>header that displays the title of the carousel. - 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 movie you want to display.
JavaScript
Render the carousel widgets
The following function returns an array of all carousel widgets.
JavaScript
- Carry out a search on the index associated with the carousel, using any search parameters defined in the widget.
- Create the widget with the results obtained.
- Append the
carouselwidget to thecarouselContainer.
JavaScript
e_commerce_transformed_config to create the different carousels.
Creating the carousels
This tutorial offers five examples of carousels. Some apply different sorting strategies. Others require Personalization or Rules. You need to define each of these carousels as records in thee_commerce_transformed_config index. To do so, you can add the JSON snippets in the following sections to that index. You don’t have to do any further frontend development.
Most popular products
This carousel displays the most popular products. The carousel targets the index that sorts results by popularity (e_commerce_transformed). The code limits the number of results (8) and display the carousel in the correct container (#carousel-most-popular). You also need to provide the indexID property, because other carousels target the same index.
Place the following record in the e_commerce_transformed_config index.
JSON
Best rated products
This carousel displays best rated products. The JSON is the same as the carousel that displays the most popular products. The difference is the targeted index: the replica callede_commerce_transformed_rating_desc. This replica ranks its records differently:
- It uses
ratingsas a sort-by attribute. - It uses
ratingsNumberas custom ranking attribute.
e_commerce_transformed_config index.
JSON
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.
Place the following record in the e_commerce_transformed_config index.
JSON
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.
Place the following record in the e_commerce_transformed_config index.
JSON