Skip to content

feat(templates): send product view events in the template and examples - #4403

Open
Ji-One1 wants to merge 4 commits into
jb/hydrogen-product-view-eventfrom
jb/hydrogen-product-view-apps
Open

Ji-One1 wants to merge 4 commits into
jb/hydrogen-product-view-eventfrom
jb/hydrogen-product-view-apps

Conversation

@Ji-One1

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

Copy link
Copy Markdown
Collaborator

Stacked on #4376. Refs Shopify/developer-tools-team#1285

Part 2 of 4. Adds ProductView from #4376 to the React Router template and the examples.

TL;DR: The template and the Hydrogen, Next.js, Nuxt, SvelteKit, and Solid examples now send shopify:product:view on product pages and product cards.

Before

The template and examples send no product view event.

After

Product pages are wrapped in <ProductView context="page">, and each product card in <ProductView context="collection" | "search" | "recommendation">, with the context of the list that shows it.

What this changes

  • React Router template: ProductView on the product page, on product cards (home and collection pages use collection, search uses search, related products use recommendation), and in the search drawer. The product card fragment now asks for the product id.
  • Hydrogen, Next.js, and Nuxt examples: the same, using the React and Vue ProductView components.
  • SvelteKit and Solid examples: they render the shopify-view-event element with getProductViewAttributes(). Their product pages key the element on the product ID ({#key} in SvelteKit, <Show keyed> in Solid), so a new product gets a new element, which sends the event. Solid sets the two attributes with attr:, because the element reads HTML attributes. The Solid example declares the element's JSX type once, in src/global.d.ts.
  • Product cards get a block class, so the element can measure how much of each card is visible.

Developer impact

No changeset: these are template and example changes only. They show the supported pattern for ProductView.

UX impact

No visible change. The new element wraps the existing markup, with a block class, and keeps the same layout, accessible names, and test IDs.

Out of scope

  • The Astro example. It still sends no product view event.

Risk

  • Cards that are keyed by product remount on sort or filter changes, so the same products can send a view again when they come back into view.

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 SHOPIFY-VIEW-EVENT 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.

@Ji-One1
Ji-One1 marked this pull request as ready for review November 20, 2026 05:41
@Ji-One1
Ji-One1 requested a review from a team as a code owner November 20, 2026 05:41
@Ji-One1
Ji-One1 force-pushed the jb/hydrogen-product-view-apps branch from d8bc01a to d2fbef4 Compare November 20, 2026 06:24
@Ji-One1
Ji-One1 force-pushed the jb/hydrogen-product-view-event branch from 3736c13 to a149550 Compare November 20, 2026 15:24
@Ji-One1
Ji-One1 force-pushed the jb/hydrogen-product-view-apps branch from d2fbef4 to d481f02 Compare November 20, 2026 15:24

@fredericoo fredericoo 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.

great stuff! some micro suggestions

import { getSelectedProductOptions } from "@shopify/hydrogen";
import { gql } from "@shopify/hydrogen";
import { useLocale } from "@shopify/hydrogen/react";
import { ProductView as ShopifyProductView, useLocale } from "@shopify/hydrogen/react";

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.

non-blocking: this file now has two components called ProductView, and the new documented one is the one that got aliased. Someone copying this example might pick the wrong one.

Let's alias the local one instead, so ProductView from @shopify/hydrogen/react keeps its documented name:

Suggested change
import { ProductView as ShopifyProductView, useLocale } from "@shopify/hydrogen/react";
import { ProductView, useLocale } from "@shopify/hydrogen/react";

This needs three more edits in the same file to compile:

  • line 9 becomes import { ProductView as ProductViewedAnalytics } from "~/lib/analytics";
  • ShopifyProductView on lines 134 and 168 becomes ProductView
  • the <ProductView products={...} /> on line 154 becomes <ProductViewedAnalytics


type ProductCardProps = {
product: ProductCardData;
context: "collection" | "search" | "recommendation";

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.

question: the card context union is repeated in all six card components, so a new card context means editing each one.

Is the literal deliberate, to keep the examples self-explanatory, or would a named type exported from #4376 be worth it? Nothing to change in this PR either way.

@Ji-One1
Ji-One1 force-pushed the jb/hydrogen-product-view-apps branch 2 times, most recently from d4cfb33 to 8867efb Compare November 20, 2026 18:28
@Ji-One1
Ji-One1 force-pushed the jb/hydrogen-product-view-apps branch from 8867efb to e3b0631 Compare November 25, 2026 21:36
@Ji-One1
Ji-One1 force-pushed the jb/hydrogen-product-view-event branch from 160d371 to 0c49bc4 Compare November 25, 2026 22:04
@Ji-One1
Ji-One1 force-pushed the jb/hydrogen-product-view-apps branch from e3b0631 to de419e6 Compare November 25, 2026 22:04
@Ji-One1
Ji-One1 force-pushed the jb/hydrogen-product-view-event branch from 0c49bc4 to d38d07a Compare November 26, 2026 06:07
@Ji-One1
Ji-One1 force-pushed the jb/hydrogen-product-view-apps branch 2 times, most recently from a94623b to 2b231b0 Compare November 26, 2026 21:56
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
shopify-view-event element with getProductViewAttributes. Solid sets
the attributes with attr:, because the element reads HTML attributes.
@Ji-One1
Ji-One1 force-pushed the jb/hydrogen-product-view-apps branch from 2b231b0 to fc66185 Compare November 26, 2026 23:42

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