@bcatalog/enhanced-ecommerce
Advanced tools
Comparing version 1.2.0 to 1.2.1
{ | ||
"name": "@bcatalog/enhanced-ecommerce", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "source": "index.js", |
431
umd/ecom.js
(() => { | ||
const $f57664131f4354de$export$7851e0b8935b3476 = | ||
(groupName, collapsed = false) => | ||
(v) => { | ||
if (collapsed) console.groupCollapsed(groupName); | ||
else console.group(groupName); | ||
console.log(v); | ||
console.groupEnd(); | ||
return v; | ||
}; | ||
const $f57664131f4354de$export$cc1de4045e855120 = (value) => value.toFixed(2); | ||
const $f57664131f4354de$export$a4f4bb6b1453fff5 = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key); | ||
const $f57664131f4354de$export$1cd13b593c4189ba = (key, arr) => { | ||
const $f57664131f4354de$export$7851e0b8935b3476 = (groupName, collapsed = false)=>(v)=>{ | ||
if (collapsed) console.groupCollapsed(groupName); | ||
else console.group(groupName); | ||
console.log(v); | ||
console.groupEnd(); | ||
return v; | ||
} | ||
; | ||
const $f57664131f4354de$export$cc1de4045e855120 = (value)=>value.toFixed(2) | ||
; | ||
const $f57664131f4354de$export$a4f4bb6b1453fff5 = (obj, key)=>Object.prototype.hasOwnProperty.call(obj, key) | ||
; | ||
const $f57664131f4354de$export$53b83ca8eaab0383 = (obj)=>typeof obj === 'object' && obj !== null | ||
; | ||
const $f57664131f4354de$export$fce6876652108ab = (obj)=>typeof obj === 'undefined' | ||
; | ||
const $f57664131f4354de$export$3988ae62b71be9a3 = (obj, path)=>{ | ||
let result = obj; | ||
for (const pathPiece of path){ | ||
if (!$f57664131f4354de$export$53b83ca8eaab0383(result)) return undefined; | ||
result = result[pathPiece]; | ||
} | ||
return result; | ||
}; | ||
const $f57664131f4354de$export$1cd13b593c4189ba = (key, arr)=>{ | ||
if (!Array.isArray(arr)) return null; | ||
return arr.reduce((acc, arrItem) => { | ||
acc[arrItem[key]] = arrItem; | ||
return acc; | ||
}, {}); | ||
}; | ||
const $f57664131f4354de$export$61fc7d43ac8f84b0 = function (func, wait = 0) { | ||
return arr.reduce((acc, arrItem)=>{ | ||
acc[arrItem[key]] = arrItem; | ||
return acc; | ||
}, { | ||
}); | ||
}; | ||
const $f57664131f4354de$export$61fc7d43ac8f84b0 = function(func, wait = 0) { | ||
let timer; | ||
const cbFn = function () { | ||
clearTimeout(timer); | ||
timer = setTimeout(func.bind(this, ...arguments), wait); | ||
const cbFn = function() { | ||
clearTimeout(timer); | ||
timer = setTimeout(func.bind(this, ...arguments), wait); | ||
}; | ||
cbFn.cancel = () => clearTimeout(timer); | ||
cbFn.cancel = ()=>clearTimeout(timer) | ||
; | ||
return cbFn; | ||
}; | ||
}; | ||
/** | ||
* | ||
* @param catalogProduct | ||
* @return {EcomProduct} | ||
*/ function $5b85ef0cee40e693$var$catalogProductToEcom(catalogProduct) { | ||
/** | ||
* | ||
* @param catalogProduct | ||
* @return {EcomProduct} | ||
*/ function $5b85ef0cee40e693$var$catalogProductToEcom(catalogProduct) { | ||
const ecomProduct = { | ||
id: catalogProduct.id, | ||
name: catalogProduct.title, | ||
category: catalogProduct?.category?.title, | ||
id: catalogProduct.id, | ||
name: catalogProduct.title, | ||
category: catalogProduct?.category?.title | ||
}; | ||
if ($f57664131f4354de$export$a4f4bb6b1453fff5(catalogProduct, 'price')) | ||
ecomProduct.price = $f57664131f4354de$export$cc1de4045e855120(catalogProduct.price); | ||
if ($f57664131f4354de$export$a4f4bb6b1453fff5(catalogProduct, 'price')) ecomProduct.price = $f57664131f4354de$export$cc1de4045e855120(catalogProduct.price); | ||
return ecomProduct; | ||
} | ||
/** | ||
* | ||
* @param {BCatalogCartPosition} position | ||
* @return {EcomProduct} | ||
*/ function $5b85ef0cee40e693$var$catalogPositionToEcom(position) { | ||
} | ||
/** | ||
* | ||
* @param {BCatalogCartPosition} position | ||
* @return {EcomProduct} | ||
*/ function $5b85ef0cee40e693$var$catalogPositionToEcom(position) { | ||
/** @type {EcomProduct} */ const ecomProduct = { | ||
id: position.product.id, | ||
price: $f57664131f4354de$export$cc1de4045e855120(position.price), | ||
quantity: position.amount, | ||
name: position.product.title, | ||
id: position.product.id, | ||
price: $f57664131f4354de$export$cc1de4045e855120(position.price), | ||
quantity: position.amount, | ||
name: position.product.title | ||
}; | ||
if (position.option) ecomProduct.variant = position.option.title; | ||
return ecomProduct; | ||
} | ||
class $5b85ef0cee40e693$export$a2f2547f010a5e2a { | ||
} | ||
class $5b85ef0cee40e693$export$a2f2547f010a5e2a { | ||
/** | ||
* @private | ||
* @param {*} dataLayer | ||
*/ constructor(dataLayer) { | ||
this.dataLayer = dataLayer; | ||
* @private | ||
* @param {*} dataLayer | ||
*/ constructor(dataLayer){ | ||
this.dataLayer = dataLayer; | ||
} | ||
static create(dataLayer) { | ||
return new this(dataLayer); | ||
return new this(dataLayer); | ||
} | ||
resetEcommerce() { | ||
this.dataLayer.push({ | ||
ecommerce: null, | ||
}); | ||
this.dataLayer.push({ | ||
ecommerce: null | ||
}); | ||
} | ||
createEvent(event, action, content) { | ||
return { | ||
event: event, | ||
ecommerce: { | ||
[action]: content, | ||
}, | ||
}; | ||
return { | ||
event: event, | ||
ecommerce: { | ||
[action]: content | ||
} | ||
}; | ||
} | ||
createAction(action, content) { | ||
return { | ||
ecommerce: { | ||
[action]: content, | ||
}, | ||
}; | ||
return { | ||
ecommerce: { | ||
[action]: content | ||
} | ||
}; | ||
} | ||
/** | ||
* | ||
* @param {Array<BCatalogCartPosition>} positions | ||
*/ addPositions(positions) { | ||
const { dataLayer: dataLayer } = this; | ||
this.resetEcommerce(); | ||
dataLayer.push( | ||
this.createEvent('addToCart', 'add', { | ||
products: positions.map($5b85ef0cee40e693$var$catalogPositionToEcom), | ||
}), | ||
); | ||
* | ||
* @param {Array<BCatalogCartPosition>} positions | ||
*/ addPositions(positions) { | ||
const { dataLayer: dataLayer } = this; | ||
this.resetEcommerce(); | ||
dataLayer.push(this.createEvent('addToCart', 'add', { | ||
products: positions.map($5b85ef0cee40e693$var$catalogPositionToEcom) | ||
})); | ||
} | ||
/** | ||
* | ||
* @param {Array<BCatalogCartPosition>} positions | ||
*/ dropPosition(positions) { | ||
const { dataLayer: dataLayer } = this; | ||
this.resetEcommerce(); | ||
dataLayer.push( | ||
this.createEvent('removeFromCart', 'remove', { | ||
products: positions.map($5b85ef0cee40e693$var$catalogPositionToEcom), | ||
}), | ||
); | ||
* | ||
* @param {Array<BCatalogCartPosition>} positions | ||
*/ dropPosition(positions) { | ||
const { dataLayer: dataLayer } = this; | ||
this.resetEcommerce(); | ||
dataLayer.push(this.createEvent('removeFromCart', 'remove', { | ||
products: positions.map($5b85ef0cee40e693$var$catalogPositionToEcom) | ||
})); | ||
} | ||
/** | ||
* | ||
* @param {?BCatalogCart} oldCart | ||
* @param {?BCatalogCart} newCart | ||
*/ updateCart({ oldCart: oldCart, newCart: newCart }) { | ||
/** @type {Object<number, BCatalogCartPosition>} */ const oldPositionsMap = | ||
(oldCart && $f57664131f4354de$export$1cd13b593c4189ba('id', oldCart.positions)) ?? {}; | ||
/** @type {Object<number, BCatalogCartPosition>} */ const newPositionsMap = | ||
(newCart && $f57664131f4354de$export$1cd13b593c4189ba('id', newCart.positions)) ?? {}; | ||
const fullDataPositions = Object.assign({}, oldPositionsMap, newPositionsMap); | ||
const toDeletePosIds = []; | ||
const toAddPosIds = []; | ||
// Ищем позиции, которые надо зафиксировать | ||
for (const posId in newPositionsMap) { | ||
if (!$f57664131f4354de$export$a4f4bb6b1453fff5(oldPositionsMap, posId)) { | ||
toAddPosIds.push(posId); | ||
break; | ||
* | ||
* @param {?BCatalogCart} oldCart | ||
* @param {?BCatalogCart} newCart | ||
*/ updateCart({ oldCart: oldCart , newCart: newCart }) { | ||
/** @type {Object<number, BCatalogCartPosition>} */ const oldPositionsMap = (oldCart && $f57664131f4354de$export$1cd13b593c4189ba('id', oldCart.positions)) ?? { | ||
}; | ||
/** @type {Object<number, BCatalogCartPosition>} */ const newPositionsMap = (newCart && $f57664131f4354de$export$1cd13b593c4189ba('id', newCart.positions)) ?? { | ||
}; | ||
const fullDataPositions = Object.assign({ | ||
}, oldPositionsMap, newPositionsMap); | ||
const toDeletePosIds = []; | ||
const toAddPosIds = []; | ||
// Ищем позиции, которые надо зафиксировать | ||
for(const posId in newPositionsMap){ | ||
if (!$f57664131f4354de$export$a4f4bb6b1453fff5(oldPositionsMap, posId)) { | ||
toAddPosIds.push(posId); | ||
break; | ||
} | ||
if (oldPositionsMap[posId].amount !== newPositionsMap[posId].amount) { | ||
toAddPosIds.push(posId); | ||
break; | ||
} | ||
} | ||
if (oldPositionsMap[posId].amount !== newPositionsMap[posId].amount) { | ||
toAddPosIds.push(posId); | ||
break; | ||
} | ||
} | ||
// Ищем позиции, которые надо пометить как удаленные | ||
for (const posId1 in oldPositionsMap) | ||
if (!$f57664131f4354de$export$a4f4bb6b1453fff5(newPositionsMap, posId1)) toDeletePosIds.push(posId1); | ||
if (toAddPosIds.length > 0) this.addPositions(toAddPosIds.map((i) => fullDataPositions[i])); | ||
if (toDeletePosIds.length > 0) this.dropPosition(toDeletePosIds.map((i) => fullDataPositions[i])); | ||
// Ищем позиции, которые надо пометить как удаленные | ||
for(const posId1 in oldPositionsMap)if (!$f57664131f4354de$export$a4f4bb6b1453fff5(newPositionsMap, posId1)) toDeletePosIds.push(posId1); | ||
if (toAddPosIds.length > 0) this.addPositions(toAddPosIds.map((i)=>fullDataPositions[i] | ||
)); | ||
if (toDeletePosIds.length > 0) this.dropPosition(toDeletePosIds.map((i)=>fullDataPositions[i] | ||
)); | ||
} | ||
/** | ||
* | ||
* @param {BCatalogProduct} product | ||
*/ clickProduct(product) { | ||
const { dataLayer: dataLayer } = this; | ||
this.resetEcommerce(); | ||
dataLayer.push( | ||
this.createEvent('productClick', 'click', { | ||
products: [$5b85ef0cee40e693$var$catalogProductToEcom(product)], | ||
}), | ||
); | ||
* | ||
* @param {BCatalogProduct} product | ||
*/ clickProduct(product) { | ||
const { dataLayer: dataLayer } = this; | ||
this.resetEcommerce(); | ||
dataLayer.push(this.createEvent('productClick', 'click', { | ||
products: [ | ||
$5b85ef0cee40e693$var$catalogProductToEcom(product) | ||
] | ||
})); | ||
} | ||
/** | ||
* | ||
* @param {BCatalogProduct} product | ||
*/ detailViewProduct(product) { | ||
const { dataLayer: dataLayer } = this; | ||
this.resetEcommerce(); | ||
dataLayer.push( | ||
this.createAction('detail', { | ||
products: [$5b85ef0cee40e693$var$catalogProductToEcom(product)], | ||
}), | ||
); | ||
* | ||
* @param {BCatalogProduct} product | ||
*/ detailViewProduct(product) { | ||
const { dataLayer: dataLayer } = this; | ||
this.resetEcommerce(); | ||
dataLayer.push(this.createAction('detail', { | ||
products: [ | ||
$5b85ef0cee40e693$var$catalogProductToEcom(product) | ||
] | ||
})); | ||
} | ||
/** | ||
* | ||
* @param {BCatalogOrder} order | ||
*/ purchase({ order: order }) { | ||
this.resetEcommerce(); | ||
dataLayer.push( | ||
this.createAction('purchase', { | ||
actionField: { | ||
id: order.id, | ||
shipping: order.total - order.subtotal, | ||
revenue: order.total, | ||
}, | ||
}), | ||
); | ||
* | ||
* @param {BCatalogOrder} order | ||
*/ purchase({ order: order }) { | ||
this.resetEcommerce(); | ||
const actionContent = { | ||
actionField: { | ||
id: order.id, | ||
shipping: order.total - order.subtotal, | ||
revenue: order.total | ||
} | ||
}; | ||
if (Array.isArray(order.items)) actionContent.products = order.items.map((item)=>({ | ||
id: item.product_id, | ||
price: item.price, | ||
quantity: item.amount, | ||
variant: $f57664131f4354de$export$3988ae62b71be9a3(item, [ | ||
'product_option', | ||
'title' | ||
]) | ||
}) | ||
); | ||
dataLayer.push(this.createAction('purchase', actionContent)); | ||
} | ||
productsAppears({ products: products }) { | ||
const { dataLayer: dataLayer } = this; | ||
this.resetEcommerce(); | ||
dataLayer.push( | ||
this.createAction('impressions', [ | ||
products.map((product, index) => ({ | ||
...$5b85ef0cee40e693$var$catalogProductToEcom(product), | ||
position: index + 1, | ||
})), | ||
]), | ||
); | ||
productsAppears({ products: products }) { | ||
const { dataLayer: dataLayer } = this; | ||
this.resetEcommerce(); | ||
dataLayer.push(this.createAction('impressions', [ | ||
products.map((product, index)=>({ | ||
...$5b85ef0cee40e693$var$catalogProductToEcom(product), | ||
position: index + 1 | ||
}) | ||
), | ||
])); | ||
} | ||
} | ||
} | ||
const $fe14511c13de6fc9$export$3482381851ec9578 = { | ||
const $fe14511c13de6fc9$export$3482381851ec9578 = { | ||
product: 'PRODUCT', | ||
@@ -202,67 +225,71 @@ cart: 'CART', | ||
customPage: 'CUSTOM_PAGE', | ||
search: 'SEARCH', | ||
}; | ||
search: 'SEARCH' | ||
}; | ||
function $1b6479369d7ad83c$export$310e73af8708799c({ dataLayer: dataLayer, bCatalog: bCatalog }) { | ||
function $1b6479369d7ad83c$export$310e73af8708799c({ dataLayer: dataLayer , bCatalog: bCatalog }) { | ||
const dataLayerShortcuts = $5b85ef0cee40e693$export$a2f2547f010a5e2a.create(dataLayer); | ||
function onPageMove(pageObj) { | ||
switch (pageObj.type) { | ||
case $fe14511c13de6fc9$export$3482381851ec9578.product: | ||
dataLayerShortcuts.clickProduct({ | ||
id: pageObj.params.productId, | ||
}); | ||
break; | ||
} | ||
switch(pageObj.type){ | ||
case $fe14511c13de6fc9$export$3482381851ec9578.product: | ||
dataLayerShortcuts.clickProduct({ | ||
id: pageObj.params.productId | ||
}); | ||
break; | ||
} | ||
} | ||
function onPageLoaded(pageObj) { | ||
switch (pageObj.type) { | ||
case $fe14511c13de6fc9$export$3482381851ec9578.product: | ||
dataLayerShortcuts.detailViewProduct(pageObj.params.product); | ||
break; | ||
} | ||
switch(pageObj.type){ | ||
case $fe14511c13de6fc9$export$3482381851ec9578.product: | ||
dataLayerShortcuts.detailViewProduct(pageObj.params.product); | ||
break; | ||
} | ||
} | ||
bCatalog.events.onPageSwitch(onPageMove); | ||
bCatalog.events.onPageLoaded(onPageLoaded); | ||
let cartActionSource = null; | ||
bCatalog.events.appEvents('cart:actionSource', ({ actionSource: actionSource })=>{ | ||
cartActionSource = actionSource; | ||
}); | ||
/** @type {?BCatalogCart} */ let currentCart; | ||
bCatalog.events.onCartChanged( | ||
/** @param {?BCatalogCart} updatedCart */ (updatedCart) => { | ||
dataLayerShortcuts.updateCart({ | ||
newCart: updatedCart, | ||
oldCart: currentCart, | ||
bCatalog.events.onCartChanged(/** @param {?BCatalogCart} updatedCart */ (updatedCart)=>{ | ||
if (cartActionSource === 'user') dataLayerShortcuts.updateCart({ | ||
newCart: updatedCart, | ||
oldCart: currentCart | ||
}); | ||
currentCart = updatedCart; | ||
}, | ||
}); | ||
bCatalog.events.appEvents('category:productAppears', ({ products: products })=>dataLayerShortcuts.productsAppears({ | ||
products: products | ||
}) | ||
); | ||
bCatalog.events.appEvents('category:productAppears', ({ products: products }) => | ||
dataLayerShortcuts.productsAppears({ | ||
products: products, | ||
}), | ||
bCatalog.events.appEvents('order:created', ({ order: order })=>dataLayerShortcuts.purchase({ | ||
order: order | ||
}) | ||
); | ||
bCatalog.events.appEvents('order:created', ({ order: order }) => | ||
dataLayerShortcuts.purchase({ | ||
order: order, | ||
}), | ||
); | ||
} | ||
/** | ||
* | ||
* @param {*} dataLayer | ||
* @param {BCatalogType} bCatalog | ||
* @returns | ||
*/ function $1b6479369d7ad83c$var$init(dataLayer, bCatalog) { | ||
} | ||
/** | ||
* | ||
* @param {*} dataLayer | ||
* @param {BCatalogType} bCatalog | ||
* @returns | ||
*/ function $1b6479369d7ad83c$var$init(dataLayer, bCatalog) { | ||
if (!dataLayer) { | ||
console.error(`Can't find dataLayer`); | ||
return; | ||
console.error(`Can't find dataLayer`); | ||
return; | ||
} | ||
if (!bCatalog) { | ||
console.error(`Can't find BCatalog storefront`); | ||
return; | ||
console.error(`Can't find BCatalog storefront`); | ||
return; | ||
} | ||
$1b6479369d7ad83c$export$310e73af8708799c({ | ||
bCatalog: bCatalog, | ||
dataLayer: dataLayer, | ||
bCatalog: bCatalog, | ||
dataLayer: dataLayer | ||
}); | ||
} | ||
$1b6479369d7ad83c$var$init(window.dataLayer, window.BCatalog); | ||
if (null) null.accept(); | ||
} | ||
$1b6479369d7ad83c$var$init(window.dataLayer, window.BCatalog); | ||
if (null) null.accept(); | ||
})(); |
@@ -1,100 +0,1 @@ | ||
(() => { | ||
const t = (t) => t.toFixed(2), | ||
e = (t, e) => Object.prototype.hasOwnProperty.call(t, e), | ||
r = (t, e) => (Array.isArray(e) ? e.reduce((e, r) => ((e[r[t]] = r), e), {}) : null); | ||
function a(r) { | ||
const a = { id: r.id, name: r.title, category: r?.category?.title }; | ||
return e(r, 'price') && (a.price = t(r.price)), a; | ||
} | ||
function o(e) { | ||
const r = { id: e.product.id, price: t(e.price), quantity: e.amount, name: e.product.title }; | ||
return e.option && (r.variant = e.option.title), r; | ||
} | ||
class c { | ||
constructor(t) { | ||
this.dataLayer = t; | ||
} | ||
static create(t) { | ||
return new this(t); | ||
} | ||
resetEcommerce() { | ||
this.dataLayer.push({ ecommerce: null }); | ||
} | ||
createEvent(t, e, r) { | ||
return { event: t, ecommerce: { [e]: r } }; | ||
} | ||
createAction(t, e) { | ||
return { ecommerce: { [t]: e } }; | ||
} | ||
addPositions(t) { | ||
const { dataLayer: e } = this; | ||
this.resetEcommerce(), e.push(this.createEvent('addToCart', 'add', { products: t.map(o) })); | ||
} | ||
dropPosition(t) { | ||
const { dataLayer: e } = this; | ||
this.resetEcommerce(), e.push(this.createEvent('removeFromCart', 'remove', { products: t.map(o) })); | ||
} | ||
updateCart({ oldCart: t, newCart: a }) { | ||
const o = (t && r('id', t.positions)) ?? {}, | ||
c = (a && r('id', a.positions)) ?? {}, | ||
s = Object.assign({}, o, c), | ||
i = [], | ||
n = []; | ||
for (const t in c) { | ||
if (!e(o, t)) { | ||
n.push(t); | ||
break; | ||
} | ||
if (o[t].amount !== c[t].amount) { | ||
n.push(t); | ||
break; | ||
} | ||
} | ||
for (const t in o) e(c, t) || i.push(t); | ||
n.length > 0 && this.addPositions(n.map((t) => s[t])), i.length > 0 && this.dropPosition(i.map((t) => s[t])); | ||
} | ||
clickProduct(t) { | ||
const { dataLayer: e } = this; | ||
this.resetEcommerce(), e.push(this.createEvent('productClick', 'click', { products: [a(t)] })); | ||
} | ||
detailViewProduct(t) { | ||
const { dataLayer: e } = this; | ||
this.resetEcommerce(), e.push(this.createAction('detail', { products: [a(t)] })); | ||
} | ||
purchase({ order: t }) { | ||
this.resetEcommerce(), | ||
dataLayer.push( | ||
this.createAction('purchase', { | ||
actionField: { id: t.id, shipping: t.total - t.subtotal, revenue: t.total }, | ||
}), | ||
); | ||
} | ||
productsAppears({ products: t }) { | ||
const { dataLayer: e } = this; | ||
this.resetEcommerce(), | ||
e.push(this.createAction('impressions', [t.map((t, e) => ({ ...a(t), position: e + 1 }))])); | ||
} | ||
} | ||
const s = 'PRODUCT'; | ||
!(function (t, e) { | ||
t | ||
? e | ||
? (function ({ dataLayer: t, bCatalog: e }) { | ||
const r = c.create(t); | ||
let a; | ||
e.events.onPageSwitch(function (t) { | ||
t.type === s && r.clickProduct({ id: t.params.productId }); | ||
}), | ||
e.events.onPageLoaded(function (t) { | ||
t.type === s && r.detailViewProduct(t.params.product); | ||
}), | ||
e.events.onCartChanged((t) => { | ||
r.updateCart({ newCart: t, oldCart: a }), (a = t); | ||
}), | ||
e.events.appEvents('category:productAppears', ({ products: t }) => r.productsAppears({ products: t })), | ||
e.events.appEvents('order:created', ({ order: t }) => r.purchase({ order: t })); | ||
})({ bCatalog: e, dataLayer: t }) | ||
: console.error("Can't find BCatalog storefront") | ||
: console.error("Can't find dataLayer"); | ||
})(window.dataLayer, window.BCatalog); | ||
})(); | ||
(()=>{const t=t=>t.toFixed(2),e=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r=t=>"object"==typeof t&&null!==t,o=(t,e)=>{let o=t;for(const t of e){if(!r(o))return;o=o[t]}return o},a=(t,e)=>Array.isArray(e)?e.reduce(((e,r)=>(e[r[t]]=r,e)),{}):null;function c(r){const o={id:r.id,name:r.title,category:r?.category?.title};return e(r,"price")&&(o.price=t(r.price)),o}function s(e){const r={id:e.product.id,price:t(e.price),quantity:e.amount,name:e.product.title};return e.option&&(r.variant=e.option.title),r}class i{constructor(t){this.dataLayer=t}static create(t){return new this(t)}resetEcommerce(){this.dataLayer.push({ecommerce:null})}createEvent(t,e,r){return{event:t,ecommerce:{[e]:r}}}createAction(t,e){return{ecommerce:{[t]:e}}}addPositions(t){const{dataLayer:e}=this;this.resetEcommerce(),e.push(this.createEvent("addToCart","add",{products:t.map(s)}))}dropPosition(t){const{dataLayer:e}=this;this.resetEcommerce(),e.push(this.createEvent("removeFromCart","remove",{products:t.map(s)}))}updateCart({oldCart:t,newCart:r}){const o=(t&&a("id",t.positions))??{},c=(r&&a("id",r.positions))??{},s=Object.assign({},o,c),i=[],n=[];for(const t in c){if(!e(o,t)){n.push(t);break}if(o[t].amount!==c[t].amount){n.push(t);break}}for(const t in o)e(c,t)||i.push(t);n.length>0&&this.addPositions(n.map((t=>s[t]))),i.length>0&&this.dropPosition(i.map((t=>s[t])))}clickProduct(t){const{dataLayer:e}=this;this.resetEcommerce(),e.push(this.createEvent("productClick","click",{products:[c(t)]}))}detailViewProduct(t){const{dataLayer:e}=this;this.resetEcommerce(),e.push(this.createAction("detail",{products:[c(t)]}))}purchase({order:t}){this.resetEcommerce();const e={actionField:{id:t.id,shipping:t.total-t.subtotal,revenue:t.total}};Array.isArray(t.items)&&(e.products=t.items.map((t=>({id:t.product_id,price:t.price,quantity:t.amount,variant:o(t,["product_option","title"])})))),dataLayer.push(this.createAction("purchase",e))}productsAppears({products:t}){const{dataLayer:e}=this;this.resetEcommerce(),e.push(this.createAction("impressions",[t.map(((t,e)=>({...c(t),position:e+1})))]))}}const n="PRODUCT";!function(t,e){t?e?function({dataLayer:t,bCatalog:e}){const r=i.create(t);e.events.onPageSwitch((function(t){t.type===n&&r.clickProduct({id:t.params.productId})})),e.events.onPageLoaded((function(t){t.type===n&&r.detailViewProduct(t.params.product)}));let o,a=null;e.events.appEvents("cart:actionSource",(({actionSource:t})=>{a=t})),e.events.onCartChanged((t=>{"user"===a&&r.updateCart({newCart:t,oldCart:o}),o=t})),e.events.appEvents("category:productAppears",(({products:t})=>r.productsAppears({products:t}))),e.events.appEvents("order:created",(({order:t})=>r.purchase({order:t})))}({bCatalog:e,dataLayer:t}):console.error("Can't find BCatalog storefront"):console.error("Can't find dataLayer")}(window.dataLayer,window.BCatalog)})(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13478
284