An fix currency empty before cart - #3931
Conversation
| } | ||
|
|
||
| function getLocalizationCurrency(data: LocalizationData | null | undefined): string { | ||
| return data?.localization?.country?.currency?.isoCode ?? SHOP_FALLBACK.currency; |
There was a problem hiding this comment.
I know SHOP_FALLBACK was already in here but it feels weird to me. Shouldn't we just bail on analytics if we don't have the right information?
There was a problem hiding this comment.
if it's required – if not pass undefined
but yes fail before hardcoding something that may not be accurate
frandiox
left a comment
There was a problem hiding this comment.
Thanks! I thought currency became optional in analytics?
I guess most shops will hardcode it to their only currency, or query currency early also for their own markets support (e.g. dropdown with currencies etc). I wonder if this kind of decision should be part of the skills 🤔
|
Yea, we made currency is optional in the Hydrogen config. I don't think it makes sense to turn it back to required just because analytics needs it I'm checking if the analytics side can make currency optional too. If it can't, the skill felt like the more pragmatic approach. Document it as basically required so bootstrapped storefronts resolve it, pass it along, and have a fallback. |
7c03625 to
04c9a70
Compare
|
@andguy95 What if we just make currency a requirement when you publish a related event, instead of populating that field automatically? I'm not familiar with the analytics events, but I think they follow web pixel standards? Those seem to use |
TL;DR: Initialize Shopify analytics with the active market currency before consent can replay buffered events. This prevents analytics from failing on a fresh session until the shopper adds an item to the cart.
Before
ShopifyScriptsrendered beforewindow.Shopify.currency.activewas initialized. Accepting consent could therefore produce a missing-currency analytics error. Adding an item to the cart appeared to fix the issue because cart tracking synchronized the currency later.After
Both storefront templates resolve
localization.country.currency.isoCodeunder the active Storefront API market context and pass it toShopifyScriptsasi18n.currencyduring initial rendering. A configuredUSDfallback covers API failures and mock mode.What this changes
i18ntoShopifyScripts.i18nthrough its client wrapper.shop.paymentSettings.currencyCodeis the shop currency, not necessarily the active market presentment currency.Developer impact
Includes a patch changeset for
@shopify/hydrogen. This hardens the packaged analytics guidance and starter templates without adding exports or changing a public runtime contract.UX impact
Analytics can send consent-replayed events on a fresh storefront session without requiring the shopper to modify their cart first.
Risk
How to Test
window.Shopify.currency.activematches the active market currency.window.Shopify.currency.activeis unchanged and no missing-currency analytics error appears in the console.