fix(analytics): always render <Analytics.ProductView> on PDP#384
Merged
Conversation
The component was gated on `{selectedVariant && ...}`, which skips
the mount entirely on the first render when `selectedVariant` is
`null`. For combined-listing products (which use
`useOptimisticVariant` to resolve the variant after hydration), this
means `PRODUCT_VIEWED` never dispatches and the `view_item` event
never reaches GTM / dataLayer / Hydrogen Shopify Analytics.
The fallback values (`selectedVariant?.price.amount || "0"`,
`selectedVariant?.id || ""`, `selectedVariant?.title || ""`) were
already in place — they just weren't reachable behind the truthiness
guard. Removing the guard activates them as intended.
After this change, `view_item` fires reliably on every PDP visit,
including products where the variant resolves post-hydration.
hta218
approved these changes
May 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Remove the
{selectedVariant && ...}guard around<Analytics.ProductView>inapp/routes/products/product.tsx. The component now mounts on every PDP render.Why
For combined-listing products (where the variant is resolved after hydration via
useOptimisticVariant),selectedVariantisnullon the first render. The truthiness guard skips the mount entirely, so:AnalyticsEvent.PRODUCT_VIEWEDnever dispatchesview_itemnever reacheswindow.dataLayerview_itemnever fire on combined-listing PDPs<ShopifyAnalytics>is missing the product context for downstream events on the same visitThe fallback values
selectedVariant?.price.amount || "0",selectedVariant?.id || "",selectedVariant?.title || ""were already in place inside the JSX — they just weren't reachable behind the guard. Removing the guard activates them as intended.Behaviour change
view_itemfires only afterselectedVariantresolves (which is post-hydration for combined listings — often after the user has already interacted with the page).view_itemfires on first render for every product, with safe fallback values when variant data isn't loaded yet.For products where
selectedVariantIS available on first render (the majority — standard non-combined products), behaviour is unchanged.Verification
view_itemshould appear on initial page load.selectedVariantresolves (they're reactive bindings, so the analytics payload updates if needed for subsequent events).Risk
Minimal. The change reverses a defensive guard that was over-restrictive; the fallback values handle the no-variant case gracefully. Tested locally on standard products, combined listings, and bundled products.