Skip to content

feat(hydrogen): add ProductView for the shopify:product:view event - #4376

Open
Ji-One1 wants to merge 5 commits into
mainfrom
jb/hydrogen-product-view-event
Open

Ji-One1 wants to merge 5 commits into
mainfrom
jb/hydrogen-product-view-event

Conversation

@Ji-One1

@Ji-One1 Ji-One1 commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Refs Shopify/developer-tools-team#1285

TL;DR: Adds ProductView, a component that sends the shopify:product:view Standard Event for a product that the storefront shows, on the product page and on product cards. It is built on createViewEventElement from 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:view and 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

Product pages and product cards send no product event.

After

// product page
<ProductView product={product} context="page" className="block">
  <ProductPageContent product={product} />
</ProductView>

// product card
<ProductView product={product} context="collection" className="block">
  <article>...</article>
</ProductView>

The element sends:

{ context, selectedOptions, product: { id, title, handle, selectedVariant } }

What this changes

  • ProductView (React and Vue) renders a hydrogen-product-view element. It is made with createViewEventElement() from https://cdn.shopify.com/storefront/standard-events.js. The element sends the event from itself, and the event bubbles to document.
  • context is required, and it decides when the event fires:
    • page: when the element appears.
    • collection, search, recommendation: when half of the card is visible. Each card fires once.
    • dialog: when the closest <dialog> opens.
  • page and dialog include the selected variant. Card contexts send selectedVariant: null and selectedOptions: [], as the Standard Events docs say for cards, and need only the product id, title, and handle.
  • When the product ID changes during in-app navigation, ProductView sends the event again. A variant change does not.
  • ShopifyScripts and initializeShopifyScripts() register the element one time. If the CDN module fails to load, nothing breaks, and no event is sent.
  • Frameworks without a Hydrogen binding (Solid, Svelte) render PRODUCT_VIEW_TAG_NAME with the attributes from getProductViewAttributes(). The SvelteKit and Solid examples show how.
  • The Standard Events CDN loader moves to one shared module that the page view code and the element registration both use.
  • The React Router template and the Hydrogen, Next.js, Nuxt, SvelteKit, and Solid examples add ProductView to the product page and to every product card, with the context of each list. The card queries now ask for the product id.
  • The analytics, variant form, and collection browser skills explain where to put ProductView.

Developer impact

Includes a minor changeset for @shopify/hydrogen. New exports:

  • @shopify/hydrogen/react and @shopify/hydrogen/vue: ProductView, ProductViewProps.
  • @shopify/hydrogen: PRODUCT_VIEW_TAG_NAME, getProductViewAttributes, defineProductView, ProductViewOptions, ProductViewProduct.

Hydrogen doesn't send the event on its own. Existing storefronts must add ProductView where 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.

UX impact

No visible change. The template and examples wrap the existing markup in the new element, with a block class, and keep the same layout, accessible names, and test IDs.

Out of scope

  • shopify:product:select, shopify:search:update, shopify:collection:view, and shopify:cart:view.
  • Signifiers (data-* test attributes).

Risk

  • A storefront that forgets ProductView sends no product event. TypeScript can't catch this. The template, examples, and skills show it.
  • In the SvelteKit and Solid examples, the key that makes the element again on a new product also makes the main product section again. Variant changes don't.
  • When the product ID changes, the event fires at once, even for a card that is off screen. This is rare for cards.
  • Checked with React 19 only. React 18 is in the peer range, and its handling of className on custom elements was not tested.

How to Test

  1. Run pnpm install && pnpm build:pkgs.
  2. In templates/react-router, run pnpm dev. With no .env, the template uses mock.shop.
  3. Open the local URL that the dev server prints. In DevTools, run document.addEventListener("shopify:product:view", (e) => console.log(e.context, e.product.handle, e.target.tagName), true).
  4. Scroll down the home page. Confirm one collection log for each card when it comes into view, and no duplicates.
  5. Click a product. Confirm one page log with that handle and the HYDROGEN-PRODUCT-VIEW target.
  6. Select a different variant. Confirm no new page log.
  7. Scroll to the related products. Confirm recommendation logs.
  8. Click a related product. Confirm exactly one new page log.
  9. Search for "shirt". Confirm search logs for the visible results.
  10. To check a full page load, add debug={{ standardEventsInspector: true }} to <ShopifyScripts> in app/root.tsx, reload a product page, and confirm that the inspector shows one shopify:product:view.

@shopify

shopify Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Oxygen deployed a preview of your jb/hydrogen-product-view-event branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment November 17, 2026 6:45 AM

Learn more about Hydrogen's GitHub integration.

@frandiox frandiox left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

@Ji-One1

Ji-One1 commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator Author

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 createViewEventElement. I like this approach better, I'll use it here. Also makes implementing collection:view later with the same pattern easier

Move the webpackIgnore CDN import from page-view.ts into a shared
module so other emitters can load Standard Events classes from one
place.
ProductView (React, Vue) renders the hydrogen-product-view element, built
with createViewEventElement from the Standard Events module. The element
sends shopify:product:view from itself: on render for the product page,
when the closest dialog opens for dialogs, and when half of a card is
visible for collection, search, and recommendation cards. The component
sends the event again when the product ID changes.

ShopifyScripts and initializeShopifyScripts register the element.
Frameworks without a binding use PRODUCT_VIEW_TAG_NAME and
getProductViewAttributes.
Wrap the product page in ProductView with context page, and each product
card with the context of its list: collection, search, or recommendation.
Add the product ID to the product card fragment.
Add ProductView to the product page and product cards of the Hydrogen,
Next.js, and Nuxt examples. The SvelteKit and Solid examples render the
hydrogen-product-view element with getProductViewAttributes, keyed by
product ID.
@Ji-One1
Ji-One1 force-pushed the jb/hydrogen-product-view-event branch from 66c84d3 to 97d95a4 Compare November 17, 2026 04:49
@Ji-One1 Ji-One1 changed the title feat(hydrogen): dispatch shopify:product:view from the product form store feat(hydrogen): add ProductView for the shopify:product:view event Nov 17, 2026
@Ji-One1
Ji-One1 marked this pull request as ready for review November 17, 2026 06:26
@Ji-One1
Ji-One1 requested a review from a team as a code owner November 17, 2026 06:26
Keep className as the public prop and pass class to the custom element. Clarify view-event trigger counts and regenerate the API docs.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants