Skip to content

Commit

Permalink
Fix decode URI at the base
Browse files Browse the repository at this point in the history
  • Loading branch information
swamu committed Dec 9, 2024
1 parent aaedf58 commit 2e0bd5a
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions libs/martech/helpers.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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: [{
Expand All @@ -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,
}],
Expand Down Expand Up @@ -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;
Expand All @@ -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}`);
}

Check warning on line 335 in libs/martech/helpers.js

View check run for this annotation

Codecov / codecov/patch

libs/martech/helpers.js#L334-L335

Added lines #L334 - L335 were not covered by tests
}

Expand Down Expand Up @@ -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'));
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2e0bd5a

Please sign in to comment.