From 620d3a423a173d931c2bf8e57d10cf84d9c48684 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 7 May 2024 18:54:29 +0200 Subject: [PATCH] wip #6889 @3 --- .../cart/fluidbook.cart.bastide-resah.js | 57 ++++++++++++++----- js/libs/fluidbook/fluidbook.stats.js | 45 ++++++++++++++- 2 files changed, 86 insertions(+), 16 deletions(-) diff --git a/js/libs/fluidbook/cart/fluidbook.cart.bastide-resah.js b/js/libs/fluidbook/cart/fluidbook.cart.bastide-resah.js index c971bb8c..677f9d1b 100644 --- a/js/libs/fluidbook/cart/fluidbook.cart.bastide-resah.js +++ b/js/libs/fluidbook/cart/fluidbook.cart.bastide-resah.js @@ -3,6 +3,8 @@ function FluidbookCartBastideResah(cart) { this.fluidbook = this.cart.fluidbook; this.data = this.fluidbook.settings.basketReferences; this.form_endpoint = this.fluidbook.service.getBaseURL() + 'bastide'; // Where cart form is processed + this.matomo_items = []; + this.matomo_total = 0; this.init(); } @@ -31,23 +33,28 @@ FluidbookCartBastideResah.prototype = { $(document).on(this.fluidbook.input.clickEvent, '#Bastide_cart [data-validate-cart]', function (event) { event.preventDefault(); + $this.updateCart(); + $.ajax('/fluidbook/order', { - method: 'POST', - data: {details: $this.getItems()}, + method: 'POST', data: {details: $this.getItems()}, }).done(function (data) { - $this.fluidbook.stats.trackEcommerceOrder(data.order, data.total); + $this.fluidbook.stats.trackEcommerceOrder($this.matomo_items, data.order, data.total, data.subTotal, data.taxes); }); $this.fluidbook.menu.quickCloseView(); - $this.openModal("Merci !", '
Vous recevrez un devis ainsi que les conditions générales de vente de la part du RESAH à l’adresse mail que vous avez mentionnée dans un délai d’environ 72h (pensez à vérifier votre dossier spam).

' + - 'La validation de ce devis auprès du RESAH nécessitera l’établissement et l’envoi au RESAH d’un bon de commande valant pour acceptation pleine et entière.

' + - 'Nos équipes commerciales se tiennent à votre disposition : resah@bastide-medical.fr
', function () { - + $this.openModal("Merci !", '
Vous recevrez un devis ainsi que les conditions générales de vente de la part du RESAH à l’adresse mail que vous avez mentionnée dans un délai d’environ 72h (pensez à vérifier votre dossier spam).

' + 'La validation de ce devis auprès du RESAH nécessitera l’établissement et l’envoi au RESAH d’un bon de commande valant pour acceptation pleine et entière.

' + 'Nos équipes commerciales se tiennent à votre disposition : resah@bastide-medical.fr
', function () { }); - }); }, + getCartItemsForMatomo: function () { + return this.matomo_items; + }, + + getCartTotalForMatomo: function () { + return this.matomo_total; + }, + getCartItemIndex: function (reference) { let cartItems = this.getItems(); @@ -80,6 +87,7 @@ FluidbookCartBastideResah.prototype = { return true; }, + removeFromCart: function (index) { if (index >= 0) { this.items.splice(index, 1); @@ -90,6 +98,10 @@ FluidbookCartBastideResah.prototype = { save: function () { this.fluidbook.cache.set('cart', this.getItems()); this.updateIcon(); + if (this.fluidbook.stats.isMatomoEnabled()) { + this.updateCart(); + this.fluidbook.stats.matomoCartUpdate(this.matomo_items, this.matomo_total); + } //this.fluidbook.cart.updateLinks(); }, @@ -114,8 +126,9 @@ FluidbookCartBastideResah.prototype = { }, updateCart: function () { + let content = this.getCartContent(); if ($('#Bastide_cart').length > 0) { - $('#Bastide_cart .content').html(this.getCartContent()); + $('#Bastide_cart .content').html(content); this.fluidbook.resize.resize(); } }, @@ -177,7 +190,15 @@ FluidbookCartBastideResah.prototype = { return $('.cart-sent').length > 0 ? 440 : 1385; }, + emptyCart: function () { + this.items = []; + this.save(); + }, + getCartContent: function () { + this.matomo_items = []; + this.matomo_total = 0; + if (this.getItemCount() === 0) { return `
${this.fluidbook.l10n.__('your cart is empty')}
`; } @@ -202,6 +223,7 @@ FluidbookCartBastideResah.prototype = { `; + $.each(this.getItems(), function (index, item) { content += ''; @@ -211,6 +233,8 @@ FluidbookCartBastideResah.prototype = { let pvht = $this.parseFloat($this.data[item.reference]['PV RESAH HT']); let tht = q * (pvht + et); + let matomo_item = {categories: [], price: pvht, quantity: q, reference: item.reference}; + total_ht += tht; eco_taxe += item.quantity * et; tva += $this.parseFloat($this.data[item.reference]['TVA']) * tht * 0.01; @@ -257,6 +281,10 @@ FluidbookCartBastideResah.prototype = { ${getSpriteIcon('interface-close')} `; break; + case 'DESIGNATION': + output = value; + matomo_item.name = value; + break; default: output = value; } @@ -266,15 +294,20 @@ FluidbookCartBastideResah.prototype = { content += `${output}`; }); + + $this.matomo_items.push(matomo_item); + content += ''; }); + total_ttc = tva + total_ht; + this.matomo_total = total_ht; + content += ''; content += ''; - content += ''; content += '
'; @@ -299,9 +332,7 @@ FluidbookCartBastideResah.prototype = { content += '
' - content += ''; + content += ''; content += ''; return content; diff --git a/js/libs/fluidbook/fluidbook.stats.js b/js/libs/fluidbook/fluidbook.stats.js index a9bdfa37..63479344 100644 --- a/js/libs/fluidbook/fluidbook.stats.js +++ b/js/libs/fluidbook/fluidbook.stats.js @@ -1,6 +1,7 @@ function FluidbookStats() { this.hasMatomoTagManager = this.setupMatomoTagManager(); this.pixel_pv_id; + this.matomo_stack = []; this.setupMatomo(); } @@ -148,7 +149,25 @@ FluidbookStats.prototype = { break; case 15:// this.matomoPush(['trackEvent', 'cart', 'addproduct', extra]); - this.matomoPush(['setEcommerceView', extra]); + break; + } + }, + + matomoCartUpdate: function (items, total) { + this.matomoSetCartItems(items); + _paq.push(['trackEcommerceCartUpdate', total]); + }, + + matomoSetCartItems: function (items) { + for (var i in items) { + let item = items[i]; + this.matomoPush(['addEcommerceItem', + item.reference, // (Required) productSKU + item.name, // (Optional) productName + item.categories, // (Optional) productCategory + item.price, // (Recommended) price + item.quantity + ]); } }, @@ -210,10 +229,29 @@ FluidbookStats.prototype = { }, matomoPush(data) { + + let stack = data[0] === 'addEcommerceItem'; + if (stack) { + this.matomo_stack.push(data); + return; + } + for (let i in this.matomoServers) { let dataCopy = data.slice(0); + if (this.matomo_stack.length > 0) { + for (let j in this.matomo_stack) { + let copy = this.matomo_stack[j].slice(0); + this.matomoPushToServer(copy, this.matomoServers[i]); + } + } this.matomoPushToServer(dataCopy, this.matomoServers[i]); } + + this.matomo_stack = []; + }, + + isMatomoEnabled: function () { + return this.matomoServers.length > 0; }, setup: function (fluidbook) { @@ -458,8 +496,9 @@ FluidbookStats.prototype = { } }, - trackEcommerceOrder: function (orderId, total) { - this.matomoPush(['trackEcommerceOrder', orderId, total]); + trackEcommerceOrder: function (items, orderId, total, subTotal, taxes) { + this.matomoSetCartItems(items); + this.matomoPush(['trackEcommerceOrder', orderId, total, subTotal, taxes]); }, trackGoogleAnalytics: function (type, page, extra, url) { -- 2.39.5