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-156866 [MILO][MEP] Create martech metadata table if placeholders are used in non EN page #2780

Merged
merged 15 commits into from
Sep 3, 2024
22 changes: 22 additions & 0 deletions libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,25 @@ export function parseManifestVariants(data, manifestPath, targetId) {
return null;
}

export async function createMartechMetadata(placeholders, config, column) {
if (config.locale.ietf === 'en-US') return;

await import('../../martech/attributes.js').then(({ processTrackingLabels }) => {
Copy link
Contributor

@mokimo mokimo Aug 29, 2024

Choose a reason for hiding this comment

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

I'd be a bit careful with those imports as they block the pipeline and we aren't loading things in parallel.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here's a bit of a case-study I did on project unity on this: adobecom/unity#102

config.mep.analyticLocalization ??= {};

placeholders.forEach((item, i) => {
const firstRow = placeholders[i];
let usValue = firstRow['en-us'] || firstRow.us || firstRow.en || firstRow.key;

if (!usValue) return;

usValue = processTrackingLabels(usValue);
const translatedValue = processTrackingLabels(item[column]);
config.mep.analyticLocalization[translatedValue] = usValue;
});
});
}

/* c8 ignore start */
function parsePlaceholders(placeholders, config, selectedVariantName = '') {
if (!placeholders?.length || selectedVariantName === 'default') return config;
Expand All @@ -561,6 +580,9 @@ function parsePlaceholders(placeholders, config, selectedVariantName = '') {
}, {});
config.placeholders = { ...(config.placeholders || {}), ...results };
}

createMartechMetadata(placeholders, config, val);

return config;
}

Expand Down
10 changes: 8 additions & 2 deletions libs/martech/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ const LEAD_UNDERSCORES = /^_+|_+$/g;
export function processTrackingLabels(text, config, charLimit) {
let analyticsValue = text?.replace(INVALID_CHARACTERS, ' ').replace(LEAD_UNDERSCORES, '').trim();
if (config) {
const { analyticLocalization, loc = analyticLocalization?.[analyticsValue] } = config;
if (loc) analyticsValue = loc;
const { analyticLocalization, mep } = config;
const mepLoc = mep?.analyticLocalization?.[analyticsValue];
if (mepLoc) {
analyticsValue = mepLoc;
} else {
const loc = analyticLocalization?.[analyticsValue];
if (loc) analyticsValue = loc;
}
}
if (charLimit) return analyticsValue.slice(0, charLimit);
return analyticsValue;
Expand Down
51 changes: 51 additions & 0 deletions test/features/personalization/createMartechMetadata.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { expect } from '@esm-bundle/chai';
import { createMartechMetadata } from '../../../libs/features/personalization/personalization.js';
import placeholders from './mocks/placeholders.js';

const config = {
locale: { ietf: 'fr-fr' },
mep: {},
};

// Note that the manifestPath doesn't matter as we stub the fetch
describe('test martech metadata creation', () => {
beforeEach(() => {
config.mep = {};
});
it('test two non US manifests', async () => {
expect(config.mep).to.deep.equal({});

await createMartechMetadata(placeholders.geoTest, config, 'fr');
expect(config.mep.analyticLocalization).to.deep.equal({
'value1 fr': 'value1 en us',
'value2 fr': 'value2 en us',
'bonjour fr': 'Hello en us',
'buy now fr': 'buy now en us',
'try now fr': 'try now en us',
});
await createMartechMetadata(placeholders.secondManifestTest, config, 'fr');
expect(config.mep.analyticLocalization).to.deep.equal({
'new fr': 'new en us',
'value1 fr': 'value1 en us',
'value2 fr': 'new2 en us',
'bonjour fr': 'Hello en us',
'buy now fr': 'buy now en us',
'try now fr': 'try now en us',
});
});
it('test one manifest non US withou en-us keys', async () => {
await createMartechMetadata(placeholders.keyTest, config, 'fr');
expect(config.mep.analyticLocalization).to.deep.equal({
'value1 fr': 'test placeholder',
'value2 fr': 'test placeholder2',
'bonjour fr': 'marquee headline',
'buy now fr': 'marquee hollow',
'try now fr': 'marquee solid',
});
});
it('test one manifest en-US', async () => {
config.locale.ietf = 'en-US';
await createMartechMetadata(placeholders.keyTest, config, 'us');
expect(config.mep).to.deep.equal({});
});
});
103 changes: 103 additions & 0 deletions test/features/personalization/mocks/placeholders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const placeholders = {
geoTest: [
{
key: 'test-placeholder',
'mobile-device & us': 'US Mobile Value',
us: 'value1-us',
'en-us': 'value1-en-us',
'ca & not fr': 'value1-ca-not-fr',
fr: 'value1-fr',
'mobile-device': 'value1-mobile',
},
{
key: 'test-placeholder2',
'mobile-device & us': 'US Mobile Value2',
us: 'value2-us',
'en-us': 'value2-en-us',
'ca & not fr': 'value2-ca-not-fr',
fr: 'value2-fr',
'mobile-device': 'value2-mobile',
},
{
key: 'marquee-headline',
'mobile-device & us': 'hello US mobile',
us: 'hello-us',
'en-us': 'Hello-en-us',
'ca & not fr': 'hello-ca-not-fr',
fr: 'bonjour-fr',
'mobile-device': 'hello-mobile',
},
{
key: 'marquee-hollow',
'mobile-device & us': 'buy-now-mobile-us',
us: 'buy-now-us',
'en-us': 'buy-now-en-us',
'ca & not fr': 'buy-now-ca-not-fr',
fr: 'buy-now-fr',
'mobile-device': 'buy-now-mobile',
},
{
key: 'marquee-solid',
'mobile-device & us': 'try-now-mobile-us',
us: 'try-now-us',
'en-us': 'try-now-en-us',
'ca & not fr': 'try-now-ca-not-fr',
fr: 'try-now-fr',
'mobile-device': 'try-now-mobile',
},
],
secondManifestTest: [
{
key: 'test-placeholder',
'mobile-device & us': 'US Mobile Value',
us: 'value1-us',
'en-us': 'new-en-us',
'ca & not fr': 'value1-ca-not-fr',
fr: 'new-fr',
'mobile-device': 'value1-mobile',
},
{
key: 'test-placeholder2',
'mobile-device & us': 'US Mobile Value2',
us: 'value2-us',
'en-us': 'new2-en-us',
'ca & not fr': 'value2-ca-not-fr',
fr: 'value2-fr',
'mobile-device': 'value2-mobile',
},
],
keyTest: [
{
key: 'test-placeholder',
'mobile-device & us': 'US Mobile Value',
fr: 'value1-fr',
'mobile-device': 'value1-mobile',
},
{
key: 'test-placeholder2',
'mobile-device & us': 'US Mobile Value2',
fr: 'value2-fr',
'mobile-device': 'value2-mobile',
},
{
key: 'marquee-headline',
'mobile-device & us': 'hello US mobile',
fr: 'bonjour-fr',
'mobile-device': 'hello-mobile',
},
{
key: 'marquee-hollow',
'mobile-device & us': 'buy-now-mobile-us',
fr: 'buy-now-fr',
'mobile-device': 'buy-now-mobile',
},
{
key: 'marquee-solid',
'mobile-device & us': 'try-now-mobile-us',
fr: 'try-now-fr',
'mobile-device': 'try-now-mobile',
},
],
};

export default placeholders;
9 changes: 9 additions & 0 deletions test/martech/attributes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,13 @@ describe('Analytics', async () => {
}, 20);
expect(processedString).to.equal('Buy now');
});
it('should process tracking labels with foreign locale and MEP placeholder', () => {
const translatedString = 'Comprar ahora';
const processedString = processTrackingLabels(translatedString, {
locale: { ietf: 'es-ES' },
analyticLocalization: { 'Comprar ahora': 'Buy now' },
mep: { analyticLocalization: { 'Comprar ahora': 'Buy right now' } },
}, 20);
expect(processedString).to.equal('Buy right now');
});
});
Loading