Using rules to customize search results by device
Learn how to customize your search results based on whether your user is on a mobile or desktop device.
Search needs to be flexible. Depending on the user’s context, results can vary. For example, because some products are more likely to be bought or viewed from phones than desktops, you may want to promote items based on the user’s device.
To do this:
- Identify your user’s device.
- Assign a
mobile
context when users search from mobile devices. A context is a string passed as a search parameter to InstantSearch or an Algolia API client. - Create a contextual rule that acts on the
mobile
context.
To help understand user behavior across devices, pair your device-specific experience with analytics. You can even use the same context string to tag search queries for analytics. These analyticsTags
help you group search analytics into different segments.
Example scenario
Suppose you work for a movie database company. Research shows that mobile users, unlike desktop users, tend to see a film in theaters after it has appeared in their search results. To capitalize on this, you partner with a movie theater chain and decide to promote newly released movies to your mobile users.
The records in your Algolia index include a release_date
attribute, in Unix timestamp format, this is useful for identifying newly released movies.
By creating a mobile context and contextual rules, you can promote recent movies to mobile users.
Identify your user’s device
Before assigning a mobile context to searches, write a function that detects what device the app is running on, and return “desktop” or “mobile” depending on what it finds:
Assign context
You can assign context with InstantSearch or an Algolia API client.
Assign context with InstantSearch
Set the user’s device context at the beginning of their search session by adding it to the searchParameters
setting of your InstantSearch instance during initialization.
Assign context with an API client
Pass the user’s device context as a search parameter whenever you search
your index.
Create a contextual rule
You can create a mobile contextual rule with an API client or from the Algolia dashboard.
Create a contextual rule with an API client
Use the save-rule
method to create a context-dependent rule.
For example, to promote movies released after January 1, 2020, for mobile users:
Create a contextual rule with the dashboard
-
Select the Search product icon on your dashboard.
-
Select the Rules section from the left sidebar menu in the Algolia dashboard.
-
Under the heading Rules, select the index to which you’re adding a rule.
-
Select Create your first rule or New rule. In the drop-down menu, click the Manual Editor option.
-
In the Condition(s) section, toggle Context on and toggle Query off.
-
Enter “mobile” in the context input field.
-
In the Consequence(s) sections, click Add Consequence and select the Add Query Parameter consequence.
-
In the editor, add a filter for the
release_date
attribute. For example, to show all movies released since 2020 (1577836800
is the Unix timestamp for January 1, 2020), enter:{ "filters": "release_date >= 1577836800" }
-
If you wish, you can add other consequences by clicking Add consequence. For example, there may be movies that you want to pin to a specific location or rank at the top,
-
Save your changes.
Was this page helpful?