Free javascript Hosting


inapp-payload-personalization2.js

Uploaded on Jan 14 2022 13:44 by jobin.netcore

// RegEx to find [%__CONTENT__%], example [%__name$Product View$no product__%]

const recoBlockRegEx = /\[\%__(.*?)__\%\]/g;
const tagLength = 4;
const splitChar = '$';
const recoElements = document.querySelectorAll('.NC_RECO_SHOW');
const activities = new Set();
try {
if (recoElements.length > 0) {
// find each activity name and add it to activities set
recoElements.forEach(item => {
const matches = item.innerHTML.match(recoBlockRegEx);
matches && matches.forEach(item => {
const eventData = item.substring(tagLength, item.length - tagLength);
activities.add(eventData.split(splitChar)[1]);
});

})
if(activities.size > 0){
console.log('Before calling app method for events');
if (typeof jse !== 'undefined' && typeof jse.getEventListFromJS !== 'undefined') {
console.log('Calling jse method of android');
jse.getEventListFromJS(JSON.stringify(Array.from(activities)));
} else if (typeof window.webkit !== 'undefined' && typeof window.webkit.messageHandlers !== 'undefined' && typeof window.webkit.messageHandlers.getEventListFromJS !== 'undefined') {
console.log('Calling IOS method for events');
window.webkit.messageHandlers.getEventListFromJS.postMessage(JSON.stringify(Array.from(activities)))
} else {
/*
// Test payload with all possible structure
var payloads = [
{
eventId: 1,
eventName: 'page browse',
payload: {
"screenname": "Events Screen"
}
},
{
eventId: 2,
eventName: 'add to cart',
payload: {
product: {
'price': 200,
'name': 'sparx'
}
}
},
{
eventId: 3,
eventName: 'checkout',
payload: {
product: [
{
'item_price': 900,
'company': 'Vivo'
},
{
'item_price': 800,
'company': 'Microsoft'
}
]
}
}
];
payloadFromSdk(payloads);
// comment display message if you are testing outside app
*/
console.log('Not android or IOS method found to get payload data hence calling displayMessage directly');
displayMessage();
}
}
else{
console.log('No activities to personalize');
displayMessage();
}
} else {
console.log('No activities to personalize');
displayMessage();
}
} catch (e) {
console.log('unable to call getEventListFromJS', e);
displayMessage();
}
function payloadFromSdk(payloads) {
console.log('payloadFromSdk >>>>> ', JSON.stringify(payloads));
const finalContent = getPersonalizedContent(payloads);
document.documentElement.innerHTML = finalContent;
swapUiAndActualCss();
setTimeout(() => {
displayMessage();
}, 1000);
}
function displayMessage() {
try {
console.log('We are inside show inapp to check android or IOS device');
if (typeof jse !== 'undefined' && typeof jse.showInApp !== 'undefined') {
console.log('Calling android method to show inapp');
jse.showInApp();
} else if (typeof window.webkit !== 'undefined' && typeof window.webkit.messageHandlers !== 'undefined' && typeof window.webkit.messageHandlers.showInApp !== 'undefined' && typeof window.webkit.messageHandlers.showInApp.postMessage !== 'undefined') {
console.log('Calling IOS method to show inapp');
window.webkit.messageHandlers.showInApp.postMessage('inapp_ios_success')
}
} catch (e) {
console.log('Unable to call showInApp', e);
}
}
function getPersonalizedContent(payloads) {
const content = document.documentElement.innerHTML;
const contentArray = [];
var personalised = content.match(/%__(.*?)__%/g) || [];
var finalContent = content;
personalised.forEach(function (element) {
personalised = element.replace(/%__/g, '');
personalised = personalised.replace(/__%/g, '');
const res = personalised.split("$");
// here get respected activity from array given by android
var activity = '';
payloads.forEach(item => {
if (item.eventName && item.eventName.toLowerCase() === res[1].toLowerCase()) {
activity = item;
}
console.log(item);
});
if (activity) {
const contentPiece = makePersonalizedContent(res, activity);
contentArray.push(contentPiece);
} else {
contentArray.push(res[2]);
}
});
contentArray.forEach(function (el) {
finalContent = finalContent.replace(/\[%(.*?)%\]/, el);
})
return finalContent;
}
function makePersonalizedContent(res, activity) {
activity = activity['payload'] || '';
if (res.length === 3) {
const payload = res[0];
var personalizedContent = activity[payload];
personalizedContent = validateItemsArrayPayload(payload, personalizedContent, activity)
}
if (!personalizedContent) {
personalizedContent = res[2];
}
return personalizedContent;
}
function validateItemsArrayPayload(payload, personalizedContent, activity) {
var isItem = payload.includes(".");
if (isItem) {
var payloadArr = payload.split('.'),
itemPayload = payloadArr[1] || '',
checkIfItems = payloadArr[0] || '';
}
if(isItem && checkIfItems && checkIfItems.endsWith('[]')) {
// its an array
var keyName = checkIfItems.slice(0, -2);
if(activity && activity[keyName]) {
for(let i=0; i<activity[keyName].length; i++) {
if(activity[keyName][i][itemPayload]) {
personalizedContent = activity[keyName][i][itemPayload];
break;
}
}
}
} else if(isItem) {
// its an json object
personalizedContent = activity[checkIfItems][itemPayload];
}
return personalizedContent;
}
function swapUiAndActualCss() {
const dummyElements = document.getElementsByClassName('NC_RECO_HIDE'),
recoContainers = document.getElementsByClassName('NC_RECO_SHOW');
if (dummyElements.length > 0) {
for (var i = 0; i < dummyElements.length; i++) {
dummyElements[i].hidden = true;
}
}
if (dummyElements.length > 0) {
for (var i = 0; i < recoContainers.length; i++) {
recoContainers[i].hidden = false;
}
}
}

Back to list