Skip to main content

Loading Recommendations

The SDK fetches the recommendation sections you configure in the Glood dashboard — complete with full product data (title, handle, prices, images, options, and variants) — so you can render them with your own React components. There are two ways to load recommendations:

Client-Side: useRecommendations Hook

Use the hook inside any component rendered under <GloodProvider>. The page URL, locale, and Glood client id are filled in automatically from the browser.
For a v3 client the hook waits for the page-load /v3/headless/init call to settle before fetching sections (staying in loading until then), so init runs first and the sections request carries the init-issued visitor id. No extra wiring needed — this ordering is automatic.
Shopify product ids in Hydrogen are GIDs like gid://shopify/Product/8977367564515. Pass only the numeric part: product.id.split('/').pop().

Page Types

Every request declares which page the visitor is on. Glood returns the sections configured for that page type in the dashboard:

Hook Result

The hook returns everything needed to render and attribute recommendations:

Deferring the Request

Pass { skip: true } as the second argument to wait until required params are ready:

Server-Side: fetchRecommendationSections

For SSR, fetch recommendations inside a Hydrogen route loader. Pass pageUrl and locale explicitly — there is no browser context on the server:
Always wrap the call in a .catch() (or try/catch) inside loaders. A recommendations outage should degrade gracefully, not 500 your page.

Rendering SSR sections with attribution

The loader only fetches the data — you still render and track it in a client component. Because you are not using the useRecommendations hook here, the hook’s trackRender/trackView/trackClick/trackAddToCart helpers aren’t available; instead get the recommendations app from useGloodAnalytics() and call its attribution methods directly. Two SSR-specific details are handled below:
  • Persist the assigned experience to localStorage yourself. fetchRecommendationSections only persists it in the browser, and the loader runs on the server — so pixel/attribution events won’t carry it unless you save it client-side.
  • Fire trackRender once the component mounts in the browser (the data is already in the SSR HTML, but attribution events are always sent client-side).
Render it with the loader data (it’s already in the initial HTML — no client fetch, no layout shift):
Unlike the client-side hook, an SSR loader does not run /v3/headless/init and cannot read the browser’s _glood_user_id cookie. To attribute SSR requests to a returning visitor, parse the id from the incoming request cookies and pass it explicitly as userId (and clientId) to fetchRecommendationSections.

Attribution Tracking

To attribute recommendation performance (views, clicks, and add-to-carts tied back to revenue in Glood analytics), fire the attribution helpers at the right moments. Each takes a SectionTrack object that links the interaction to the exact section render via sectionServeId:
Standard events (page_viewed, product_added_to_cart, etc.) are sent automatically by GloodProvider — you never emit those manually. Attribution helpers are the only events you call yourself, because only your UI knows when a recommendation is clicked.

Experience Persistence

The sections API assigns each visitor an A/B experience. The SDK automatically persists the assigned experience id to localStorage (key gai_s_e) after every fetch, and every pixel event carries it — so all analytics segment correctly by experience without any work on your side.

Error Handling

Failed requests throw (or surface via the hook’s error) a GloodApiError carrying the HTTP status and response body:
Common failures: Empty sections: [] with a 200 is not an error — it means no sections are enabled for that page type in the Glood dashboard (or targeting rules excluded them).

See Also