feat: auto-calculate commerce event total amount from products#1280
feat: auto-calculate commerce event total amount from products#1280jamesnrokt wants to merge 1 commit into
Conversation
PR SummaryMedium Risk Overview A new Compatibility: downstream Reviewed by Cursor Bugbot for commit fd939eb. Bugbot is set up for automated code reviews on this repo. Configure here. |
jaissica12
left a comment
There was a problem hiding this comment.
@jamesnrokt small thing, can you retarget this to development development
Derive a commerce event's transaction-level total (ProductAction.TotalAmount) from the product list plus shipping and tax when the caller does not supply transactionAttributes.Revenue. Previously the Web SDK left the total at 0 whenever Revenue was omitted, even though individual product amounts were recorded, so multi-item checkouts reported a zero basket total. The total is derived as sum(quantity * price) + shipping + tax, aligning the Web SDK's behavior with mParticle's other platform SDKs. A total the caller explicitly provides (including 0) is never overwritten. The calculation runs in logCommerceEvent so it applies uniformly to every product-action commerce event. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Done |
7c9f9ed to
fd939eb
Compare
|



Summary
A commerce event's transaction-level Total Amount (
ProductAction.TotalAmount) was only ever populated from a caller-suppliedtransactionAttributes.Revenue. WhenRevenuewas omitted, the Web SDK left the total at0— even though each product's individual amount was recorded correctly. The result: multi-item checkouts reported every line item but a zero basket total.This PR derives the total from the product list (
Σ quantity × price) plus shipping and tax whenever the caller doesn't supplyRevenue, bringing the Web SDK's behavior in line with mParticle's other platform SDKs.Changes
src/ecommerce.js— newcalculateProductActionTotalAmount(productAction)helper: sumsquantity × priceacross the product list, addsShippingAmountandTaxAmount, and assigns the result toTotalAmount. A total the caller explicitly provided (including0) is never overwritten.src/events.js— invoked fromlogCommerceEvent, the single chokepoint all commerce events flow through, so it applies uniformly to checkout, purchase, refund, add-to-cart, etc.test/src/tests-eCommerce.js— integration tests (auto-sum, shipping/tax included, explicit revenue not overridden) and unit tests for the helper.Behavior change / compatibility
Revenue: they will now emit a non-zeroTotal Amount(tr/total_amount) downstream instead of0. Integrations that passRevenueare unaffected.Testing
npm test— 1090 passing, 0 failing (ChromeHeadless).🤖 Generated with Claude Code