---
title: Create a Data Collection Action
slug: sdks/data-collection
description: How to deploy a Data Collection action using the React Native, iOS, and Android versions of the Monetate SDK
image:  https://archbee-image-uploads.s3.amazonaws.com/ceIfEvQHzQk1IVxoRWUh7/K9eTL4jKm7UQINb6zNziJ-20250912-181225.png
docTags: 
createdAt: 2024-07-19T18:00:00.354Z
---

:::BlockQuote
**Note :**&#x54;his legacy flow remains supported for backward compatibility.New integrations are recommended to use the Context Layer–based getActions() approach.[ check below](https://developer.monetate.com/sdks/data-collection#fmkdD)
:::

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](https://docs.monetate.com/docs/available-omnichannel-action-types#omni-data-collect) action.

You can set up a handler for a data collection action by using two methods. The [addEvent](docId\:F5SIeJXaS3hi38DaXdVPy) method defines the events that can trigger the action. The [getActionsData](docId\:F5SIeJXaS3hi38DaXdVPy) 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.

:::::Tabs
::::Tab{title="React Native"}
:::CodeblockTabs
addEvent Method

```javascript
addEvent(context: EventTypes, events: ContextData): void
```
:::
::::

::::Tab{title="React Web"}
:::CodeblockTabs
addEvent Method

```javascript
addEvent(context: EventTypes, events: ContextData): void
```
:::
::::

::::Tab{title="iOS"}
:::CodeblockTabs
addEvent Method

```swift
addEvent(context: <ContextEnum>, event: <MEvent?>)
```
:::
::::

::::Tab{title="Android"}
:::CodeblockTabs
addEvent Method

```java
public void addEvent(String context, Object event)
```
:::
::::
:::::

The parameters are as follows:

- `context` is name of the event.
- `event` is the event data.&#x20;

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

:::::Tabs
::::Tab{title="React Native"}
:::CodeblockTabs
addEvent Example

```javascript
personalizationInstance.addEvent(EventTypes.ContextScreenSize,
{
  width: 23,
  height: 34
});
personalizationInstance.addEvent(EventTypes.ContextIpAddress,
{
  ipAddress: "10.0.0.2",
});
personalizationInstance.addEvent(EventTypes.ContextPageView,
{
  url: "http://www.monetate.com/index.html",
  pageType: "Home"
});
```
:::
::::

::::Tab{title="React Web"}
:::CodeblockTabs
addEvent Example

```javascript
personalizationInstance.addEvent(EventTypes.ContextScreenSize,
{
  width: 23,
  height: 34
});
personalizationInstance.addEvent(EventTypes.ContextIpAddress,
{
  ipAddress: "10.0.0.2",
});
personalizationInstance.addEvent(EventTypes.ContextPageView,
{
  url: "http://www.monetate.com/index.html",
  pageType: "Home"
});
```
:::
::::

::::Tab{title="iOS"}
:::CodeblockTabs
addEvent Example

```swift
objPersonalization.addEvent(context: .PageView, event: PageView(pageType: "Homepage", path: "n/a", url: "n/a", categories: [], breadcrumbs: []))
```
:::
::::

::::Tab{title="Android"}
:::CodeblockTabs
addEvent Example

```java
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.

:::::Tabs
::::Tab{title="React Native"}
:::CodeblockTabs
getActionsData Method

```javascript
getActionsData(actionType: Array<string>, includeReporting: boolean) : Promise<any>
```
:::

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.&#x20;

Use `DataCollection` as the action type for this method. `includeReports` must be set to `true` for this method to return data.
::::

::::Tab{title="React Web"}
:::CodeblockTabs
getActionsData Method

```javascript
getActionsData(actionType: Array<string>, includeReporting: boolean) : Promise<any>
```
:::

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.&#x20;

Use `DataCollection` as the action type for this method. `includeReports` must be set to `true` for this method to return data.
::::

::::Tab{title="iOS"}
:::CodeblockTabs
getActionsData Method

```swift
getActionsData(requestId: <String>, includeReporting: <Bool>, arrActionTypes: <[String]>) 
```
:::

The parameters in the iOS version are as follows:

- `requestID` is the request ID for the API.
- `includeReporting` indicates whether the response will have impression reporting data.
- `arrActionTypes` is the types of action you want to request. You can specify multiple actions in an array to handle.

Use `DataCollection` as the action type for this method. `includeReporting` must be set to `true` for this method to return data.
::::

::::Tab{title="Android"}
:::CodeblockTabs
getActionsData Method

```java
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.&#x20;

Use `DataCollection` as the action type for this method. `includeReports` must be set to `true` for this method to return data.
::::
:::::

### Code Example

:::::Tabs
::::Tab{title="React Native"}
:::CodeblockTabs
&#x20;getActionsData Example

```javascript
responseData = personalizationInstance.getActionsData("monetate:action:DataCollection", true);
```
:::
::::

::::Tab{title="React Web"}
:::CodeblockTabs
getActionsData Example

```javascript
responseData = personalizationInstance.getActionsData("monetate:action:DataCollection", true);
```
:::
::::

::::Tab{title="iOS"}
:::CodeblockTabs
&#x20;getActionsData Example

```swift
objPersonalization.getActionsData(requestId: "123456", includeReporting: true, arrActionTypes:["monetate:action:DataCollection"]).on{res in 
  if res.status==200 {
    self.handleRecommendations(res: res)
  } else {
  }
 }
}
```
:::
::::

::::Tab{title="Android"}
:::CodeblockTabs
&#x20;getActionsData Example

```java
String responseData = personalization.getActionsData(new String[]{"monetate:action:DataCollection"},true);
```
:::
::::
:::::

## Full Code Example

:::::Tabs
::::Tab{title="React Native"}
:::CodeblockTabs
Data Collection Action Full Example

```javascript
// Add Context / Events
// ----------------------------------------------------
personalizationInstance.addEvent(EventTypes.ContextScreenSize,
{
  width: 23,
  height: 34
});
personalizationInstance.addEvent(EventTypes.ContextIpAddress,
{
  ipAddress: "10.0.0.2",
});
personalizationInstance.addEvent(EventTypes.ContextPageView,
{
  url: "http://www.monetate.com/index.html",
  pageType: "Home"
});
// ----------------------------------------------------
// Get Actions
let recData;
personalizationInstance
  .getActionsData("monetate:action:DataCollection", true)
  .then(res =>
  {
    recsData = res[0].actions;
  })
  .catch(error =>
  {
    console.warn('Error!', error);
  });
```
:::
::::

::::Tab{title="React Web"}
:::CodeblockTabs
Data Collection Action Full Example

```javascript
// Add Context / Events
// ----------------------------------------------------
personalizationInstance.addEvent(EventTypes.ContextScreenSize,
{
  width: 23,
  height: 34
});
personalizationInstance.addEvent(EventTypes.ContextIpAddress,
{
  ipAddress: "10.0.0.2",
});
personalizationInstance.addEvent(EventTypes.ContextPageView,
{
  url: "http://www.monetate.com/index.html",
  pageType: "Home"
});
// ----------------------------------------------------
// Get Actions
let recData;
personalizationInstance
  .getActionsData("monetate:action:DataCollection", true)
  .then(res =>
  {
    recsData = res[0].actions;
  })
  .catch(error =>
  {
    console.warn('Error!', error);
  });
```
:::
::::

::::Tab{title="iOS"}
:::CodeblockTabs
Data Collection Action Full Example

```swift
import MarqueeLabel
import UIKit
import monetate_ios_sdk

class CategoryViewController: UIViewController {
  final var objPersonalization = Personalization(
    account: Account(
      instance: "p", domain: "localhost.org", name: "a-701b337c", shortname: "localhost"),
    user: User(deviceId: "62bd2e2d-213d-463f-83bb-12c0b2530a14"))
  override func viewDidLoad() {
    super.viewDidLoad()
  }
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    dataCollectionEvent()
  }
  func dataCollectionEvent() {
    objPersonalization.addEvent(context: .PageView, event: PageView(pageType: "Homepage", path: "n/a", url: "n/a", categories: [], breadcrumbs: []))
     
    objPersonalization.getActionsData(requestId: "test_request_id", includeReporting: true, arrActionTypes: ["monetate:action:DataCollection"]).on { (res) in
      if res.status == 200 {
        let data = JSON(res.data)
        print(data)
        self.handleAction(res: res)
      } else {
      }
    }
  }
}
```
:::
::::

::::Tab{title="Android"}
:::CodeblockTabs
Data Collection Action Full Example

```java
// 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&#x20;

:::BlockQuote
**Note:**&#x54;he Context-based getActions() flow replaces the need to manually use addEvent and getActionsData when evaluating actions.
:::

<font color="#15803D">**Supported SDK Versions**</font>

- <font color="#4283c7">**Android**</font>: 1.0.6-A or later&#x20;
- <font color="#4283c7">**React-Native**</font>: 1.0.6-RN or later&#x20;
- <font color="#4283c7">**Web**</font>: 1.0.6-W or later&#x20;
- <font color="#4283c7">**iOS**</font>**:** 2026.01.06 or later

Using the new **Context Layer–based approach**, **DataCollection** Actions can be implemented using a single enhanced **getActions()&#x20;**&#x6D;ethod. Below is the sample code snippet demonstrating this usage.

For detailed information on the enhanced getActions() flow, please refer to the **[Context](https://developer.monetate.com/sdks/objects#mDSKQ)** and **[getActions](https://developer.monetate.com/sdks/getactions)** sections.

### Code Example

:::::Tabs
::::Tab{title="iOS"}
:::CodeblockTabs
&#x20;getActionsData Example

```swift
public func getActions(context:ContextObj,arrActionTypes:[String],includeReporting: Bool)
```
:::
::::

::::Tab{title="Android"}
:::CodeblockTabs
&#x20;getActionsData Example

```java
public void getActions(Context context, String[] actionTypes, boolean includeReports, ActionsCallback actionsCallback)()
```
:::
::::

::::Tab{title="React-Native"}
:::CodeblockTabs{indent="2"}
getAction Method New

```typescript
 async getActions(
        actionType: string | string[],
        context?: any,
        includeReporting: boolean = true,
        errorCallback?: (result: { status: "success" | "failed"; error?: unknown }) => void
    ): Promise<{ data: unknown; errors?: unknown }> {}

```
:::
::::

::::Tab{title="Web"}
:::CodeblockTabs{indent="2"}
getAction Method New

```typescript
 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

:::::Tabs
::::Tab{title="iOS"}
:::CodeblockTabs
Data Collection Action Full Example

```swift
   //setting up context data 
   let contextObj = ContextObj()
        contextObj.addIpAddress("1.0.0.0")
        contextObj.addPageDetails(pageType: "PDP", path: "n/a", url: "n/a", categories: [], breadcrumbs: [])
        contextObj.addScreenSizeData(screenHeight: 1000, screenWidth: 400)
        //Request DataCollection Action data
        objPersonalization.getActions(context: contextObj, arrActionTypes: ["monetate:action:OmnichannelRecommendation"], includeReporting: false)
            .on(success: { (actions) in
                    self.handleRecommendations(actions: actions)
            })
    }
```
:::
::::

::::Tab{title="Android"}
:::CodeblockTabs
Data Collection Action Full Example

```java
//setting up context data
Context context = new Context();
context.addIpAddress("1.0.0.0");			
context.addScreenSizeData(1010, 2121);
context.addPageDetails("homepage", "https://www.monetate.com", new String[] {"categories-here"},new String[] {"breadcrums--here"},"test/path");

//Request DataCollection Action data							
personalization.getActions(context, new String[] {"monetate:action:DataCollection"}, false,new ActionsCallback() {
	@Override
	public void onSuccess(String actions) {
	   //prints the actions data received here
		System.out.println("Action data :"+actions);
	}		
	@Override
	public void onError(Exception e) {
	   //prints the error/exception data here
		System.out.println("Ex :"+e);
	}
});
```
:::
::::

::::Tab{title="React-Native"}
:::CodeblockTabs
Data Collection Action Full Example

```typescript
//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));
```
:::
::::

:::Tab{title="Web"}

:::
:::::



