Repository navigation
Conversation
d8bc01a to
d2fbef4
Compare
3736c13 to
a149550
Compare
d2fbef4 to
d481f02
Compare
fredericoo
left a comment
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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:
| 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"; ShopifyProductViewon lines 134 and 168 becomesProductView- the
<ProductView products={...} />on line 154 becomes<ProductViewedAnalytics
|
|
||
| type ProductCardProps = { | ||
| product: ProductCardData; | ||
| context: "collection" | "search" | "recommendation"; |
There was a problem hiding this comment.
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.
d4cfb33 to
8867efb
Compare
8867efb to
e3b0631
Compare
160d371 to
0c49bc4
Compare
e3b0631 to
de419e6
Compare
0c49bc4 to
d38d07a
Compare
a94623b to
2b231b0
Compare
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.
2b231b0 to
fc66185
Compare
Stacked on #4376. Refs Shopify/developer-tools-team#1285
Part 2 of 4. Adds
ProductViewfrom #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:viewon 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
ProductViewon the product page, on product cards (home and collection pages usecollection, search usessearch, related products userecommendation), and in the search drawer. The product card fragment now asks for the productid.ProductViewcomponents.shopify-view-eventelement withgetProductViewAttributes(). 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 withattr:, because the element reads HTML attributes. The Solid example declares the element's JSX type once, insrc/global.d.ts.blockclass, 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
blockclass, and keeps the same layout, accessible names, and test IDs.Out of scope
Risk
How to Test
pnpm install && pnpm build:pkgs.templates/react-router, runpnpm dev. With no.env, the template uses mock.shop.document.addEventListener("shopify:product:view", (e) => console.log(e.context, e.product.handle, e.target.tagName), true).collectionlog for each card when it comes into view, and no duplicates.pagelog with that handle and theSHOPIFY-VIEW-EVENTtarget.pagelog.recommendationlogs.pagelog.searchlogs for the visible results.debug={{ standardEventsInspector: true }}to<ShopifyScripts>inapp/root.tsx, reload a product page, and confirm that the inspector shows oneshopify:product:view.