Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MWPW-151790[MILO][MEP] Organize getPersConfig function in personalization.js #2454

Merged
merged 10 commits into from
Jun 13, 2024
107 changes: 52 additions & 55 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 @@ -558,6 +558,11 @@ const checkForParamMatch = (paramStr) => {
};

async function getPersonalizationVariant(manifestPath, variantNames = [], variantLabel = null) {
const config = getConfig();
if (config.mep?.variantOverride?.[manifestPath]) {
return config.mep.variantOverride[manifestPath];
}

const variantInfo = variantNames.reduce((acc, name) => {
let nameArr = [name];
if (!name.startsWith(TARGET_EXP_PREFIX)) nameArr = name.split(',');
Expand All @@ -572,7 +577,6 @@ async function getPersonalizationVariant(manifestPath, variantNames = [], varian

let userEntitlements = [];
if (hasEntitlementTag) {
const config = getConfig();
userEntitlements = await config.entitlements();
}

Expand Down Expand Up @@ -608,7 +612,7 @@ const createDefaultExperiment = (manifest) => ({
variants: {},
});

export async function getPersConfig(info, override = false) {
export async function getManifestConfig(info, variantOverride = false) {
const {
name,
manifestData,
Expand All @@ -620,7 +624,7 @@ export async function getPersConfig(info, override = false) {
disabled,
event,
} = info;
if (disabled && !override) {
if (disabled && !variantOverride) {
return createDefaultExperiment(info);
}
let data = manifestData;
Expand All @@ -633,17 +637,17 @@ export async function getPersConfig(info, override = 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 @@ -657,8 +661,8 @@ export async function getPersConfig(info, override = 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 @@ -668,43 +672,43 @@ export async function getPersConfig(info, override = 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 @@ -743,26 +747,19 @@ export async function categorizeActions(experiment) {
};
}

function overridePersonalizationVariant(manifest, config) {
const { manifestPath, variantNames } = manifest;
if (!config.mep?.override) return;
let selectedVariant;
config.mep?.override?.split('---').some((item) => {
function parseMepParam(mepParam) {
if (!mepParam) return false;
const mepObject = Object.create(null);
const decodedParam = decodeURIComponent(mepParam);
decodedParam.split('---').forEach((item) => {
const pair = item.trim().split('--');
if (pair[0] === manifestPath && pair.length > 1) {
[, selectedVariant] = pair;
return true;
if (pair.length > 1) {
const [manifestPath, selectedVariant] = pair;
mepObject[manifestPath] = selectedVariant;
}
return false;
});
if (!selectedVariant) return;
if (variantNames.includes(selectedVariant)) {
manifest.selectedVariantName = selectedVariant;
manifest.selectedVariant = manifest.variants[selectedVariant];
return;
}
manifest.selectedVariantName = selectedVariant;
manifest.selectedVariant = manifest.variants[selectedVariant];

return mepObject;
}

function compareExecutionOrder(a, b) {
Expand All @@ -789,11 +786,11 @@ export function cleanAndSortManifestList(manifests) {
freshManifest.name = fullManifest.name;
freshManifest.selectedVariantName = fullManifest.selectedVariantName;
freshManifest.selectedVariant = freshManifest.variants[freshManifest.selectedVariantName];
delete freshManifest.variants;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this one is the only business logic change. Everything else is a name swap. Is this prop dele intentional ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is intentional. I just updated the comment to mention it. Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markpadbe I hate to say it, but let's not remove. I forgot about this use of the object. We're essentially trying to take the decision from the Target instance and merge it with the fresh data of the imported instance (when there's a duplicate). So I worry if we delete the variants we create a small fringe case of deleting it and then needing it later in the loop.

manifestObj[manifest.manifestPath] = freshManifest;
} else {
manifestObj[manifest.manifestPath] = manifest;
}
if (config.mep?.override) overridePersonalizationVariant(manifest, config);
} catch (e) {
console.warn(e);
window.lana?.log(`MEP Error parsing manifests: ${e.toString()}`);
Expand Down Expand Up @@ -832,7 +829,7 @@ export async function applyPers(manifests, postLCP = false) {
handleFragmentCommand,
preview: (mepButton !== 'off'
&& (config.env?.name !== 'prod' || mepParam || mepParam === '' || mepButton)),
override: mepParam ? decodeURIComponent(mepParam) : '',
variantOverride: parseMepParam(mepParam),
highlight: (mepHighlight !== undefined && mepHighlight !== 'false'),
mepParam,
targetEnabled: config.mep?.targetEnabled,
Expand All @@ -842,7 +839,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?.override);
experiments[i] = await getManifestConfig(experiments[i], config.mep?.variantOverride);
}

experiments = cleanAndSortManifestList(experiments);
Expand Down
Loading