-
Notifications
You must be signed in to change notification settings - Fork 174
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-147206 making promo schedule geo aware #2438
Changes from 1 commit
cd09233
df29e84
0c52771
70ba20d
e0de285
5c15e53
47c7384
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,70 @@ | ||
import { getMetadata } from '../../utils/utils.js'; | ||
import { getMetadata, getConfig } from '../../utils/utils.js'; | ||
|
||
const GMTStringToLocalDate = (gmtString) => new Date(`${gmtString}+00:00`); | ||
|
||
const APAC = ['au', 'cn', 'hk_en', 'hk_zh', 'id_en', 'id_id', 'in', 'in_hi', 'kr', 'my_en', 'my_ms', 'nz', 'ph_en', 'ph_fil', 'sg', 'th_en', 'th_th', 'tw', 'vn_en', 'vn_vi']; | ||
const EMEA = ['ae_en', 'ae_ar', 'africa', 'at', 'be_en', 'be_fr', 'be_nl', 'bg', 'ch_de', 'ch_fr', 'ch_it', 'cis_en', 'cis_ru', 'cz', 'de', 'dk', 'ee', 'eg_ar', 'eg_en', 'es', 'fi', 'fr', 'gr_el', 'gr_en', 'hu', 'ie', 'il_en', 'il_he', 'iq', 'is', 'it', 'kw_ar', 'kw_en', 'lt', 'lu_de', 'lu_en', 'lu_fr', 'lv', 'mena_ar', 'mena_en', 'ng', 'nl', 'no', 'pl', 'pt', 'qa_ar', 'qa_en', 'ro', 'ru', 'sa_en', 'sa_ar', 'se', 'si', 'sk', 'tr', 'ua', 'uk', 'za']; | ||
const AMERICAS = ['us', 'ar', 'br', 'ca', 'ca_fr', 'cl', 'co', 'cr', 'ec', 'gt', 'la', 'mx', 'pe', 'pr']; | ||
const JP = ['jp']; | ||
const REGIONS = { APAC, EMEA, AMERICAS, JP }; | ||
|
||
const getLocaleCode = () => { | ||
const getLocale = () => getConfig()?.locale?.prefix?.substring(1) || 'us'; | ||
return (getLocale()?.split('-')?.pop() || 'us').toLowerCase(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be a function? Looks like it could be a simple constant. |
||
}; | ||
|
||
const localeCode = getLocaleCode(); | ||
const regionCode = Object.keys(REGIONS) | ||
.find((r) => REGIONS[r]?.includes(localeCode))?.toLowerCase() || null; | ||
|
||
export const isDisabled = (event, searchParams) => { | ||
if (!event) return false; | ||
if (!event?.locales?.includes(localeCode)) return true; | ||
const currentDate = searchParams?.get('instant') ? new Date(searchParams.get('instant')) : new Date(); | ||
if ((!event.start && event.end) || (!event.end && event.start)) return true; | ||
return Boolean(event.start && event.end | ||
&& (currentDate < event.start || currentDate > event.end)); | ||
}; | ||
|
||
export default function getPromoManifests(manifestNames, searchParams) { | ||
const getRegionalPromoManifests = (manifestNames, region, searchParams) => { | ||
const attachedManifests = manifestNames | ||
? manifestNames.split(',')?.map((manifest) => manifest?.trim()) | ||
: []; | ||
const schedule = getMetadata('schedule'); | ||
|
||
const schedule = getMetadata(region ? `${region}_schedule` : 'schedule'); | ||
if (!schedule) { | ||
return []; | ||
} | ||
return schedule.split(',') | ||
.map((manifest) => { | ||
const [name, start, end, manifestPath] = manifest.trim().split('|').map((s) => s.trim()); | ||
const [name, start, end, manifestPath, locales] = manifest.trim().split('|').map((s) => s.trim()); | ||
if (attachedManifests.includes(name)) { | ||
const event = { | ||
name, | ||
start: GMTStringToLocalDate(start), | ||
end: GMTStringToLocalDate(end), | ||
}; | ||
if (locales) { | ||
event.locales = locales?.split(';').map((locale) => locale.trim()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we should check here if current page is not in target locales.
(I added toLowerCase, it doesn't hurt) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think i do agree with you. Fix the file you created and this is weird to see on de page |
||
const disabled = isDisabled(event, searchParams); | ||
return { manifestPath, disabled, event }; | ||
} | ||
return null; | ||
}) | ||
.filter((manifest) => manifest != null); | ||
}; | ||
|
||
export default function getPromoManifests(manifestNames, searchParams) { | ||
const promoManifests = getRegionalPromoManifests( | ||
manifestNames[`${regionCode}_manifestnames`], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we want to wrap the first method call in |
||
regionCode, | ||
searchParams, | ||
); | ||
const globalPromoManifests = getRegionalPromoManifests( | ||
manifestNames.manifestnames, | ||
null, | ||
searchParams, | ||
); | ||
return [...promoManifests, ...globalPromoManifests]; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -139,6 +139,8 @@ export const MILO_EVENTS = { DEFERRED: 'milo:deferred' }; | |||||
const LANGSTORE = 'langstore'; | ||||||
const PAGE_URL = new URL(window.location.href); | ||||||
|
||||||
const PROMO_PARAM = 'promo'; | ||||||
|
||||||
function getEnv(conf) { | ||||||
const { host } = window.location; | ||||||
const query = PAGE_URL.searchParams.get('env'); | ||||||
|
@@ -873,12 +875,36 @@ const getMepValue = (val) => { | |||||
return finalVal; | ||||||
}; | ||||||
|
||||||
const getMdValue = (key) => { | ||||||
const value = getMetadata(key); | ||||||
if (value) { | ||||||
return getMepValue(value); | ||||||
} | ||||||
return false; | ||||||
}; | ||||||
|
||||||
const getPromoMepEnablement = () => { | ||||||
const mds = ['apac_manifestnames', 'emea_manifestnames', 'americas_manifestnames', 'jp_manifestnames', 'manifestnames']; | ||||||
const mdObject = mds.reduce((obj, key) => { | ||||||
const val = getMdValue(key); | ||||||
if (val) { | ||||||
obj[key] = getMdValue(key); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
return obj; | ||||||
}, {}); | ||||||
if (Object.keys(mdObject).length > 0) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return mdObject; | ||||||
} | ||||||
return false; | ||||||
}; | ||||||
|
||||||
export const getMepEnablement = (mdKey, paramKey = false) => { | ||||||
const paramValue = PAGE_URL.searchParams.get(paramKey || mdKey); | ||||||
if (paramValue) return getMepValue(paramValue); | ||||||
const mdValue = getMetadata(mdKey); | ||||||
if (!mdValue) return false; | ||||||
return getMepValue(mdValue); | ||||||
if (PROMO_PARAM === paramKey) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return getPromoMepEnablement(); | ||||||
} | ||||||
return getMdValue(mdKey); | ||||||
}; | ||||||
|
||||||
export const combineMepSources = async (persEnabled, promoEnabled, mepParam) => { | ||||||
|
@@ -922,7 +948,7 @@ async function checkForPageMods() { | |||||
const { mep: mepParam } = Object.fromEntries(PAGE_URL.searchParams); | ||||||
if (mepParam === 'off') return; | ||||||
const persEnabled = getMepEnablement('personalization'); | ||||||
const promoEnabled = getMepEnablement('manifestnames', 'promo'); | ||||||
const promoEnabled = getMepEnablement('manifestnames', PROMO_PARAM); | ||||||
const targetEnabled = getMepEnablement('target'); | ||||||
const mepEnabled = persEnabled || targetEnabled || promoEnabled || mepParam; | ||||||
if (!mepEnabled) return; | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
<meta name="manifestnames" content="pre-black-friday-global,black-friday-global,cyber-monday"> | ||
<meta name="manifestnames" content="pre-black-friday-global,black-friday-global,cyber-monday,bf-us"> | ||
<meta name="emea_manifestnames" content="bf-de"> | ||
<meta name="americas_manifestnames" content="bf-us"> | ||
<meta name="schedule" content="pre-black-friday-global | 2000-11-01T00:00:00 | 2300-12-15T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/pre-black-friday/manifest-global.json, black-friday-global | 2000-12-15T00:00:00 | 2000-12-31T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/black-friday/manifest-global.json"> | ||
<meta name="emea_schedule" content="bf-de | 2000-11-01T00:00:00 | 2300-12-15T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/bf-de.json | de;ch, black-friday-de | 2000-12-15T00:00:00 | 2300-12-31T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/black-friday/manifest-de.json"> | ||
<meta name="americas_schedule" content="bf-us | 2000-11-01T00:00:00 | 2300-12-15T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/bf-us.json | ca;us, bf-ca | 2000-11-01T00:00:00 | 2300-12-15T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/bf-ca.json | ca,black-friday-us | 2000-12-15T00:00:00 | 2300-12-31T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/black-friday/manifest-us.json"> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
<meta name="personalization" content="https://main--milo--adobecom.hlx.page/products/special-offers-manifest.json"> | ||
<meta name="manifestnames" content="pre-black-friday-global,black-friday-global"> | ||
<meta name="americas_manifestnames" content="us-special-promo"> | ||
<meta name="apac_manifestnames" content="kr-special-promo"> | ||
<meta name="schedule" content="pre-black-friday-global | 2023-11-01T00:00:00 | 2023-12-15T00:00:00 | /pre-black-friday.json, black-friday-global | 2023-12-15T00:00:00 | 2023-12-31T00:00:00 | /black-friday.json"> | ||
<meta name="americas_schedule" content="us-special-promo | 2023-11-01T00:00:00 | 2023-12-15T00:00:00 | /us-special-promo.json | us;ca"> | ||
<meta name="apac_schedule" content="kr-special-promo | 2023-11-01T00:00:00 | 2023-12-15T00:00:00 | /us-special-promo.json | kr"> | ||
<title>Document Title</title> | ||
<link rel="icon" href="data:,"> | ||
+ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to get these mappings from a json? I'm thinking about geo-expansion and the need to update the list of supported languages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i understand but what is the added value to separate it beside clarity for developers? Those lists are region mapping for promotion country, for now not used elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for this use case its probably better to keep it in code similar to the list of locales in scripts.js.
hopefully geoexpansion PR will include changes to both lists at the same time, worst case we can always update it later.
json will not make it much easier