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 Event and Action Type Constants for syntax.

report

Reports an event to Monetate. This allows data to later be used for decisions within Monetate. Use this method to record events that you want to log, but do not immediately intend to use to trigger an experience.

React

report(context: EventTypes, events: ContextData): void

Parameters:

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

Example code:

const personalizationInstance = new Personalization(user, account);

const cart = {
  "cartLines": [{
    "sku": "SKU1",
    "pid": "PID1",
    "quantity": 2,
    "currency": "USD",
    "value": "24.00"
  }] 
};
personalizationInstance.report(
  EventTypes.ContextCart, 
  { cartLines: cart }
);

addEvent

Records an event to Monetate. Use this method to record events that you intend to use to trigger experiences. You can use multiple calls of this method to record multiple events for experiences that require multiple conditions.

This method is used to record events that are used by the getActionsData method to determine an experience. If you use this method, you must use getActionsData to trigger the experience that satisfies the recorded events.

React

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

Parameters:

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

Example code:

personalizationInstance.addEvent(EventTypes.ContextIpAddress, {
  ipAddress: "10.0.0.2",
});

personalizationInstance.addEvent(EventTypes.ContextPageView, {
  url: "http://www.monetate.com/index.html",
  pageType: "Home"
});

getActionsData

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

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

React

getActionsData(actionType: ActionType) : Promise<any>

Parameters:

  • actionType is the type of action you want to request. You can specify multiple actions in an array to handle. (Required)

Example code specifying a single action:

personalizationInstance
  .getActionsData(ActionTypes.OmniChannelRecommendation)
  .then(res => {
    recsData = res[0].actions;
  })
  .catch(error => {
    console.warn('Error!', error);
  });

Example code specifying multiple actions:

const requiredActions : [ ActionTypes.OmniChannelRecommendation 
  , ActionTypes.OmniSocialProofData ]
personalizationInstance
  .getActionsData(requiredActions)
  .then(res => {
    recsData = res[0].actions;
  })
  .catch(error => {
    console.warn('Error!', error);
  });

getActions

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

React

getActions(actionType, context?, eventData?) : Promise<any>

Parameters:

  • actionType is the type of action you want to request. You can specify multiple actions in an array to handle. (Required)
  • context is name of the event. (Optional)
  • eventData is the data associated with the event. (Optional)

Example code:

import React, { useEffect, useState} from "react" ;
import { View , Text} from "react-native" ;
import Personalization from '@personalization-js-sdk/react-native';.
import { ActionTypes, EventTypes } from "@personalization-js-sdk/common";

const test = () => {
  const personalizationInstance = new Personalization(account, user)
  const [actionData, setActionData] = useState({});

  const handleGetActions = () => {
    const eventType = EventTypes.ContextCart;
    personalizationInstance.getActions(ActionTypes.OmniChannelJson, eventType)
      .then((res) => {
        if (res.length > 0) {
          res[0].actions.map((action) => {
            if (action.json.meta && action.json.meta.tool === 'react-native') {
              const data = {
                color: action.json.data.json.color,
                text: action.json.data.json.text,
                fontSize: action.json.data.json.fontSize,
                style: action.json.data.json.style,
                buttonText: action.json.data.json.buttonText,
                buttonColor: action.json.data.json.buttonColor,
              };
              setActionData(prevState => {
                return {
                  ...prevState,
                  decisionEventData: data,
                }
              });
            } else {
              console.warn('Meta Object not present in Omni JSON.');
            }
          });
        }
      })
      .catch((error) => console.warn('Please try after some time!', error));
  };

  useEffect(() => {
    handleGetActions();
  }, []);

  return <View>
        <Text> {actionData.buttonText} //Render Data On UI 
    </View>;
};

flush

Immediately sends all event reports 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

React

flush()

Example code:

const Personalization= new Personalization(account, user);

PersonalizationInstance.report (
  'monetate:context:IpAddress',
  { ipAddress: 10.10.10.10 }
),
Personalization.setCustomerId("test_customer_id");
Personalization.flush();

You can also capture the method response in a variable:

const response = Personalization.flush()

setCustomerId

Updates the customerId within the User object.

React

setCustomerId(customerId)

Parameters:

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

Example code:

setCustomerId("test_customer_id");