Create a Recommendations Action
Omnichannel recommendations is an integrated product recommendations experience that is consistent across all platforms that you connect to Monetate. Whether a customer is viewing your storefront through a mobile app or your site, Omnichannel recommendations ensure a consistent experience.
You can set up a handler for an Omnichannel recommendations action by using two methods. The addEvents method defines the events that can trigger the action. The getActionsData method is then used as the trigger and requests the decision based on the defined events. getActionsData then returns a JSON object containing recommendations data that you can then handle in code.
Prerequisites
You must first create an Omnichannel experience in the Monetate platform for the methods to reference. Refer to Configure an Omnichannel Recommendations Action in the Monetate Knowledge Base for instructions.
Make note of the WHO settings because they correspond to events your code listens for. The example experience in this article uses the following WHO settings:
- IP address is 1.0.0.2
- Screen height is at least 500 pixels and screen width is at least 300 pixels
The example code in this article fulfills these conditions and will trigger the Omnichannel experience.
addEvent.
This method adds event data to the SDK's internal stack.
public void addEvent(String context, Object event)The parameters are as follows:
- context is name of the event. (Required)
- event is the event data. (Required)
You can use this method multiple times to add all the necessary events for an experience you might want to trigger. The example code uses multiple method calls to fulfill the experience requirements.
Code Example
personalization.addEvent(EventTypes.ContextIpAddress, new IpAddress("1.0.0.0"));
personalization.addEvent(EventTypes.ContextPageView, new PageView("home", "test.com",new String[] {"categoies"} ,new String[] {"breadcrums"}));
personalization.addEvent(EventTypes.ContextScreenSize, new ScreenSize(1011, 2011));getActionsData
This method sends the defined events to Monetate to trigger an experience. If the events fulfill the WHO settings of an experience, then that experience is triggered. A JSON object containing the experience response is then returned.
public String getActionsData(String[] actionTypes, boolean includeReports)The parameters are as follows:
- actionTypes is the type of action you want to request. You can specify one action or multiple actions in an array to handle.
- includeReports indicates whether the response will have impression reporting data.
Code Example
String responseData = personalization.getActionsData(new String[]{"monetate:action:OmnichannelRecommendation"},true);Full Code Example
Personalization personalization = new personalization(user, account);
// Events and context data
IpAddress ipAddress = new IpAddress();
ipAddress.setIpAddress("1.0.0.0");
ScreenSize screenSize = new ScreenSize();
screenSize.setHeight(1011);
screenSize.setWidth(2011);
PageView pageView = new PageView();
pageView.setPageType("Cartpage");
pageView.setUrl("https://www.monetate.com");
// addEvent
personalization.addEvent(EventTypes.ContextScreenSize, screenSize);
personalization.addEvent(EventTypes.ContextIpAddress, ipAddress);
personalization.addEvent(EventTypes.ContextPageView, pageView);
// getActionsData
String responseData;
new Thread(new Runnable()
{
@Override
public void run()
{
// Gets the responseData using getActionsData
responseData = personalization.getActionsData(new String[]
{
"monetate:action:OmnichannelRecommendation"
}, true);
// Once you receive the responseData from getActionsData, use the Handlerand parse the required data from it
new Handler(Looper.getMainLooper()).post(new Runnable()
{
@Override
public void run()
{
// Parse the received responseData, get the requiredData from it and use it accordingly
try
{
// Example: textView.setText(requiredData);
}
catch (Exception ex)
{
// Catch any exception that occurs while parsing
ex.printStackTrace();
}
}
});
}
}).start();You must handle the getActions method in a Thread or AsyncTask. To update the main thread, you can use either Handler or runOnUiThread. This full code example uses Thread and Handler to update the main thread and change the UI.
Recommendations using Context Layer
Supported SDK Versions
- Android: 1.0.6-A or later
- React-Native: 1.0.6-RN or later
- Web: 1.0.6-W or later
- iOS: 2026.01.06 or later
Using the new Context Layer–based approach, Recommendation Actions can be implemented using a single enhanced getActions() method. Below is the sample code snippet demonstrating this usage.
For detailed information on the enhanced getActions() flow, please refer to the Context and getActions sections.
Code Exmaple
async getActions(
actionType: string | string[],
context?: any,
includeReporting: boolean = true,
errorCallback?: (result: { status: "success" | "failed"; error?: unknown }) => void
): Promise<{ data: unknown; errors?: unknown }> {}
Full code Exmaple
const context = new Context();
context
.addPageDetails()
.url("www.monetate.com")
.path(monetate.com)
.pageType("home")
.breadcrumbs(["Home"])
.categories(["Homepage"]);
Personalization.getActions(
["monetate:action:OmnichannelRecommendation"],
context)
.then((res: any) => console.log("Action [res]", res));