Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply discounts to the ecommerce event if available #419

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion assets/js/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const getProductFieldObject = ( product, quantity ) => {
variantData.item_variant = product.variation;
}

return {
const data = {
item_id: getProductId( product ),
item_name: product.name,
...getProductCategories( product ),
Expand All @@ -26,6 +26,23 @@ export const getProductFieldObject = ( product, quantity ) => {
),
...variantData,
};

// Apply discounts to ecommerce events.
// https://developers.google.com/analytics/devguides/collection/ga4/apply-discount?client_type=gtag
if (
typeof product.price_after_coupon_discount === 'number' &&
! isNaN( product.price_after_coupon_discount ) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need these two? product.price_after_coupon_discount seems to be always a number

Copy link
Member Author

@ianlin ianlin May 13, 2024

Choose a reason for hiding this comment

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

Since there were not only purchase event would call the method getProductFieldObject(), for example begin_checkout event would call this method as well. Initially I wanted to test if product.price_after_coupon_discount is defined but since the price after discount could be 0 and 0 is treated as false, that's why I added these two conditions here.

Copy link
Contributor

@puntope puntope May 13, 2024

Choose a reason for hiding this comment

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

Ah, I see. Thanks for clarifying. That might explain why we used

if ( typeof product.price_after_coupon_discount === 'number' )

But what about the NaN line check?

Also WDYT of doing something like:

if ( product?.price_after_coupon_discount < product.prices.price )

So we can save both checks number and nan

You can test it but in case product.price_after_coupon_discount is undefined this would be false still.

Copy link
Member Author

Choose a reason for hiding this comment

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

@puntope

But what about the NaN line check?

I added it because I see typeof NaN is also a number, so added it for some prevention.

Also WDYT of doing something like:
if ( product?.price_after_coupon_discount < product.prices.price )
So we can save both checks number and nan
You can test it but in case product.price_after_coupon_discount is undefined this would be false still.

Ah good point, thanks for the info.

product.price_after_coupon_discount !== product.prices.price
Copy link
Contributor

Choose a reason for hiding this comment

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

WDYT about changing this to product.price_after_coupon_discount < product.prices.price I don't think we can have negative discounts.

) {
data.discount =
product.prices.price - product.price_after_coupon_discount;
Copy link
Contributor

@puntope puntope May 10, 2024

Choose a reason for hiding this comment

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

We need to wrap this with formatPrice() as well. Otherwise, the discount is sent without decimals.

Screenshot 2024-05-10 at 16 25 13

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the nice catch!

data.price = formatPrice(
product.price_after_coupon_discount,
product.prices.currency_minor_unit
);
}

return data;
};

/**
Expand Down
5 changes: 4 additions & 1 deletion includes/class-wc-abstract-google-analytics-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ function ( $item ) {
return array_merge(
$this->get_formatted_product( $item->get_product() ),
array(
'quantity' => $item->get_quantity(),
'quantity' => $item->get_quantity(),
// The method get_total() will return the price after coupon discounts.
// https://github.com/woocommerce/woocommerce/blob/54eba223b8dec015c91a13423f9eced09e96f399/plugins/woocommerce/includes/class-wc-order-item-product.php#L308-L310
'price_after_coupon_discount' => $this->get_formatted_price( $item->get_total() ),
)
);
},
Expand Down
Loading