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.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 therecommendations object in the SDK initialization:
enable key is optional and takes the following values:
Fetch Recommended Items
CallMoengage.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 anitems 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:
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 afailureReason 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:
networkErrorrateLimitExceeded, after a delayinternalServerError
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: truein 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
enableis set tofalsein 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, passitemsCacheDuration 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
Does the SDK render the recommended items?
Does the SDK render the recommended items?
No, the SDK returns the item data only. Your website parses the response and builds the user interface.
Do I need to add the Recommendations module to my website?
Do I need to add the Recommendations module to my website?
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.What happens if I call fetchRecommendations() before the SDK has loaded?
What happens if I call fetchRecommendations() before the SDK has loaded?
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.Can I force a fresh fetch before the cache expires?
Can I force a fresh fetch before the cache expires?
No, the SDK serves the cached items until the cache duration elapses. Configure a duration that matches how frequently your catalog changes.