- Identify your user’s device.
- Assign a
mobilecontext 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. - Create a contextual rule that acts on the
mobilecontext.
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 arelease_date attribute,
in Unix timestamp format, this is useful for identifying newly released movies.
JSON
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 thesearchParameters 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 thesaveRule 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.- 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).
Turn Query off, turn Context on, enter
mobile, and click Apply. - In Consequences, select Filter results.
Select
release_date, set the operator to>=, enter1577836800, and click Apply. - Click Review and Publish, review the rule, and click Publish.