Create a Data Collection Action
In some cases when you trigger an action, you might not want to alter your site's content while still retaining the customer data involved with the action. In these cases, you can perform a Data Collect action.
You can set up a handler for a data collection action by using two methods. The addEvent 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 customer data.
While this data does not affect your site's content, you can use it for other cases elsewhere in code.
addEvent
This method adds a local event to the SDK's internal stack.
public void addEvent(String context, Object event)The parameters are as follows:
- context is name of the event.
- event is the event data.
You can use this method multiple times to add all the necessary events for an experience that you might want to trigger. The example code uses multiple method calls to fulfill the experience requirements.
Code Example
personalization.addEvent(EventTypes.ContextIpAddress, ipAddress);
personalization.addEvent(EventTypes.ContextPageView, pageView);
personalization.addEvent(EventTypes.ContextScreenSize, screenSize);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:
- actionType 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.
Use DataCollection as the action type for this method. includeReports must be set to true for this method to return data.
Code Example
String responseData = personalization.getActionsData(new String[]{"monetate:action:DataCollection"},true);Full Code Example
// 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("homepage");
pageView.setUrl("https://www.monetate.com");
// addEvent
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.getActions(EventTypes.ContextIpAddress, address, new String[]
{
"monetate:action:DataCollection"
}, true);
// Once you receive the responseData from getActionsData, use the Handler and 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();Data Collection action using ContextLayer
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, DataCollection 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 Example
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
//setting up context data
const context = new Context();
context.addIpAddress("1.0.0.0");
context
.addScreenSizeData()
.width(211)
.height(400);
context
.addPageDetails()
.url(window.location.href)
.path(window.location.pathname)
.pageType("home")
.breadcrumbs(["Home"])
.categories(["Homepage"]);
Personalization.getActions(
["monetate:action:DataCollection"],
monetateContext)
.then((res: any) => console.log("Action === 1 res", res));