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

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 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

Hydrogen has no way to send a product view 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

  • The Standard Events CDN loader moves to one shared module (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 the createViewEventElement() class. When the key in view-event-payload changes, its attributeChangedCallback restarts the base element's own trigger (base disconnectedCallback, then connectedCallback). 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.
    • One duplicate check stays: a dialog sends once per opening, other triggers once per key. This also covers a restart racing a dialog's queued toggle event.
    • It ignores payloads that don't parse, and catches CDN load and registration errors.
  • ProductView (React and Vue) renders a shopify-product-view element with the key product.id, so a variant change doesn't send the event again. 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, dialog when the closest <dialog> opens.
  • page and dialog require selectedOrFirstAvailableVariant (or null) and include the selected variant. Card contexts need only id, title, and handle, and send selectedVariant: null and selectedOptions: [], as the Standard Events docs say for cards.
  • 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().
  • React ProductView maps className to the custom element's class attribute, so classes work in React 18 as well as React 19.
  • 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. 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.

Out of scope

Risk

  • A storefront that forgets ProductView sends no product event. TypeScript can't catch this. The skills show where it goes.
  • The element reads HTML attributes, not properties. Solid sets custom element values as properties in the browser, so Solid storefronts must use attr:view-event-trigger and attr:view-event-payload.
  • The subclass depends on the lifecycle of Shopify's createViewEventElement() class, from a CDN file with no version number. Restarting the trigger calls the base disconnectedCallback and connectedCallback by 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 for createViewEventElement to send again on payload changes itself, which would let Hydrogen drop this file.
  • Tests run under React 19 only. React ProductView maps className to class explicitly 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.

@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 20, 2026 6:25 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.
@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
@Ji-One1
Ji-One1 marked this pull request as draft November 18, 2026 06:18
@Ji-One1
Ji-One1 force-pushed the jb/hydrogen-product-view-event branch from 51c0a2b to bccc8da Compare November 18, 2026 16:21
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

📦 Bundle size report

Status Target Scenario Minified Gzip Δ Minified Δ Gzip
🟡 server entry: @shopify/hydrogen 171.57 kB 54.23 kB +2.09 kB (+1.2%) +672 B (+1.3%)
🟡 client entry: @shopify/hydrogen/react 100.31 kB 32.44 kB +2.13 kB (+2.2%) +555 B (+1.7%)
🟡 client entry: @shopify/hydrogen/vue 100.16 kB 32.28 kB +2.17 kB (+2.2%) +590 B (+1.9%)
🔴 client react shopify scripts 20.04 kB 7.38 kB +1.38 kB (+7.4%) +507 B (+7.4%)
Unchanged (11)
Status Target Scenario Minified Gzip Δ Minified Δ Gzip
⚪ server cart handlers 23.85 kB 7.11 kB ±0 ±0
⚪ server customer account 22.91 kB 7.17 kB ±0 ±0
⚪ server entry: @shopify/hydrogen/customer-account 25.41 kB 8.29 kB ±0 ±0
⚪ server predictive search handlers 9.32 kB 3.71 kB ±0 ±0
⚪ server request handling 53.8 kB 17.52 kB ±0 ±0
⚪ client react cart 48.3 kB 15.08 kB ±0 ±0
⚪ client react collection 11.59 kB 3.94 kB ±0 ±0
⚪ client react predictive search 9.39 kB 3.43 kB ±0 ±0
⚪ client react product 14.66 kB 5.23 kB ±0 ±0
⚪ client react shop pay button 10.13 kB 4.45 kB ±0 ±0
⚪ client vanilla cart 37.44 kB 11.59 kB ±0 ±0
How this is measured

Each scenario is bundled from the built package with Rolldown (tree-shaken, minified, dynamic imports inlined), then gzipped. Peer dependencies are external. Scenarios live in scripts/bundle-size-scenarios.ts.

🟢 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.
@Ji-One1
Ji-One1 force-pushed the jb/hydrogen-product-view-event branch from 15d022f to e41554b Compare November 19, 2026 21:04
@Ji-One1
Ji-One1 force-pushed the jb/hydrogen-product-view-event branch from 29b9a0f to 9054ab2 Compare November 19, 2026 22:27
@Ji-One1
Ji-One1 marked this pull request as ready for review November 20, 2026 05:41
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.

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