Skip to content

feat: auto-calculate commerce event total amount from products#1280

Open
jamesnrokt wants to merge 1 commit into
developmentfrom
feat/checkout-auto-calculate-total-amount
Open

feat: auto-calculate commerce event total amount from products#1280
jamesnrokt wants to merge 1 commit into
developmentfrom
feat/checkout-auto-calculate-total-amount

Conversation

@jamesnrokt

Copy link
Copy Markdown
Collaborator

Summary

A commerce event's transaction-level Total Amount (ProductAction.TotalAmount) was only ever populated from a caller-supplied transactionAttributes.Revenue. When Revenue was omitted, the Web SDK left the total at 0 — 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 supply Revenue, bringing the Web SDK's behavior in line with mParticle's other platform SDKs.

Changes

  • src/ecommerce.js — new calculateProductActionTotalAmount(productAction) helper: sums quantity × price across the product list, adds ShippingAmount and TaxAmount, and assigns the result to TotalAmount. A total the caller explicitly provided (including 0) is never overwritten.
  • src/events.js — invoked from logCommerceEvent, 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

⚠️ This changes observed output for integrations that omit Revenue: they will now emit a non-zero Total Amount (tr / total_amount) downstream instead of 0. Integrations that pass Revenue are unaffected.

Testing

  • npm test — 1090 passing, 0 failing (ChromeHeadless).

🤖 Generated with Claude Code

@jamesnrokt jamesnrokt requested a review from a team as a code owner June 16, 2026 18:47
@cursor

cursor Bot commented Jun 16, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes emitted commerce payload values for integrations that omit Revenue, which can affect downstream analytics and partner kits without touching auth or security.

Overview
Commerce events that omit transactionAttributes.Revenue now get ProductAction.TotalAmount filled from line items (Σ quantity × price) plus shipping and tax, instead of leaving the basket total at 0 while products still carry correct amounts.

A new calculateProductActionTotalAmount helper in ecommerce.js performs that math (via parseNumber). logCommerceEvent invokes it for every product-action commerce event before upload, so checkout, purchase, refund, and similar flows behave consistently. Explicit Revenue (including 0) is never overwritten.

Compatibility: downstream total_amount / tr will change for integrations that did not pass Revenue; callers that set Revenue are unchanged. Tests add integration and unit coverage for auto-sum, shipping/tax, and no-override behavior.

Reviewed by Cursor Bugbot for commit fd939eb. Bugbot is set up for automated code reviews on this repo. Configure here.

Comment thread src/ecommerce.js Outdated
Comment thread src/ecommerce.js Outdated
Comment thread src/ecommerce.js

@jaissica12 jaissica12 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jamesnrokt small thing, can you retarget this to development development

@jamesnrokt jamesnrokt changed the base branch from master to development June 16, 2026 21:04
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>
@jamesnrokt

Copy link
Copy Markdown
Collaborator Author

@jamesnrokt small thing, can you retarget this to development development

Done

@jamesnrokt jamesnrokt force-pushed the feat/checkout-auto-calculate-total-amount branch from 7c9f9ed to fd939eb Compare June 17, 2026 15:13
@sonarqubecloud

Copy link
Copy Markdown

@jaissica12 jaissica12 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants