diff --git a/libs/martech/helpers.js b/libs/martech/helpers.js index 1d0e213918..c70ba49ade 100644 --- a/libs/martech/helpers.js +++ b/libs/martech/helpers.js @@ -1,3 +1,5 @@ +const AMCV_COOKIE = 'AMCV_9E1005A551ED61CA0A490D45%40AdobeOrg'; + /** * Generates a random UUIDv4 using cryptographically secure random values. * This implementation follows the RFC 4122 specification for UUIDv4. @@ -92,14 +94,11 @@ function getDeviceInfo() { * @param {string} key - The cookie key. * @returns {string|null} The cookie value, or null if the cookie doesn't exist. */ -function getCookie(key, sendFullCookie) { +function getCookie(key) { const cookie = document.cookie.split(';') - .map((x) => x.trim().split('=')) + .map((x) => decodeURIComponent(x.trim()).split(/=(.*)/s)) .find(([k]) => k === key); - if (sendFullCookie) { - return cookie; - } return cookie ? cookie[1] : null; } @@ -133,11 +132,10 @@ function setCookie(key, value, options = {}) { * { FPID: [{ id: string, authenticatedState: string, primary: boolean }] } */ function getOrGenerateUserId() { - const experienceCloudCookieName = 'AMCV_9E1005A551ED61CA0A490D45%40AdobeOrg'; - const amcvCookieValue = getCookie(experienceCloudCookieName); + const amcvCookieValue = getCookie(AMCV_COOKIE); // If ECID is not found, generate and return FPID - if (!amcvCookieValue) { + if (!amcvCookieValue || (amcvCookieValue.indexOf('MCMID|') === -1)) { const fpidValue = generateUUIDv4(); return { FPID: [{ @@ -148,11 +146,9 @@ function getOrGenerateUserId() { }; } - // ECID found, return structured ECID object - const extractedEcid = amcvCookieValue.substring(6); // Extract the ECID value from the cookie return { ECID: [{ - id: extractedEcid, + id: amcvCookieValue.match(/MCMID\|([^|]+)/)?.[1], authenticatedState: 'ambiguous', primary: true, }], @@ -318,7 +314,7 @@ function createRequestPayload({ updatedContext, pageName, locale, env }) { * @param {Object} data - The response data from the API. * @returns {string|null} The ECID value, or null if not found. */ -function extractECID(data) { +function extractECIDFromResp(data) { return data.handle .flatMap((item) => item.payload) .find((p) => p.namespace?.code === 'ECID')?.id || null; @@ -330,11 +326,12 @@ function extractECID(data) { * @param {string} ECID - The Experience Cloud ID (ECID). */ function updateAMCVCookie(ECID) { - const cookieName = 'AMCV_9E1005A551ED61CA0A490D45%40AdobeOrg'; - const cookieValue = `MCMID|${ECID}`; + const cookieValue = getCookie(AMCV_COOKIE); - if (getCookie(cookieName) !== cookieValue) { - setCookie(cookieName, `MCMID|${ECID}`); + if (!cookieValue) { + setCookie(AMCV_COOKIE, `MCMID|${ECID}`); + } else if (cookieValue.indexOf('MCMID|') === -1) { + setCookie(AMCV_COOKIE, `${cookieValue}|MCMID|${ECID}`); } } @@ -374,11 +371,9 @@ function getUrl() { * personalization propositions fetched from Adobe Target. */ export const loadAnalyticsAndInteractionData = async ({ locale, env, calculatedTimeout }) => { - const value = getCookie('kndctr_9E1005A551ED61CA0A490D45_AdobeOrg_consent', true); - const isRejectedDecodedURI = value?.[2] === undefined && decodeURIComponent(value?.[1]) === 'general=out'; - const isRejectedURI = value?.[1] === 'general' && value?.[2] === 'out'; + const value = getCookie('kndctr_9E1005A551ED61CA0A490D45_AdobeOrg_consent'); - if (isRejectedDecodedURI || isRejectedURI) { + if (value === 'general=out') { return Promise.reject(new Error('Consent Cookie doesnt allow interact')); } @@ -430,7 +425,7 @@ export const loadAnalyticsAndInteractionData = async ({ locale, env, calculatedT throw new Error('Failed to fetch interact call'); } const targetRespJson = await targetResp.json(); - const ECID = extractECID(targetRespJson); + const ECID = extractECIDFromResp(targetRespJson); // Update the AMCV cookie with ECID updateAMCVCookie(ECID);