diff --git a/libs/features/personalization/personalization.js b/libs/features/personalization/personalization.js index a7502ec814..bf1cdf5caf 100644 --- a/libs/features/personalization/personalization.js +++ b/libs/features/personalization/personalization.js @@ -914,6 +914,15 @@ async function setMepCountry(config) { } } +async function setMepLob(config) { + try { + const lobPromise = await config.mep.userLOBPromise; + if (lobPromise) config.mep.meplob = lobPromise; + } catch (e) { + log('MEP Error: Unable to get user line of business'); + } +} + async function getPersonalizationVariant( manifestPath, variantNames = [], @@ -945,6 +954,7 @@ async function getPersonalizationVariant( if (name.toLowerCase().startsWith('previouspage-')) return checkForPreviousPageMatch(name); if (hasCountryMatch(name, config)) return true; if (userEntitlements?.includes(name)) return true; + if (config.mep?.meplob && config.mep?.meplob === name.split('lob-')[1]?.toLowerCase()) return true; return PERSONALIZATION_KEYS.includes(name) && PERSONALIZATION_TAGS[name](); }; @@ -960,9 +970,8 @@ async function getPersonalizationVariant( return !processedList.includes(false); }; - if (config.mep?.geoLocation) { - await setMepCountry(config); - } + if (config.mep?.geoLocation) await setMepCountry(config); + if (config.mep?.meplob === true) await setMepLob(config); const matchingVariant = variantNames.find((variant) => variantInfo[variant].some(matchVariant)); return matchingVariant; @@ -1447,7 +1456,7 @@ export async function init(enablements = {}) { const { mepParam, mepHighlight, mepButton, pzn, pznroc, promo, enablePersV2, target, ajo, countryIPPromise, mepgeolocation, targetInteractionPromise, calculatedTimeout, - postLCP, + postLCP, meplob, userLOBPromise, } = enablements; const config = getConfig(); if (postLCP) { @@ -1467,8 +1476,9 @@ export async function init(enablements = {}) { countryIPPromise, geoLocation: mepgeolocation, targetInteractionPromise, + meplob, + userLOBPromise, }; - manifests = manifests.concat(await combineMepSources(pzn, pznroc, promo, mepParam)); manifests?.forEach((manifest) => { if (manifest.disabled) return; diff --git a/libs/utils/utils.js b/libs/utils/utils.js index 51afaa30ae..a1d2222c74 100644 --- a/libs/utils/utils.js +++ b/libs/utils/utils.js @@ -1386,6 +1386,42 @@ export function enablePersonalizationV2() { return !!enablePersV2 && isSignedOut(); } +export function getCookie(key) { + const cookie = document.cookie.split(';') + .map((x) => decodeURIComponent(x.trim()).split(/=(.*)/s)) + .find(([k]) => k === key); + return cookie ? cookie[1] : null; +} +export async function getSpectraLOB(lastVisitedPage, config = getConfig()) { + const getECID = getCookie('AMCV_9E1005A551ED61CA0A490D45@AdobeOrg'); + if (!getECID) return false; + const [, ECID] = getECID.split('|'); + const apiDomain = config?.env.name === 'prod' ? '' : '-stage'; + let apiUrl = `https://cchome${apiDomain}.adobe.io/int/v1/aep/events/webpage?ecid=${ECID}`; + if (lastVisitedPage) { + const newUrl = new URL(lastVisitedPage); + const refWithoutParams = `${newUrl.origin}${newUrl.pathname}`; + apiUrl = `${apiUrl}&lastVisitedPage=${refWithoutParams}`; + } + + try { + const rawResponse = await fetch(apiUrl, { + method: 'GET', + headers: { + 'x-api-key': 'MarketingTech', + 'Content-Type': 'application/json', + }, + body: null, + }); + const content = await rawResponse.json(); + return content.modelLineOfBusiness?.toLowerCase(); + } catch (e) { + if (e.name === 'TimeoutError') window.lana?.log('Spectra Timeout'); // Abort signal + else window.lana?.log(e.reason || e.error || e.message, { errorType: 'i' }); + return false; + } +} + async function checkForPageMods() { const { mep: mepParam, @@ -1395,6 +1431,7 @@ async function checkForPageMods() { } = Object.fromEntries(PAGE_URL.searchParams); let targetInteractionPromise = null; let countryIPPromise = null; + let userLOBPromise = null; let calculatedTimeout = null; if (mepParam === 'off') return; @@ -1405,6 +1442,7 @@ async function checkForPageMods() { const xlg = martech === 'off' ? false : getMepEnablement('xlg'); const ajo = martech === 'off' ? false : getMepEnablement('ajo'); const mepgeolocation = getMepEnablement('mepgeolocation'); + const meplob = getMepEnablement('lob'); if (!(pzn || pznroc || target || promo || mepParam || mepHighlight || mepButton || mepParam === '' || xlg || ajo)) return; @@ -1417,6 +1455,8 @@ async function checkForPageMods() { countryIPPromise = getAkamaiCode(true); } } + if (meplob === true) userLOBPromise = getSpectraLOB(document.referrer); + const enablePersV2 = enablePersonalizationV2(); if ((target || xlg) && enablePersV2) { const params = new URL(window.location.href).searchParams; @@ -1463,6 +1503,8 @@ async function checkForPageMods() { targetInteractionPromise, calculatedTimeout, enablePersV2, + userLOBPromise, + meplob, }); }