SDK Methods

The following methods are available to use in the SDK. All methods are part of the personalization class.

For parameters that require event names, refer to SDK Events and Action Types for syntax.

generateMonetateId

Generates a Monetate ID and returns it as a string. You can store this ID locally for future use.

public static string generateMonetateId();

Example code:

String monetateId = Personalization.generateMonetateId();

report

Reports an event to Monetate. This allows data to later be used for decisions within Monetate. Use this method to report events.

public void report(String context, Object events){}

Parameters:

  • context is name of the event. (Required)
  • events is the event data. (Required)

This method must be run in a thread.

Example code:

Account account = new Account("localhost.org", "a-701b337c", "p", "localhost");
User user = new User();
user.setDeviceId("1109349827122");

try
{
  Personalization personalization = new Personalization(user, account);
  IpAddress address = new IpAddress();
  address.setIpAddress("127.0.0.0.");
  new Thread(new Runnable()
  {
    @Override
    public void run()
    {
      personalization.report(EventTypes.ContextIpaddress, address);
    }
  }).start();
}
catch (AccountDetailsException e)
{
  e.printStackTrace();
}

addEvent

Adds an event to the SDK's internal stack.  Use this method to add events to the stack that you intend to use to trigger experiences. You can use multiple calls of this method to add multiple events to stack. Use this method with getActionsData when an experience requires data from more than one event.

public void addEvent(String context, Object event)

Parameters:

  • context is name of the event. (Required)
  • event is the event data. (Required)

Example code:

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");

personalization.addEvent(EventTypes.ContextIpAddress, ipAddress);
personalization.addEvent(EventTypes.ContextPageView, pageView);
personalization.addEvent(EventTypes.ContextScreenSize, screenSize);

getActionsData

Request an experience decision from Monetate based off the action type. You can specify one action or multiple action types in an array to get multiple responses.

The experience decision depends on event data added using addEvent calls. Use addEvent to add all of the relevant events before you use this method to request a decision.

public String getActionsData(String[] actionTypes, boolean includeReports)

Parameters:

  • 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.

Example code:

try
{
  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:OmnichannelJson"}, 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();
}
catch (AccountDetailsException e)
{
  e.printStackTrace();
}

getActions

Reports an event and immediately requests a decision from Monetate. Use this method if an experience you want to trigger requires a single event. This method returns a JSON object that includes the response data.

The response data can then be used in your application. For example, you can use this method to obtain data to display as a banner on a page.

public String getActions(String context, Object event, String[] actionTypes, boolean includeReports) {returns response;}

Parameters:

  • context is name of the event.
  • event is the data associated with the event.
  • 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.

Example code:

Account account = new Account("localhost.org", "a-701b337c", "p", "localhost");
User user = new User();
user.setDeviceId("1109349827122");

try
{
  Personalization personalization = new Personalization(user, account);
  IpAddress address = new IpAddress();
  address.setIpAddress("127.0.0.0.");
  String responseData;
  
  new Thread(new Runnable()
  {
    @Override
    public void run()
    {
      // Get the responseData using getActions
      responseData = personalization.getActions(EventTypes.ContextIpAddress, address, new String[] {"monetate:action:OmnichannelJson"}, true);
      // Once you receive the responseData from getActions, 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();
}
catch (AccountDetailsException e)
{
  e.printStackTrace();
}

flush

Immediately sends all event data that are currently queued. Use this method if you want to report an event immediately. This method returns a response of success or failure.

This method might throw the following exceptions that you must handle:

  • InterruptedException
  • ExecutionException
  • TimeoutException
public String flush() throws InterruptedException, ExecutionException, TimeoutException {}

Example code:

try
{
  Personalization personalization = new Personalization(user, account);
  IpAddress address = new IpAddress();
  address.setIpAddress("127.0.0.0.");
  new Thread(new Runnable()
  {
    @Override
    public void run()
    {
      Personalization.report(EventTypes.ContextIpaddress, address);
      personalization.flush();
    }
  }).start();
}
catch (Exception e)
{
  e.printStackTrace();
}

setCustomerId

Updates the customerId within the User object.

public void setCustomerId(String customerId);

Parameters:

  • customerId is a string containing the customer ID. (Required)

Example code:

personalization.setCustomerId("test_customer_id");