Skip to main content

Recommendations Example

A complete, tested integration showing recommendations on a product page and the homepage, with full attribution tracking. The primary path renders client-side with the useRecommendations hook. Copy the pieces you need.

1. Client Setup

Create the client once and register the recommendations app:
Wrap your app in root.tsx (inside Shopify’s Analytics.Provider). A v3 client fires POST /v3/headless/init automatically on mount:

2. Reusable Recommendations Component (client-side)

useRecommendations(params, options?) fetches sections in the browser via the recommendations app from GloodProvider. Because the fetch runs client-side, pageUrl / locale / clientId / userId default from the browser and the assigned experience is persisted automatically. The hook also returns four attribution helpers. Fire them against a section’s section_serve_id:
  • trackRender → when the section mounts in the DOM
  • trackView → when the section scrolls into the viewport
  • trackClick → when a recommended product link is clicked
  • trackAddToCart → when the product is added to cart

3. Use It on Any Page

Product page — pass the numeric product id (strip the GID prefix):
Homepage:
Cart page — pass what’s in the cart so Glood can exclude/complement it:

4. Server-Side Rendering Variant

To include recommendations in the initial HTML, fetch in the route loader with fetchRecommendationSections instead of the hook:
With SSR you still get attribution: retrieve the track helpers client-side from useRecommendations (pass { skip: true } to reuse the helpers without a second fetch) and call trackRender / trackView / trackClick / trackAddToCart with the section_serve_id values from your loader data.

5. Don’t Forget the CSP

Recommendation fetches and events are blocked by Hydrogen’s default Content Security Policy. Add Glood’s endpoint in entry.server:
See Content Security Policy for details.

What You’ll See in the Console (debug: true)

See Also