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 theuseRecommendations hook. Copy the pieces you need.
1. Client Setup
Create the client once and register the recommendations app: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 DOMtrackView→ when the section scrolls into the viewporttrackClick→ when a recommended product link is clickedtrackAddToCart→ when the product is added to cart
3. Use It on Any Page
Product page — pass the numeric product id (strip the GID prefix):4. Server-Side Rendering Variant
To include recommendations in the initial HTML, fetch in the route loader withfetchRecommendationSections 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 inentry.server: