Repository navigation
Conversation
|
Oxygen deployed a preview of your
Learn more about Hydrogen's GitHub integration. |
frandiox
left a comment
There was a problem hiding this comment.
FYI standard events exports createViewEventElement utility to create a web component hat emits view events, including product:view. Maybe that could be used here as well?
Yep it definitely can and should be using |
Move the webpackIgnore CDN import from page-view.ts into a shared module so other emitters can load Standard Events classes from one place.
66c84d3 to
97d95a4
Compare
51c0a2b to
bccc8da
Compare
📦 Bundle size report
Unchanged (11)
How this is measuredEach scenario is bundled from the built package with Rolldown (tree-shaken, minified, dynamic imports inlined), then gzipped. Peer dependencies are external. Scenarios live in 🟢 smaller · 🟡 larger (< 5%) · 🔴 larger (≥ 5%) · 🆕 not in base · ❌ failed to bundle · ⚪ unchanged |
ProductView (React, Vue) renders the shopify-product-view element. The element extends createViewEventElement from the Standard Events module via defineViewEventElement() and sends shopify:product:view from itself: on connect for page, when half of a card is visible for collection, search, and recommendation, and when the closest dialog opens for dialog. It sends the event again when product.id in view-event-payload changes (waiting for 50% visibility on reused intersect cards and deduplicating dialog sends across pending toggle events), and ignores variant-only changes. ShopifyScripts and initializeShopifyScripts register the element. Frameworks without a binding use PRODUCT_VIEW_TAG_NAME and getProductViewAttributes.
15d022f to
e41554b
Compare
29b9a0f to
9054ab2
Compare
When view-event-payload changes to a new key, restart Shopify's own trigger (disconnect, then connect the base element) instead of copying its timing rules. The base element then sends the new payload on connect, when half of the element is visible, or when its dialog opens. Remove the copied IntersectionObserver and trigger logic. Keep one duplicate check: a dialog sends once per opening, other triggers send once per key, which also covers a restart racing a dialog's queued toggle event.
Refs Shopify/developer-tools-team#1285
Part 1 of 4. This PR adds the package API only. The template and examples use it in the next PR (#4403).
TL;DR: Adds
ProductView, a component that sends theshopify:product:viewStandard Event for a product that the storefront shows, on the product page and on product cards. It is built oncreateViewEventElementfrom the Standard Events module, the same tool that Shopify's Liquid themes use.Standard Events let tools such as PerfKit, the Standard Events inspector, and agentic tests read what the buyer sees. Today Hydrogen sends only
shopify:page:viewand the cart events, so nothing tells these tools which products the buyer looked at.An earlier version of this PR sent the event from the product form store. Following the review, it now uses
createViewEventElement, and the storefront places it explicitly. This follows Hydrogen's "no magic" rule, and it works for cards too, which the store could not do.Before
Hydrogen has no way to send a product view event.
After
The element sends:
What this changes
core/shopify-scripts/standard-events.ts) that the page view code and the element registration both use.defineViewEventElement(tagName, getKey)(core/shopify-scripts/view-event-element.ts) registers Hydrogen's subclass of thecreateViewEventElement()class. When the key inview-event-payloadchanges, itsattributeChangedCallbackrestarts the base element's own trigger (basedisconnectedCallback, thenconnectedCallback). The base element then sends the new payload at the same moment it sent the first one: on connect, when half of the element is visible, or when its<dialog>opens. Hydrogen doesn't copy those timing rules.toggleevent.ProductView(React and Vue) renders ashopify-product-viewelement with the keyproduct.id, so a variant change doesn't send the event again. The element sends the event from itself, and the event bubbles todocument.contextis required, and it decides when the event fires:pagewhen the element appears,collection/search/recommendationwhen half of the card is visible,dialogwhen the closest<dialog>opens.pageanddialogrequireselectedOrFirstAvailableVariant(ornull) and include the selected variant. Card contexts need onlyid,title, andhandle, and sendselectedVariant: nullandselectedOptions: [], as the Standard Events docs say for cards.ShopifyScriptsandinitializeShopifyScripts()register the element one time. If the CDN module fails to load, nothing breaks, and no event is sent.PRODUCT_VIEW_TAG_NAMEwith the attributes fromgetProductViewAttributes().ProductViewmapsclassNameto the custom element'sclassattribute, so classes work in React 18 as well as React 19.ProductView.Developer impact
Includes a minor changeset for
@shopify/hydrogen. New exports:@shopify/hydrogen/reactand@shopify/hydrogen/vue:ProductView,ProductViewProps.@shopify/hydrogen:PRODUCT_VIEW_TAG_NAME,getProductViewAttributes,defineProductView,ProductViewOptions,ProductViewProduct.Hydrogen doesn't send the event on its own. Storefronts must add
ProductViewwhere they show products to get the event. Nothing else changes for them.The element is inline by default. Card contexts need a box (for example
className="block"), because the element measures how much of itself is visible.Out of scope
ProductViewto the React Router template and examples (feat(templates): send product view events in the template and examples #4403).shopify:collection:view(feat(hydrogen): add CollectionView for the shopify:collection:view event #4386),shopify:product:select,shopify:search:update, andshopify:cart:view.data-*test attributes).Risk
ProductViewsends no product event. TypeScript can't catch this. The skills show where it goes.attr:view-event-triggerandattr:view-event-payload.createViewEventElement()class, from a CDN file with no version number. Restarting the trigger calls the basedisconnectedCallbackandconnectedCallbackby hand. Today they only stop and start the send trigger (checked against the live CDN module in Chromium), but if Shopify adds other work there, a restart would repeat it. The longer-term fix is forcreateViewEventElementto send again on payload changes itself, which would let Hydrogen drop this file.ProductViewmapsclassNametoclassexplicitly so React 18 keeps CSS classes, but it was not run under a React 18 runtime.How to Test
This PR has no storefront wiring. To try it in a browser, check out the next PR (#4403) and follow its steps.