Created
September 14, 2022 05:49
-
-
Save agehlot/634ccf9b13f75b0906db246cfcce9324 to your computer and use it in GitHub Desktop.
Sitecore CDP: DecisionModel: the sample programmable java-script to prepare a payload to trigger Sitecore Send (Moosend). Thanks to brimit for initial code sample.
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 () | |
| { | |
| var fallbackImage = "https://mydemo.blob.core.windows.net/assets/b98a38c3-b757-5a2d-ad32-cf64c37ef0f5"; | |
| var defaultWebsite = '0af7e4acd0454226bcc982ec8de42a38'; | |
| var perfumeWebsiteId = '426efbad-a306-4cf2-9071-1a847285773e'; | |
| var websiteId = defaultWebsite; | |
| // detect the website by pointOfSale | |
| if (entity && entity.pointOfSale == 'cdn-send-test-pos') { | |
| websiteId = perfumeWebsiteId; | |
| } | |
| print('website', websiteId); | |
| //#region Tools | |
| function toArray(arr) { | |
| if (!arr || !arr.length) { | |
| return []; | |
| } | |
| var result = []; | |
| for (var i = 0; i < arr.length; i++) { | |
| result.push(arr[i]); | |
| } | |
| return result; | |
| } | |
| // Polyfill array.find | |
| if (!Array.prototype.find) { | |
| Array.prototype.find = function (predicate) { | |
| if (this === null) { | |
| throw new TypeError('null or undefined'); | |
| } | |
| if (typeof predicate !== 'function') { | |
| throw new TypeError('predicate must be a function'); | |
| } | |
| var list = Object(this); | |
| var length = list.length >>> 0; | |
| var thisArg = arguments[1]; | |
| var value; | |
| for (var i = 0; i < length; i++) { | |
| value = list[i]; | |
| if (predicate.call(thisArg, value, i, list)) { | |
| return value; | |
| } | |
| } | |
| return undefined; | |
| }; | |
| } | |
| function mapProduct(entity, skipQuantity) { | |
| var ext = entity.arbitraryData.product.ext; | |
| var result = { | |
| itemCode: entity.arbitraryData.product.productId, | |
| itemPrice: entity.arbitraryData.product.price, | |
| itemUrl: "https://" + entity.pointOfSale + entity.arbitraryData.page, | |
| itemName: entity.arbitraryData.product.name, | |
| itemImage: (ext && ext.image) || (ext && ext.thumbnail) || fallbackImage, | |
| currencyCode: entity.arbitraryData.product.currencyCode, | |
| }; | |
| if (!skipQuantity) { | |
| result.itemQuantity = entity.arbitraryData.product.quantity; | |
| result.itemTotalPrice = entity.arbitraryData.product.price * entity.arbitraryData.product.quantity; | |
| } | |
| return result; | |
| } | |
| function mapAddEvent(entity, response) { | |
| response.actionType = "ADDED_TO_ORDER"; | |
| response.Url = "https://" + entity.pointOfSale + entity.arbitraryData.page; | |
| response.properties = { | |
| product: mapProduct(entity) | |
| } | |
| return response; | |
| } | |
| function getAllAddEvents(sessionId) { | |
| var session = toArray(guest.sessions).find(x => x.ref == sessionId); | |
| if (!session) { | |
| return []; | |
| } | |
| return toArray(session.events).filter(x => x.type == 'ADD'); | |
| } | |
| function getAllViewEvents(sessionId) { | |
| var session = toArray(guest.sessions).find(x => x.ref == sessionId); | |
| if (!session) { | |
| return []; | |
| } | |
| return toArray(session.events).filter(x => x.type == 'VIEW' && x.arbitraryData && x.arbitraryData.product && x.arbitraryData.product.productId); | |
| } | |
| function getAllProductsFromSession(sessionId) { | |
| return getAllAddEvents(sessionId).map(e => mapProduct(e)); | |
| } | |
| //#endregion | |
| if (!guest.email && entity.type != 'VIEW') { | |
| print('Unknown user'); | |
| return { | |
| data: [], | |
| json: JSON.stringify([]) | |
| } | |
| } | |
| var send_event = { | |
| ContactId: guest.ref, | |
| actionType: "register", | |
| sessionId: sessionId, | |
| siteId: websiteId, | |
| ContactEmailAddress: guest.email | |
| }; | |
| if (guest.email) { | |
| send_event.ContactEmailAddress = guest.email; | |
| } | |
| if (entity === undefined || !entity.type) { | |
| entity = { | |
| type: "IDENTITY", | |
| }; | |
| } | |
| print('entity', entity); | |
| if (entity.type == "ADD" || entity.type == "CHECKOUT" || entity.type == "VIEW") { | |
| send_event.sessionId = entity.sessionRef; | |
| if (entity.type == "ADD") { | |
| mapAddEvent(entity, send_event); | |
| } | |
| else if (entity.type == "CHECKOUT") { | |
| send_event.actionType = "ORDER_COMPLETED"; | |
| send_event.Url = "https://" + entity.pointOfSale + entity.arbitraryData.page; | |
| var products = getAllProductsFromSession(entity.sessionRef); | |
| if (products.length > 0) { | |
| send_event.properties = [{ | |
| products: products | |
| }]; | |
| } else { | |
| send_event.actionType = "ORDER_COMPLETED_EMPTY"; | |
| } | |
| } else if (entity.type == "VIEW" && entity.arbitraryData && entity.arbitraryData.product && entity.arbitraryData.product.productId) { | |
| // https://help.moosend.com/hc/en-us/articles/4413852278162-Install-website-tracking-by-using-the-JS-tracking-library | |
| send_event.actionType = "PAGE_VIEWED"; | |
| send_event.Url = "https://" + entity.pointOfSale + entity.arbitraryData.page; | |
| products = [{ | |
| product: mapProduct(entity, true) | |
| }]; | |
| send_event.properties = products; | |
| } else { | |
| return { | |
| data: [], | |
| json: JSON.stringify([]) | |
| } | |
| } | |
| } | |
| if (entity.type == "IDENTITY") { | |
| send_event.actionType = 'IDENTIFY'; | |
| send_event.sessionId = entity.sessionRef; | |
| var actions = [send_event]; | |
| var viewEvents = getAllViewEvents(entity.sessionRef); | |
| print('getAllViewEvents', viewEvents.length); | |
| for (var j = 0; j < viewEvents.length; j++) { | |
| actions.push({ | |
| actionType: 'PAGE_VIEWED', | |
| Url: "https://" + viewEvents[j].pointOfSale + viewEvents[j].arbitraryData.page, | |
| properties: [{ | |
| product: mapProduct(viewEvents[j], true) | |
| }] | |
| }); | |
| } | |
| var productEvents = getAllAddEvents(entity.sessionRef); | |
| print('getAllAddEvents', productEvents.length); | |
| for (var i = 0; i < productEvents.length; i++) { | |
| actions.push(mapAddEvent(productEvents[i], { | |
| ContactId: send_event.ContactId, | |
| sessionId: send_event.sessionId, | |
| siteId: send_event.siteId, | |
| ContactEmailAddress: send_event.ContactEmailAddress | |
| })) | |
| } | |
| print(actions); | |
| return { | |
| data: actions, | |
| json: JSON.stringify(actions), | |
| } | |
| } | |
| print(JSON.stringify(send_event)); | |
| return { | |
| data: [send_event], | |
| json: JSON.stringify([send_event]) | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment