Skip to main content

Overview

The MoEngage React Native SDK fetches the recommended items for a recommendation you created on the MoEngage dashboard and returns them to your app as key-value data, handling user identity, networking, and caching internally.
Prerequisites
  • Recommendations must be enabled for your workspace. Contact your MoEngage account manager to enable it.
  • Integrate react-native-moengage 13.1.0 or above, which the Recommendations plugin requires. For more information, refer to Framework Dependency.
  • You have created a recommendation on the MoEngage dashboard and know its Recommendation ID. For more information, refer to Recommendations.
  • You have initialized the core MoEngage SDK in your application. For more information, refer to React Native SDK Initialization.

Install the Module

Add the MoEngage Recommendations plugin to your project:
Shell

iOS Installation

Install the iOS dependencies for the react-native-moengage-recommendations plugin with either CocoaPods or Swift Package Manager (SPM). For the supported packages and setup details, refer to iOS Installation.
  1. Navigate to the ios directory.
  2. Run the following command:
    Bash
Initialize the core MoEngage SDK before you call fetchRecommendations(). If the SDK is not initialized, the promise rejects with the SDK_STATE reason and the SDK makes no request. For more information, refer to React Native SDK Initialization.
Call fetchRecommendations() on a ReactMoEngageRecommendations instance to retrieve the items of a recommendation. Pass the Recommendation ID of the recommendation you want to fetch items from, as defined on the MoEngage dashboard. You can also pass an anchor item ID and the catalog fields you want returned for each item. The method returns a promise.
Each ReactMoEngageRecommendations instance is scoped to the workspace you pass to its constructor. If your application runs multiple MoEngage workspaces, create one instance per workspace.

Parameters

Handle the Result

The promise resolves with a RecommendedItems object. Its items property is a list of objects ranked best-first, where each object is one recommended item keyed by its catalog attribute names. An empty list is a successful result and means no recommended items are available for the user. The SDK passes the keys and value types through from your catalog unchanged, so keys are snake_case, such as product_id and image_link, and values differ between catalogs. Each value is typed unknown, so narrow its type before you use it:
To serve a default set of items when a recommendation returns nothing, configure Fallback Items on the recommendation. The SDK returns those items in the resolved result, in the same items list. Fallback Items apply to a successful fetch that has no recommended items. They do not apply when the fetch fails.

Handle Failures

The promise rejects with a RecommendationsFailure object. Use its failureReason property to identify the failure. Its message property carries details for logging only and can be empty. The SDK writes nothing to the cache on failure and leaves any cached items untouched. failureReason is a RecommendationsFailureReason, which combines the reasons specific to Recommendations with the reasons common to every MoEngage SDK request. A reason the SDK cannot classify maps to UNKNOWN_ERROR. The following reasons are specific to Recommendations: The following reasons are common to every MoEngage SDK request: The SDK does not retry a failed request. To try again, call fetchRecommendations() again. Because the SDK caches nothing on failure, the next call makes a fresh network request. Retry only for these reasons:
  • NETWORK_ERROR
  • RATE_LIMIT_EXCEEDED, after a delay
  • INTERNAL_SERVER_ERROR
  • SERVER_ERROR
  • UNKNOWN_ERROR

Caching

The SDK caches the items it fetches, so repeated calls for the same recommendation do not trigger a network request.
  • The SDK scopes each cache entry to the combination of Recommendation ID, item ID, and included fields. Changing any of the three fetches a fresh list.
  • Within the cache duration, fetchRecommendations() returns the cached items without a network call.
  • After the cache duration elapses, the next call fetches the items from the server and refreshes the entry.
  • The SDK caches an empty result in the same way, so an empty recommendation does not trigger a network call on every screen render.
  • The SDK clears the cache when the user logs out, when the user identity changes, and when the SDK is disabled.

Configure the Cache Duration

The SDK caches recommended items for 24 hours (86400 seconds). To cache items for longer, set the cache duration in the native initialization on each platform. The plugin does not expose this setting in JavaScript. Make the following changes in the native classes where you initialize the MoEngage framework.
The duration is in seconds. The minimum cache duration is 86400 seconds (24 hours). The SDK ignores a smaller value and uses 86400 seconds instead.

FAQs

No, the SDK does not download or cache media. Use a standard image loading library for the image URLs in the response.
The promise rejects with the SDK_STATE reason. Initialize the core SDK before you fetch recommendations. For more information, refer to React Native SDK Initialization.
No, the SDK serves the cached items until the cache duration elapses. Configure a duration that matches how frequently your catalog changes.