From 6a90e94c62f5e300fe970ac5d6b64c8c0e52eb40 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Sun, 13 Sep 2020 11:52:36 +0200 Subject: [PATCH] fix(medusa-plugin-segment): Track additional order details --- .../src/services/segment.js | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/packages/medusa-plugin-segment/src/services/segment.js b/packages/medusa-plugin-segment/src/services/segment.js index 82994fb06b..39f356d6cc 100644 --- a/packages/medusa-plugin-segment/src/services/segment.js +++ b/packages/medusa-plugin-segment/src/services/segment.js @@ -10,7 +10,7 @@ class SegmentService extends BaseService { * write_key: Segment write key given in Segment dashboard * } */ - constructor({totalsService}, options) { + constructor({ totalsService }, options) { super() this.options_ = options @@ -40,7 +40,9 @@ class SegmentService extends BaseService { if (fromCurrency === toCurrency) return value.toFixed(2) const exchangeRate = await axios - .get(`https://api.exchangeratesapi.io/${date}?symbols=${fromCurrency}&base=${toCurrency}`) + .get( + `https://api.exchangeratesapi.io/${date}?symbols=${fromCurrency}&base=${toCurrency}` + ) .then(({ data }) => { return data.rates[fromCurrency] }) @@ -48,7 +50,6 @@ class SegmentService extends BaseService { return (value / exchangeRate).toFixed(2) } - async buildOrder(order) { const subtotal = await this.totalsService_.getSubtotal(order) const total = await this.totalsService_.getTotal(order) @@ -66,12 +67,29 @@ class SegmentService extends BaseService { checkout_id: order.cart_id, order_id: order._id, email: order.email, + region_id: order.region_id, + payment_provider: order.payment_method.provider_id, + shipping_methods: order.shipping_methods, + shipping_country: order.shipping_address.country_code, + shipping_city: order.shipping_address.city, reporting_total: await this.getReportingValue(order.currency_code, total), - reporting_subtotal: await this.getReportingValue(order.currency_code, subtotal), - reporting_revenue: await this.getReportingValue(order.currency_code, revenue), - reporting_shipping: await this.getReportingValue(order.currency_code, shipping), + reporting_subtotal: await this.getReportingValue( + order.currency_code, + subtotal + ), + reporting_revenue: await this.getReportingValue( + order.currency_code, + revenue + ), + reporting_shipping: await this.getReportingValue( + order.currency_code, + shipping + ), reporting_tax: await this.getReportingValue(order.currency_code, tax), - reporting_discount: await this.getReportingValue(order.currency_code, discount), + reporting_discount: await this.getReportingValue( + order.currency_code, + discount + ), total, subtotal, revenue, @@ -81,13 +99,16 @@ class SegmentService extends BaseService { coupon, currency: order.currency_code, products: await Promise.all( - order.items.map(async item => { + order.items.map(async (item) => { let name = item.title let variant = item.description const unit_price = item.content.unit_price const line_total = unit_price * item.content.quantity * item.quantity - const revenue = await this.getReportingValue(order.currency_code, line_total) + const revenue = await this.getReportingValue( + order.currency_code, + line_total + ) return { name, variant, @@ -101,7 +122,6 @@ class SegmentService extends BaseService { ), } - return orderData } }