Skip to main content
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:
  1. Identify your user’s device.
  2. Assign a mobile context when users search from mobile devices. A context is a string passed as a search parameter to InstantSearch or one of the Algolia API clients.
  3. 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 in your Algolia include a release_date attribute, in Unix timestamp format, this is useful for identifying newly released movies.
JSON
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:
JavaScript

Assign context

You can assign context with InstantSearch or one of the Algolia API clients.

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

To run the code examples on this page, install the latest 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.

Using the API

Use the saveRule method to create a context-dependent rule. For example, to promote movies released after January 1, 2020, for mobile users:

Using the dashboard

You can create this rule from the Algolia dashboard using either the Visual Editor or the Manual Editor.
  1. In the Algolia dashboard, select your application and index, open Rules, and select New rule > Visual Editor.
  2. Click Set query condition(s). Turn Query off, turn Context on, enter mobile, and click Apply.
  3. In Consequences, select Filter results. Select release_date, set the operator to >=, enter 1577836800, and click Apply.
  4. Click Review and Publish, review the rule, and click Publish.
Last modified on September 28, 2026