woocommerce / woocommerce-google-analytics-integration

WordPress plugin: Provides the integration between WooCommerce and Google Analytics.
http://wordpress.org/plugins/woocommerce-google-analytics-integration/
170 stars 69 forks source link

Apply discounts to the ecommerce event if available #419

Closed ianlin closed 4 months ago

ianlin commented 4 months ago

Changes proposed in this Pull Request:

Closes #417. This PR fixes a bug that the price in purchase events does not take coupon discounts into account by getting the price after discount using the method get_total() in WC_Order_Item_Product class, and add the discount parameter to the ecommerce event.

Only the purchase event will include discounts if available, since only the method get_formatted_order() will include the "price after coupon discounts" for each order line item.

Checks:

Screenshots:

Detailed test instructions:

  1. Checkout the branch of this PR and run npm ci and npm run build
  2. Open Network tab from browser dev console, add google-analytics as the filter
  3. Add some products into the cart, for example, two products.
  4. Go to checkout page
  5. Apply a coupon
  6. Complete the purchase, see the order received page
  7. From the Network tab, find the request with dt: Order Confirmation and en: purchase, which is the purchase event
  8. From this event you should see there are two product parameters starts with pr1 and pr2, e.g.
    • pr1: id28~nmSingle~caMusic~qt1~pr12~ds12
    • pr2: id36~nmBeanie with Logo~caAccessories~qt1~pr10~ds8
  9. The pr in the value is the price after coupon discounts, and ds is the amount of coupon discounts.
  10. Add another two products into the carts, complete the order WITHOUT applying any coupons.
  11. You should see there is no ds in the event parameters, and the price is just the regular price.

Additional details:

Changelog entry

Fix - Apply discounts to the ecommerce event if available

ianlin commented 4 months ago

Hey @puntope, thanks for the review and nice suggestions. I've pushed more commits and it's ready for another round.