Skip to content

Commit

Permalink
MWPW-151790[MILO][MEP] Organize getPersConfig function in personaliza…
Browse files Browse the repository at this point in the history
…tion.js (#2454)

* replace overridePersonalizationVariant function

* Fix chaining

* Move parseMepParam

* add additional optional chaining operator

Co-authored-by: Vivian A Goodrich <101133187+vgoodric@users.noreply.github.com>

* Refactor personalization.js

* Remove comment

* Add back in deleted variants

---------

Co-authored-by: Vivian A Goodrich <101133187+vgoodric@users.noreply.github.com>
  • Loading branch information
markpadbe and vgoodric authored Jun 13, 2024
1 parent 2614acd commit 96ba26c
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,10 @@ const getVariantInfo = (line, variantNames, variants, manifestId) => {
});
};

export function parseConfig(data, manifestId) {
export function parseManifestVariants(data, manifestId) {
if (!data?.length) return null;

const config = {};
const manifestConfig = {};
const experiences = data.map((d) => normalizeKeys(d));

try {
Expand All @@ -512,12 +512,12 @@ export function parseConfig(data, manifestId) {

experiences.forEach((line) => getVariantInfo(line, variantNames, variants, manifestId));

config.variants = variants;
config.variantNames = variantNames;
return config;
manifestConfig.variants = variants;
manifestConfig.variantNames = variantNames;
return manifestConfig;
} catch (e) {
/* c8 ignore next 3 */
console.log('error parsing personalization config:', e, experiences);
console.log('error parsing personalization manifestConfig:', e, experiences);
}
return null;
}
Expand Down Expand Up @@ -612,7 +612,7 @@ const createDefaultExperiment = (manifest) => ({
variants: {},
});

export async function getPersConfig(info, variantOverride = false) {
export async function getManifestConfig(info, variantOverride = false) {
const {
name,
manifestData,
Expand All @@ -637,17 +637,17 @@ export async function getPersConfig(info, variantOverride = false) {
if (!persData) return null;

let manifestId = getFileName(manifestPath);
const globalConfig = getConfig();
if (!globalConfig.mep?.preview) {
const config = getConfig();
if (!config.mep?.preview) {
manifestId = false;
} else if (name) {
manifestId = `${name}: ${manifestId}`;
}
const config = parseConfig(persData, manifestId);
const manifestConfig = parseManifestVariants(persData, manifestId);

if (!config) {
if (!manifestConfig) {
/* c8 ignore next 3 */
console.log('Error loading personalization config: ', name || manifestPath);
console.log('Error loading personalization manifestConfig: ', name || manifestPath);
return null;
}

Expand All @@ -661,8 +661,8 @@ export async function getPersConfig(info, variantOverride = false) {
acc[item.key] = item.value;
return acc;
}, {});
config.manifestOverrideName = infoObj?.['manifest-override-name']?.toLowerCase();
config.manifestType = infoObj?.['manifest-type']?.toLowerCase();
manifestConfig.manifestOverrideName = infoObj?.['manifest-override-name']?.toLowerCase();
manifestConfig.manifestType = infoObj?.['manifest-type']?.toLowerCase();
const executionOrder = {
'manifest-type': 1,
'manifest-execution-order': 1,
Expand All @@ -672,43 +672,43 @@ export async function getPersConfig(info, variantOverride = false) {
const index = infoKeyMap[key].indexOf(infoObj[key]);
executionOrder[key] = index > -1 ? index : 1;
});
config.executionOrder = `${executionOrder['manifest-execution-order']}-${executionOrder['manifest-type']}`;
manifestConfig.executionOrder = `${executionOrder['manifest-execution-order']}-${executionOrder['manifest-type']}`;
} else {
// eslint-disable-next-line prefer-destructuring
config.manifestType = infoKeyMap['manifest-type'][1];
config.executionOrder = '1-1';
manifestConfig.manifestType = infoKeyMap['manifest-type'][1];
manifestConfig.executionOrder = '1-1';
}

config.manifestPath = normalizePath(manifestPath);
manifestConfig.manifestPath = normalizePath(manifestPath);
const selectedVariantName = await getPersonalizationVariant(
config.manifestPath,
config.variantNames,
manifestConfig.manifestPath,
manifestConfig.variantNames,
variantLabel,
);

if (selectedVariantName && config.variantNames.includes(selectedVariantName)) {
config.run = true;
config.selectedVariantName = selectedVariantName;
config.selectedVariant = config.variants[selectedVariantName];
if (selectedVariantName && manifestConfig.variantNames.includes(selectedVariantName)) {
manifestConfig.run = true;
manifestConfig.selectedVariantName = selectedVariantName;
manifestConfig.selectedVariant = manifestConfig.variants[selectedVariantName];
} else {
/* c8 ignore next 2 */
config.selectedVariantName = 'default';
config.selectedVariant = 'default';
manifestConfig.selectedVariantName = 'default';
manifestConfig.selectedVariant = 'default';
}

const placeholders = manifestPlaceholders || data?.placeholders?.data;
if (placeholders) {
updateConfig(
parsePlaceholders(placeholders, getConfig(), config.selectedVariantName),
parsePlaceholders(placeholders, getConfig(), manifestConfig.selectedVariantName),
);
}

config.name = name;
config.manifest = manifestPath;
config.manifestUrl = manifestUrl;
config.disabled = disabled;
config.event = event;
return config;
manifestConfig.name = name;
manifestConfig.manifest = manifestPath;
manifestConfig.manifestUrl = manifestUrl;
manifestConfig.disabled = disabled;
manifestConfig.event = event;
return manifestConfig;
}

export const deleteMarkedEls = (rootEl = document) => {
Expand Down Expand Up @@ -838,7 +838,7 @@ export async function applyPers(manifests, postLCP = false) {
if (!manifests?.length) return;
let experiments = manifests;
for (let i = 0; i < experiments.length; i += 1) {
experiments[i] = await getPersConfig(experiments[i], config.mep?.variantOverride);
experiments[i] = await getManifestConfig(experiments[i], config.mep?.variantOverride);
}

experiments = cleanAndSortManifestList(experiments);
Expand Down

0 comments on commit 96ba26c

Please sign in to comment.