Skip to content

Conversation

@xav-ie
Copy link

@xav-ie xav-ie commented Apr 15, 2025

Fix totalValue test measurement

WHY are these changes introduced?

In my limited experience, I notice many Shopify stores are sending an incorrect
totalValue(in analytics lib)/total_value(in network request) measurement in
their analytics. Most Shopify storefronts default to sending just the
product.value.

I am pretty sure this is incorrect:

/** Total value of products. */
totalValue?: number;

I went to verify this, but the tests do not rigourously test the totalValue. It
just sends placeholder value, not based on the products at all. Currently, it
is not clear what expected totalValue is for different product payloads.

WHAT is this pull request doing?

Enhances the tests to ensure the totalValue is more accurate. Establishes
that totalValue is indeed the total value of the products in the event.

  1. adds helpers
  2. tests the helpers
  3. enhances the tests to have accurate totalValue measurement, making it
    clear totalValue is based on products total value.

HOW to test your changes?

npm run test

Checklist

  • I've read the Contributing Guidelines
  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've added a changeset if this PR contains user-facing or noteworthy changes
  • I've added tests to cover my changes
  • I've added or updated the documentation

@xav-ie xav-ie changed the title fix(tests): use more accurate totalValue Fix totalValue test measurement Apr 15, 2025
@xav-ie xav-ie marked this pull request as ready for review April 15, 2025 20:36
@xav-ie xav-ie marked this pull request as draft April 15, 2025 20:45
@xav-ie xav-ie force-pushed the fix-totalValue-measurement branch from 35ab53a to aad4b27 Compare April 15, 2025 20:46
@xav-ie xav-ie marked this pull request as ready for review April 15, 2025 20:46
@xav-ie xav-ie force-pushed the fix-totalValue-measurement branch from aad4b27 to aa28a44 Compare April 15, 2025 20:56
expect(events[1]).toEqual(
getExpectedPayload(pageViewPayload, {
event_name: 'product_page_rendered',
total_value: pageViewPayload.totalValue,
Copy link
Author

Choose a reason for hiding this comment

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

total_value is actually expected to not exist when product quantity is 0 or undefined:

addDataIf(
{
event_name: PRODUCT_ADDED_TO_CART_EVENT_NAME,
customerId: addToCartPayload.customerId,
cart_token: cartToken?.id ? `${cartToken.id}` : null,
total_value: addToCartPayload.totalValue,

^ removes total_value if totalValue is 0/undefined.

This is test case where totalValue is 0, since all products here are quantity undefined. products array is derived from BASE_PRODUCT_PAYLOAD, which does not define quantity.

I add a test below to test a payload where products has a product with quantity 1.

@xav-ie xav-ie marked this pull request as draft April 16, 2025 14:56
Enhances the tests to ensure the totalValue is more accurate. Establishes
that totalValue is indeed the total value of the products in the event.
@xav-ie xav-ie force-pushed the fix-totalValue-measurement branch from aa28a44 to f098607 Compare April 16, 2025 15:11
@xav-ie xav-ie marked this pull request as ready for review April 16, 2025 15:11
@juanpprieto juanpprieto requested a review from Copilot April 16, 2025 16:12
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

packages/hydrogen-react/src/analytics-utils.ts:125

  • If product.quantity is undefined, the result defaults to 0. If a missing quantity should be interpreted as 1, consider adjusting the logic to use a default of 1.
export const getProductValue = (product: ShopifyAnalyticsProduct): number => parseFloat(product.price) * (product.quantity || 0);

@xav-ie
Copy link
Author

xav-ie commented Apr 16, 2025

If product.quantity is undefined, the result defaults to 0. If a missing quantity should be interpreted as 1, consider adjusting the logic to use a default of 1.

  • copilot

If a product has no quantity, there is no quantity. The quantity is 0.
Default 1 is unexpected behavior.

@xav-ie
Copy link
Author

xav-ie commented May 15, 2025

Dear shopify devs, may I get another test run and review, please?

Your review approval will clarify what an expected event should have, which is very valuable for me. A disapproval will also be helpful in clarification.

@xav-ie
Copy link
Author

xav-ie commented Jul 8, 2025

Hello @juanpprieto , please let me know if I can change something to make this look good.

@juanpprieto
Copy link
Contributor

Hey @xav-ie! Thanks for the PR and apologies for the delay.

I am still investigating internally what this value should be. In the liquid domain the reference Horizon theme's add to cart event appears to set total_value as the individual variant price and not the combined value with the quantity.

Screenshot 2025-07-08 at 2 07 58 PM

You can test here:
https://themes.shopify.com/themes/horizon/styles/horizon/preview

I will talk to the analytics team and try to confirm why we don't seem to be sending the combined value.

@juanpprieto juanpprieto added the analytics Issues with analytics, dashboards, etc. label Jul 10, 2025
@xav-ie
Copy link
Author

xav-ie commented Jul 15, 2025

Thank you for investigating. I appreciate it a lot. This is the reason I made the PR.

I was also confused and thought this was wrong. Hopefully, it will become clear at conclusion of this PR :)

@github-actions
Copy link
Contributor

This pull request has been marked as stale due to inactivity for 60 days. If no further activity occurs, it will be closed in 7 days.

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

Labels

analytics Issues with analytics, dashboards, etc. inactive

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants