|
// @flow |
|
/* eslint array-callback-return: "error" */ |
|
/* eslint block-scoped-var: "error" */ |
|
/* eslint no-implied-eval: "error" */ |
|
/* eslint comma-style: ["error", "first"] */ |
|
/* eslint max-len: ["error", 191] */ |
|
/* eslint max-params: ["error", 3] */ |
|
/* eslint-env es6 */ |
|
/* eslint newline-per-chained-call: ["error", { "ignoreChainWithDepth": 1 }] */ |
|
/* eslint operator-linebreak: [2, "before"] */ |
|
|
|
if (window.pageMeta_PageType === 'ProductDetail') { |
|
const getopStikyId = () => { |
|
let opgumCookie = false |
|
document.cookie.replace(/.*op1717homepagecookietakeovergum=([^; ]*).*/, (match, p1) => { |
|
opgumCookie = p1 |
|
}) |
|
return opgumCookie |
|
} |
|
|
|
const fromSimilarItems = () => { |
|
const categoryTrim = category => category |
|
.replace(/.*\W?(Womens).*/, '$1') |
|
.replace(/.*\W?(Mens).*/, '$1') |
|
|
|
const byRecognisedCategory = (pool, cat) => { |
|
console.log(cat) |
|
const segment = /Mens|Womens/i.test(cat) ? categoryTrim(cat) : 'Misc' |
|
console.log(segment) |
|
pool[segment] = (pool[segment] || 0) + 1 |
|
return pool |
|
} |
|
|
|
return [...document.querySelectorAll('div#MoreFromLinks ul li')] |
|
.map(li => li.innerText) |
|
.reduce(byRecognisedCategory, {}) |
|
} |
|
|
|
const findMainSegment = segments => { |
|
let mainCategory = 'Misc' |
|
if (segments.Mens) { |
|
if (segments.Womens && segments.Mens < segments.Womens) { |
|
mainCategory = 'Womens' |
|
} |
|
mainCategory = 'Mens' |
|
} else if (segments.Womens) { |
|
mainCategory = 'Womens' |
|
} |
|
return mainCategory |
|
} |
|
|
|
const pageData = Object.assign( |
|
{} |
|
, window.dataLayer |
|
.filter(({event}) => event === 'USC_onLoad') |
|
.reduce((x, {productId}) => ({ |
|
productId |
|
}), {}) |
|
, { |
|
'timestamp': Date.now() |
|
, 'stickyId': getopStikyId() |
|
, 'category': findMainSegment(fromSimilarItems()) |
|
} |
|
) |
|
|
|
const refreshOpHistory = () => { |
|
const oldOpHistory = JSON.parse(window.localStorage.getItem('opHistory') || '[]') |
|
const opStikyId = getopStikyId() |
|
return oldOpHistory.filter(({stickyId}) => stickyId === opStikyId) |
|
} |
|
|
|
const updateOpHistory = pageData => { |
|
const opHistory = refreshOpHistory() |
|
if (/Mens|Womens/i.test(pageData.category)) { |
|
if (3 <= opHistory.length) { |
|
opHistory.shift() |
|
} |
|
opHistory.push(pageData) |
|
} |
|
return opHistory |
|
} |
|
|
|
const newOpHistory = updateOpHistory(pageData) |
|
window.localStorage.setItem('opHistory', JSON.stringify(newOpHistory)) |
|
|
|
const decidePersona = (newOpHistory = []) => { |
|
const byCategory = (pool, cat) => { |
|
pool[cat] = (pool[cat] || 0) + 1 |
|
return pool |
|
} |
|
const fromNewOpHistory = () => newOpHistory |
|
.map(({category}) => category) |
|
.reduce(byCategory, {}) |
|
|
|
let persona = 'Misc' |
|
if (3 <= newOpHistory.length) { |
|
persona = findMainSegment(fromNewOpHistory()) |
|
} |
|
return persona |
|
} |
|
const opPersona = decidePersona(newOpHistory) |
|
const _90days = 60 * 60 * 24 * 90 |
|
window.localStorage.setItem('opPersona', opPersona || 'Misc') |
|
document.cookie = `opHomepageTakeover=${opPersona};max-age=${_90days};path=/` |
|
} else { |
|
const _90days = 60 * 60 * 24 * 90 |
|
const opPersona = window.localStorage.getItem('opPersona') || 'misc' |
|
document.cookie = `opHomepageTakeover=${opPersona};max-age=${_90days};path=/` |
|
} |