Examples of programmable decisions
The following are examples of programmable decisions to help get you started writing JavaScript in a a decision model variant.
Example 1: Retrieve custom attributes in a programmable decision
You can leverage custom guest attributes that are specific to your organisation, as inputs in decisioning. For example, use the guest's loyalty membership tier as an input in a decision model variant. This can ensure that you only display offers and content relevant to the guest's current membership tier.
Ensure you set the Return Type as Map for the programmable decision.
(function () {
// change the guest data extension details for the required one. Snippet below gives
// "name": "loyalty"
// "key": "privilegeClub"
var loyaltyData = {};
for (var i = 0; i < guest.dataExtensions.length; i++) {
if (guest.dataExtensions[i].name === 'loyalty' && guest.dataExtensions[i].key === 'privilegeClub') {
loyaltyData = guest.dataExtensions[i].values;
return loyaltyData;
}
}
return loyaltyData;
})();Example 2: Retrieve currency of closed session in a programmable decision
To ensure your organization is consistent in personalizing to the guest in the same currency that they chose in their closed session, you can create a programmable decision that retrieves the currency of the closed session. This is particularly useful in experiments that are designed for retargeting.
Ensure you set the Return Type as String for the programmable decision.
(function () {
// for Abandoned Cart or Session Closed triggered flows, gets the closed session currency
var sessionRef;
if (entity) {
sessionRef = entity.ref;
}
var sessionCurrency = "";
for (var i = 0; i < guest.sessions.length; i++) {
var session = guest.sessions[i];
if (session.ref === sessionRef) {
sessionCurrency = session.currency;
break;
}
}
return sessionCurrency;
})();Using programmable outputs in decision tables
After creating a programmable decision, you can use its output as input for decision tables. This allows you to combine custom JavaScript logic with rule-based decision-making.
We'll show an example on how to use the output from Example 2 in a decision table to create currency-specific offers and pricing.