Skip to content

fix(ga4-destination): fix an issue with zero value purchases/transact… #1183

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

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Changes from all commits
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
37 changes: 27 additions & 10 deletions libs/core-functions/src/functions/ga4-destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ function adjustName(name: string): string {
return name.substring(0, 40);
}

// `0`is falsy so we need to explicitly check for it
function resolvePossibleValue(...possibleValues: any[]): any {
for (const value of possibleValues) {
if (typeof value !== "undefined" && value !== null) {
return value;
}
}

return possibleValues.at(-1);
}

// typically values are resolved in the following order, however there are
// a few exceptions.
function resolveStandardValue(evp: any) {
return resolvePossibleValue(evp?.value, evp?.total, evp?.revenue);
}

function trackEvent(event: AnalyticsServerEvent): Ga4Event {
const evp = event.properties || {};
let params: Record<string, any> = {};
Expand All @@ -190,15 +207,15 @@ function trackEvent(event: AnalyticsServerEvent): Ga4Event {
case "Checkout Started":
name = "begin_checkout";
params.currency = evp.currency;
params.value = evp.value || evp.total || evp.revenue;
params.value = resolveStandardValue(evp);
params.coupon = evp.coupon;
params.items = getItems(event);
break;
case "Order Refunded":
name = "refund";
params.currency = evp.currency;
params.transaction_id = evp.order_id;
params.value = evp.total || evp.value || evp.revenue;
params.value = resolvePossibleValue(evp.total, evp.value, evp.revenue);
params.coupon = evp.coupon;
params.shipping = evp.shipping;
params.affiliation = evp.affiliation;
Expand All @@ -208,27 +225,27 @@ function trackEvent(event: AnalyticsServerEvent): Ga4Event {
case "Product Added":
name = "add_to_cart";
params.currency = evp.currency;
params.value = evp.value || evp.total || evp.revenue;
params.value = resolveStandardValue(evp);
params.items = getItems(event);
break;
case "Payment Info Entered":
name = "add_payment_info";
params.currency = evp.currency;
params.value = evp.value || evp.total || evp.revenue;
params.value = resolveStandardValue(evp);
params.coupon = evp.coupon;
params.payment_type = evp.payment_method;
params.items = getItems(event);
break;
case "Product Added to Wishlist":
name = "add_to_wishlist";
params.currency = evp.currency;
params.value = evp.value || evp.total || evp.revenue;
params.value = resolveStandardValue(evp);
params.items = getItems(event);
break;
case "Product Viewed":
name = "view_item";
params.currency = evp.currency;
params.value = evp.value || evp.total || evp.revenue;
params.value = resolveStandardValue(evp);
params.items = getItems(event);
break;
case "Signed Up":
Expand All @@ -239,7 +256,7 @@ function trackEvent(event: AnalyticsServerEvent): Ga4Event {
name = "purchase";
params.currency = evp.currency;
params.transaction_id = evp.order_id;
params.value = evp.total || evp.value || evp.revenue;
params.value = resolvePossibleValue(evp.total, evp.value, evp.revenue);
params.coupon = evp.coupon;
params.shipping = evp.shipping;
params.affiliation = evp.affiliation;
Expand All @@ -258,7 +275,7 @@ function trackEvent(event: AnalyticsServerEvent): Ga4Event {
case "Cart Viewed":
name = "view_cart";
params.currency = evp.currency;
params.value = evp.value || evp.total || evp.revenue;
params.value = resolveStandardValue(evp);
params.items = getItems(event);
break;
case "Signed In":
Expand All @@ -268,7 +285,7 @@ function trackEvent(event: AnalyticsServerEvent): Ga4Event {
case "Product Removed":
name = "remove_from_cart";
params.currency = evp.currency;
params.value = evp.value || evp.total || evp.revenue;
params.value = resolveStandardValue(evp);
params.items = getItems(event);
break;
case "Products Searched":
Expand All @@ -286,7 +303,7 @@ function trackEvent(event: AnalyticsServerEvent): Ga4Event {
params = { ...evp };
params = removeProperties(params, StandardProperties);
params.currency = evp.currency;
params.value = evp.value || evp.total || evp.revenue;
params.value = resolveStandardValue(evp);
break;
}
params.engagement_time_msec = 1;
Expand Down
Loading