Skip to main content

Overview

The MoEngage Web SDK fetches the recommended items for a recommendation you created on the MoEngage dashboard and returns them to your website 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.
  • You have created a recommendation on the MoEngage dashboard and know its Recommendation ID. For more information, refer to Recommendations.
  • You have integrated and initialized the Web SDK on your website. For more information, refer to Web SDK Integration.
  • Your website uses Web SDK version 2.82.00 or later.

Enable the Module

Recommendations is a separate module that the Web SDK loads at runtime, so you do not add a script or package to your website. To control when the module loads, pass the recommendations object in the SDK initialization:
The enable key is optional and takes the following values:
In a script integration, fetchRecommendations() is available on window.Moengage.recommendations only after the Web SDK bundle has downloaded. If you call it before the bundle loads, the call throws. Call it after the SDK has loaded on the page. In an NPM integration, the call does not throw. The promise rejects with sdkState instead.
Call Moengage.recommendations.fetchRecommendations() 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.

Parameters

Handle the Result

The promise resolves with an object that has an items property. items is an array of objects, where each object is one recommended item keyed by its catalog attribute names. An empty array 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 values differ between catalogs. Check the type of each value 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 array. 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 an object that has a failureReason property and a message property. Use failureReason to identify the failure. The SDK writes nothing to the cache on failure and leaves any cached items untouched. If the SDK cannot download the Recommendations module, the promise rejects instead of remaining pending. 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:
  • networkError
  • rateLimitExceeded, after a delay
  • internalServerError

Caching

The SDK caches the items it fetches in the browser, so repeated calls for the same recommendation do not trigger a network request. The cache persists across page reloads.
  • 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. The order of included fields does not matter, so ["size", "color"] and ["color", "size"] share one cache entry.
  • 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 render.
  • The SDK removes expired entries when it initializes and on every route change. The cleanup on route change runs only when SPA support is turned on with enableSPA: true in the SDK initialization.
  • The SDK clears the cache when the user logs out, when the user identity changes, and when the SDK is disabled.
  • The SDK deletes the cache when Recommendations is disabled for your workspace, or when enable is set to false in the SDK initialization.

Configure the Cache Duration

By default, the SDK caches recommended items for 24 hours (86400 seconds). MoEngage can set a longer duration for your workspace. To cache items for longer, pass itemsCacheDuration in the recommendations object of the SDK initialization:
The duration is in seconds. The SDK uses the greater of your itemsCacheDuration value and the duration set for your workspace, and never less than 86400 seconds (24 hours). A value lower than either has no effect.

FAQs

No, the Web SDK loads the module at runtime. To control when it loads, pass the recommendations object in the SDK initialization. For more information, refer to Enable the Module.
In a script integration, the method does not exist on window.Moengage.recommendations until the Web SDK bundle has downloaded, so the call throws. Call fetchRecommendations() after the SDK has loaded on the page. In an NPM integration, the promise rejects with sdkState.
No, the SDK serves the cached items until the cache duration elapses. Configure a duration that matches how frequently your catalog changes.