React Components
The SDK provides React components and hooks for seamless integration with your Hydrogen application.Lifecycle
On page load the SDK’s components and hooks fire in a fixed sequence.GloodProvider bootstraps the session first (v3 init), and useRecommendations waits for that to settle before fetching sections — so recommendations are always attributed to the init-issued visitor.
Sequence of hooks:
GloodProvidermounts — subscribes to Shopify analytics and, for a v3 client, calls/v3/headless/initonce. (v2 clients skip init.)useGloodInit()— returns the init response once it resolves (nulluntil then, and alwaysnullfor v2). Also readable synchronously viaclient.getInitData().useGloodInitReady()— flips totruewhen init settles (or immediately for v2 / no client). This is the gate the next step waits on.useRecommendations(params)— holds inloadinguntiluseGloodInitReady()istrue, then fetches/v3/headless/sections. The request carries the init-issueduser_id.useGloodAnalytics()+trackRender/View/Click/AddToCart— fire attribution events (through the recommendations app) to/v3/headless/eventsas the visitor interacts.
For a v2 client there is no init call:
useGloodInitReady() is true immediately, so useRecommendations fetches without waiting. Events route to /api/storefront/event instead of /v3/headless/events.GloodProvider
A React context provider that automatically subscribes to Shopify Analytics events, manages pixel tracking for the recommendations app, and — for v3 clients — fires the headless session init on mount.Signature
Props
Basic Usage
How It Works
TheGloodProvider component:
- Creates React Context - Provides the Glood client to all child components
- Fires Headless Init - On mount, a v3 client calls
POST /api/storefront/v3/headless/initonce to bootstrap the session. The response is exposed viauseGloodInit(). For a v2 client this is a no-op. - Subscribes to Analytics - Uses Hydrogen’s
useAnalytics()hook to receive events - Distributes Events - Routes events to the recommendations app based on its subscriptions
- Checks Consent - Verifies customer privacy permissions before sending pixels
- Handles Errors - Provides comprehensive error handling and debug logging
The headless init runs exactly once per provider mount and only when the client is configured for API version 3 (the default). It replays a persisted visitor id, creates a per-visit id, and returns the shop config, storefront token, and visitor history. Init failures are logged but never block rendering.
Event Subscription Flow
Error Handling
The provider includes comprehensive error handling:Server-Side Rendering
The provider automatically handles SSR — analytics subscriptions only run in the browser:useGloodAnalytics
A React hook that provides access to the Glood client from context.Signature
Returns
Usage
useGloodInit
A React hook that returns the/v3/headless/init response fired by GloodProvider on mount.
Signature
Returns
The
InitResponse carries the server-issued session identity and shop context: the echoed user_id, the per-visit visit_id, visitor history (browsed / cart / purchased), the shop config (currency, money format, storefront GraphQL version, integrations, analytics enabled), and the storefront token.
Usage
Outside React, read the same value synchronously with
client.getInitData().useGloodInitReady
A React hook that reports whether the page-load headless init has settled — resolved, failed, or skipped (v2 / no client).useRecommendations waits on this internally so the init call lands before the first sections request; you can use it to gate your own init-dependent calls.
Signature
Returns
Usage
useRecommendations
A React hook that fetches recommendation sections client-side (in the browser). Prefer this for client-side rendering; usefetchRecommendationSections(options, params) in SSR route loaders instead.
Must be used within a GloodProvider whose client has the recommendations app registered.
Ordering: for a v3 client the hook waits for the page-load
/v3/headless/init call to settle before fetching sections (it stays in the
loading state until then). This guarantees init runs first and the sections
request carries the init-issued visitor id. For v2 clients there is no init,
so sections fetch immediately.Usage
Provider Placement
Correct Placement
TheGloodProvider must be placed correctly in your component tree:
Incorrect Placement
Debug Logging
Enable debug mode to see detailed component behavior:Performance Considerations
Memoization
The provider uses React’suseMemo to prevent unnecessary re-renders:
Event Deduplication
Events are subscribed to only once per event type, regardless of how many apps are interested:Lazy Loading
Analytics setup is deferred to prevent blocking:Error Scenarios
Missing Analytics
Network Errors
Consent Denial
Best Practices
1. Single Provider Instance
Use only oneGloodProvider at the root of your application:
2. Client Stability
Create the client outside of the component to prevent recreating:3. Conditional Hook Usage
Always check for null when using the hook:4. Error Boundaries
Wrap the provider in error boundaries for production:See Also
- Client API - createGlood() and GloodClient
- App Modules - App configuration and usage
- Event System - Event tracking and pixel transmission
- Examples - Complete working recommendations page