Created
September 14, 2022 04:58
-
-
Save agehlot/35cae3b683a52cbc092040977b6d2f99 to your computer and use it in GitHub Desktop.
Sitecore CDP: Decision Model - Programmable - JavaScript sample to fetch the list of abandoned cart products in ul/li html format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
function getTriggerSession(){ | |
var sessionRef; | |
if (typeof entity !== 'undefined' && entity && entity.ref !== 'undefined') { | |
sessionRef = entity.ref; | |
} | |
//print (sessionRef); | |
var triggerSession; | |
for (var i = 0; i < guest.sessions.length; i++) { | |
var session = guest.sessions[i]; | |
//print(session); | |
if (session.ref === sessionRef) { | |
triggerSession = session; | |
break; | |
} | |
} | |
return triggerSession; | |
} | |
var triggerSession = getTriggerSession(); | |
if(triggerSession) | |
{ | |
//print(triggerSession); | |
var productTypes = []; | |
htmlBody="<ul>"; | |
for (var i = 0; i < triggerSession.events.length; i++) { | |
var event = triggerSession.events[i]; | |
if (event.type === "ADD" && triggerSession.cartType == "ABANDONED") { | |
productTypes.push({ | |
"item_id": event.arbitraryData.product.item_id, | |
"price": event.arbitraryData.product.price, | |
"currency": event.arbitraryData.product.currency, | |
"abandonedAt": triggerSession.endedAt | |
}); | |
htmlBody +="<li> ItemID : " + event.arbitraryData.product.item_id + ", Price :" + event.arbitraryData.product.price +", Currency:"+ event.arbitraryData.product.currency | |
+ ", AbandonedAt : " + triggerSession.endedAt; | |
htmlBody +="</li>"; | |
} | |
} | |
htmlBody +="</ul>"; | |
print(htmlBody); | |
return { | |
"email": guest.email, | |
"productTypes": productTypes, | |
"htmlBody":htmlBody | |
} | |
} | |
//print(JSON.stringify(finalJSONObect)); | |
//return JSON.stringify(finalJSONObect); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment