From 4eaa8f937dd7e9c4c84426d10ae8e6535dc65d73 Mon Sep 17 00:00:00 2001 From: indykoning <15870933+indykoning@users.noreply.github.com> Date: Tue, 11 Jun 2024 14:13:23 +0200 Subject: [PATCH] Get cart items from graphql definition (#27) --- resources/js/datalayer/ga4.js | 40 +++++++++++++++++------------------ resources/js/datalayer/ua.js | 8 +++---- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/resources/js/datalayer/ga4.js b/resources/js/datalayer/ga4.js index 755fd3c..cea5058 100644 --- a/resources/js/datalayer/ga4.js +++ b/resources/js/datalayer/ga4.js @@ -144,13 +144,13 @@ export const viewCart = async () => { event: 'view_cart', ecommerce: { currency: window.config.currency, - value: removeTrailingZeros(window.app.cart.total), + value: removeTrailingZeros(window.app.cart?.prices?.grand_total?.value), items: Object.values(window.app.cart.items).map(function (item) { return { - item_name: item.name, - item_id: item.sku, - price: item.price, - quantity: item.qty, + item_name: item?.product?.name, + item_id: item?.product?.sku, + price: item?.prices?.price_including_tax?.value, + quantity: item?.quantity, } }), } @@ -164,13 +164,13 @@ export const beginCheckout = async (step) => { event: 'begin_checkout', ecommerce: { currency: window.config.currency, - value: removeTrailingZeros(window.app.cart.total), + value: removeTrailingZeros(window.app.cart?.prices?.grand_total?.value), items: Object.values(window.app.cart.items).map(function (item) { return { - item_name: item.name, - item_id: item.sku, - price: item.price, - quantity: item.qty, + item_name: item?.product?.name, + item_id: item?.product?.sku, + price: item?.prices?.price_including_tax?.value, + quantity: item?.quantity, } }), } @@ -184,14 +184,14 @@ export const addShippingInfo = async () => { event: 'add_shipping_info', ecommerce: { currency: window.config.currency, - value: removeTrailingZeros(window.app.cart.total), + value: removeTrailingZeros(window.app.cart?.prices?.grand_total?.value), shipping_tier: window.app.checkout.shipping_method, items: Object.values(window.app.cart.items).map(function (item) { return { - item_name: item.name, - item_id: item.sku, - price: item.price, - quantity: item.qty, + item_name: item?.product?.name, + item_id: item?.product?.sku, + price: item?.prices?.price_including_tax?.value, + quantity: item?.quantity, } }), } @@ -205,14 +205,14 @@ export const addPaymentInfo = async () => { event: 'add_payment_info', ecommerce: { currency: window.config.currency, - value: removeTrailingZeros(window.app.cart.total), + value: removeTrailingZeros(window.app.cart?.prices?.grand_total?.value), payment_type: window.app.checkout.payment_method, items: Object.values(window.app.cart.items).map(function (item) { return { - item_name: item.name, - item_id: item.sku, - price: item.price, - quantity: item.qty, + item_name: item?.product?.name, + item_id: item?.product?.sku, + price: item?.prices?.price_including_tax?.value, + quantity: item?.quantity, } }), } diff --git a/resources/js/datalayer/ua.js b/resources/js/datalayer/ua.js index ce9f96f..8a7b24b 100644 --- a/resources/js/datalayer/ua.js +++ b/resources/js/datalayer/ua.js @@ -63,10 +63,10 @@ export const checkoutStep = async (step) => { 'actionField': {'step': step}, 'products': Object.values(window.app.cart.items).map(function (item) { return { - name: item.name, - id: item.product_id, - price: item.price, - quantity: item.qty, + name: item?.product?.name, + id: item?.product?.sku, + price: item?.prices?.price_including_tax?.value, + quantity: item?.quantity, } }), }