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.
static generateMonetateId(): string
Example code:
const 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.
report(context: EventTypes, events: ContextData): void
Parameters:
context
is name of the event.events
is the event data.
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
Adds an event to the Monetate 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.
addEvent(context: EventTypes, events: ContextData): void
Parameters:
context
is name of the event.events
is the event data.
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 added using addEvent
calls. Use addEvent
to add all of the relevant events before you use this method to request a decision.
getActionsData(actionType: Array<string>, includeReporting?: boolean) : Promise<any>
Parameters:
actionType
is the type of action you want to request. You can specify multiple actions in an array to handle.includeReporting
indicates whether the response will have impression reporting data.
Example code specifying a single action:
personalizationInstance
.getActionsData("monetate:action:OmniChannelRecommendation")
.then(res => {
recsData = res[0].actions;
})
.catch(error => {
console.warn('Error!', error);
});
Example code specifying multiple actions:
// For multiple actions, send an array
// When an empty array in passed as an argument, it will fetch all the configured Omnichannel actions
const requiredActions : [ "monetate:action:OmniChannelRecommendation"
, "monetate:action:OmniSocialProofData" ]
personalizationInstance
.getActionsData(requiredActions)
.then(res => {
recsData = res[0].actions;
})
.catch(error => {
console.warn('Error!', error);
});
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.
getActions(actionType, context?, eventData?, includeReporting?) : Promise<any>
Parameters:
actionType
is the type of action you want to request. You can specify multiple actions in an array to handle.context
is name of the event.eventData
is the data associated with the event.includeReporting
indicates whether the response will have impression reporting data.
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("monetate:action: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
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.
setCustomerId(customerId)
Parameters:
customerId
is a string containing the customer ID. (Required)
Example code:
setCustomerId("test_customer_id");