feat(medusa): cart context (#201)
- Adds a context field to Cart - context is automatically populated with ip + user agent - context can be updated via POST /store/cart/:id or set when creating via POST /store/cart
This commit is contained in:
@@ -3,15 +3,19 @@ class OrderSubscriber {
|
||||
segmentService,
|
||||
eventBusService,
|
||||
orderService,
|
||||
cartService,
|
||||
claimService,
|
||||
returnService,
|
||||
fulfillmentService,
|
||||
}) {
|
||||
this.orderService_ = orderService
|
||||
|
||||
this.cartService_ = cartService
|
||||
|
||||
this.returnService_ = returnService
|
||||
|
||||
this.claimService_ = claimService
|
||||
|
||||
this.fulfillmentService_ = fulfillmentService
|
||||
|
||||
eventBusService.subscribe(
|
||||
@@ -239,12 +243,46 @@ class OrderSubscriber {
|
||||
],
|
||||
})
|
||||
|
||||
const eventContext = {}
|
||||
const integrations = {}
|
||||
|
||||
if (order.cart_id) {
|
||||
try {
|
||||
const cart = await this.cartService_.retrieve(order.cart_id, {
|
||||
select: ["context"],
|
||||
})
|
||||
|
||||
if (cart.context) {
|
||||
if (cart.context.ip) {
|
||||
eventContext.ip = cart.context.ip
|
||||
}
|
||||
|
||||
if (cart.context.user_agent) {
|
||||
eventContext.user_agent = cart.context.user_agent
|
||||
}
|
||||
|
||||
if (segmentService.options_ && segmentService.options_.use_ga_id) {
|
||||
if (cart.context.ga_id) {
|
||||
integrations["Google Analytics"] = {
|
||||
clientId: cart.context.ga_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
console.warn("Failed to gather context for order")
|
||||
}
|
||||
}
|
||||
|
||||
const orderData = await segmentService.buildOrder(order)
|
||||
const orderEvent = {
|
||||
event: "Order Completed",
|
||||
userId: order.customer_id,
|
||||
properties: orderData,
|
||||
timestamp: order.created_at,
|
||||
context: eventContext,
|
||||
integrations,
|
||||
}
|
||||
|
||||
segmentService.track(orderEvent)
|
||||
|
||||
Reference in New Issue
Block a user