From 963b272df81d76b330af66aabbda133ff0da3e55 Mon Sep 17 00:00:00 2001 From: Michal-Dziedzinski Date: Wed, 19 Feb 2020 11:45:07 +0100 Subject: [PATCH] Fix gtm order placement event when user was guest --- CHANGELOG.md | 1 + core/helpers/index.ts | 2 +- core/modules/user/store/actions.ts | 2 +- .../hooks/afterRegistration.ts | 34 +++++++++---------- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e08b491fa..65b3477b01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Set `totals` in products in cart always in reactive way - @psmyrek (#4079) - Fix sync cart between tabs - @Michal-Dziedzinski (#3838) - Add currentRoute to url module and return cached requests - @gibkigonzo (pr#4077, issue#4066) +- Fix gtm order placement event when user was guest - @Michal-Dziedzinski (#4064) ## [1.11.1] - 2020.02.05 diff --git a/core/helpers/index.ts b/core/helpers/index.ts index 1addece6b0..1d69132b2f 100644 --- a/core/helpers/index.ts +++ b/core/helpers/index.ts @@ -218,7 +218,7 @@ export const serial = async promises => { return results } -// helper to calcuate the hash of the shopping cart +// helper to calculate the hash of the shopping cart export const calcItemsHmac = (items, token) => { return sha3_224(JSON.stringify({ items, token: token })) } diff --git a/core/modules/user/store/actions.ts b/core/modules/user/store/actions.ts index fa2b3607e7..a29b887d84 100644 --- a/core/modules/user/store/actions.ts +++ b/core/modules/user/store/actions.ts @@ -269,7 +269,7 @@ const actions: ActionTree = { */ async getOrdersHistory ({ dispatch, getters }, { refresh = true, useCache = true, pageSize = 20, currentPage = 1 }) { if (!getters.getToken) { - Logger.debug('No User token, user unathorized', 'user')() + Logger.debug('No User token, user unauthorized', 'user')() return Promise.resolve(null) } let resolvedFromCache = false diff --git a/src/modules/google-tag-manager/hooks/afterRegistration.ts b/src/modules/google-tag-manager/hooks/afterRegistration.ts index a75e436e6a..97e31997e0 100644 --- a/src/modules/google-tag-manager/hooks/afterRegistration.ts +++ b/src/modules/google-tag-manager/hooks/afterRegistration.ts @@ -79,7 +79,7 @@ export function afterRegistration (config, store: Store) { } // Measuring Purchases - if (type === 'order/order/LAST_ORDER_CONFIRMATION') { + if (type === 'order/orders/LAST_ORDER_CONFIRMATION') { const orderId = payload.confirmation.backendOrderId const products = payload.order.products.map(product => getProduct(product)) store.dispatch( @@ -87,24 +87,22 @@ export function afterRegistration (config, store: Store) { { refresh: true, useCache: false } ).then(() => { const orderHistory = state.user.orders_history - const order = orderHistory.items.find((order) => order['entity_id'].toString() === orderId) - if (order) { - GTM.trackEvent({ - 'ecommerce': { - 'purchase': { - 'actionField': { - 'id': orderId, - 'affiliation': order.store_name, - 'revenue': order.total_due, - 'tax': order.tax_amount, - 'shipping': order.shipping_amount, - 'coupon': '' - }, - 'products': products - } + const order = state.user.orders_history ? orderHistory.items.find((order) => order['entity_id'].toString() === orderId) : null + GTM.trackEvent({ + 'ecommerce': { + 'purchase': { + 'actionField': { + 'id': orderId, + 'affiliation': order ? order.store_name : '', + 'revenue': order ? order.total_due : state.cart.platformTotals && state.cart.platformTotals.base_grand_total ? state.cart.platformTotals.base_grand_total : '', + 'tax': order ? order.total_due : state.cart.platformTotals && state.cart.platformTotals.base_shipping_amount ? state.cart.platformTotals.base_shipping_amount : '', + 'shipping': order ? order.total_due : state.cart.platformTotals && state.cart.platformTotals.base_tax_amount ? state.cart.platformTotals.base_tax_amount : '', + 'coupon': '' + }, + 'products': products } - }) - } + } + }) }) } })