- InstantSearch banners: use a rule to add banner data to the
renderingContent.widgets.bannersfield, so that supported InstantSearch widgets render the banner for you. - Custom banners: use a rule to add banner data to the
userDatafield. Your own UI code reads this data and renders the banner.
InstantSearch banners
If your site already uses an InstantSearchHits or InfiniteHits
widget, you can display banners automatically based on a Visual Editor rule which adds banner data to the search response.
This requires InstantSearch.js version 4.73.0 or later, React InstantSearch version 7.12.0 or later, or Vue InstantSearch version 4.19.0 or later.
Create a Visual Editor rule with a banner consequence
- Click Create your first Rule or New rule and select Visual Editor.
- In Conditions, click Set query condition(s).
In Your search, enter
harry potterand click Apply. - In Consequences, expand More consequences and click Display banner. Enter the URL for your banner image and click Apply.
- Review the banner preview above the results, click Review and Publish, review the rule, and click Publish.
Make sure your search UI uses a supported InstantSearch widget (
Hits or InfiniteHits) and
is deployed on your website so it can display banners from the renderingContent.widgets.banners property in the API response.Create a rule with a banner consequence using the API
To run the code examples on this page, install the latest API client. To add a rule with a banner consequence using the API, use thesaveRule method.
When adding a rule, you need to define a condition and a consequence.
In the consequence, add search parameters and configure renderingContent using the following template:
JSON
Render banner with InstantSearch
After adding the rule, you can display the banner in your UI with the InstantSearchHits or InfiniteHits widgets.
The banner renders automatically unless you configure the widget to hide it.
For example:
Custom banners
If you don’t use InstantSearch, or you need a fully custom banner, use this approach.Create a rule for returning custom data
If you want to motivate users to buy Harry Potter books, you can display a special promotional banner at the top of search results whenever their search query containsharry potter.
To display the banner:
- Create a rule that returns the banner data in the
userDatafield. - Update your search UI to read and render this
userData.
Create a rule with the API
To add a rule, you need to use thesaveRule method.
When setting a rule, you need to define a condition and a consequence.
For the preceding example, you want to show the banner whenever the query contains harry potter.
The match doesn’t need to be exact.
If the query is harry potter azkaban or books harry potter, you still want to display the promotion.
Create a rule in the dashboard
You can add this rule from the Algolia dashboard using either the Visual Editor or the Manual Editor.- Visual Editor
- Manual Editor
- In the Algolia dashboard, select your application and index, open Rules, and select New rule > Visual Editor.
- Click Set query condition(s), select contains, enter
harry potter, and click Apply. - In Consequences, expand More consequences and select Return custom data.
In Value, enter
{ "promo_content": "20% OFF on all Harry Potter books!" }and click Apply. - Click Review and Publish, review the rule, and click Publish.
Duplicate rules to adapt to user search patterns
To show the Harry Potter promotion for searches forpotter as well as harry potter:
- Duplicate the existing Harry Potter rule.
- Edit the rule to change the query search phrase to
potter.
potter.
For the query harry potter, both rules match, but the original rule has higher precedence because its match starts earlier (the Position criterion).
Retrieve the banner data in your UI
The search response includes your banner data in theuserData property.
This data isn’t displayed automatically:
your search UI must read userData and render the banner when it’s present.
For example, with InstantSearch you can use the queryRuleCustomData widget:
- InstantSearch.js:
queryRuleCustomData - React InstantSearch:
QueryRulesCustomData - Vue InstantSearch:
ais-query-rule-custom-data
Apply rules with context conditions
If you want to apply rules based on context, see:- InstantSearch.js:
queryRuleContext - React InstantSearch:
QueryRuleContext - Vue InstantSearch:
ais-query-rule-context
Determine priority when you have multiple banners
You may have several semi-permanent promotional banners for specific product categories, as well as some time-limited, temporary banners for certain products. For instance, an online IT hardware store has banners for the following categories:- Phones
- Tablet computers
- Laptop computers
- Desktop computers
- Infinix Note 40 Pro
- Xiaomi Redmi Note 14 Pro+
- Samsung Galaxy S25 Ultra
- Samsung Galaxy Tab S10 Ultra
- Samsung Galaxy Book3 360
- User starts on the Phones category page and sees the Phones banner.
- They search for
note. - At this point, three rules can match: the Phones category banner and the two product banners for Infinix Note 40 Pro and Xiaomi Redmi Note 14 Pro+.
- The two product banners are more specific than the category banner, so they take precedence over it.
- When multiple rules match, Algolia compares their
objectIDvalues and shows the banner whoseobjectIDcomes first alphabetically.
In this context,
objectID refers to the rule’s ID, not to any product or record objectID in your index.
Rule objectIDs are unique identifiers for rules.
When using the API, you can set them manually.
In the dashboard, they’re generated automatically.To control this tie-breaker, change the rule’s objectID with the saveRule method.