Skip to main content

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:
  1. GloodProvider mounts — subscribes to Shopify analytics and, for a v3 client, calls /v3/headless/init once. (v2 clients skip init.)
  2. useGloodInit() — returns the init response once it resolves (null until then, and always null for v2). Also readable synchronously via client.getInitData().
  3. useGloodInitReady() — flips to true when init settles (or immediately for v2 / no client). This is the gate the next step waits on.
  4. useRecommendations(params) — holds in loading until useGloodInitReady() is true, then fetches /v3/headless/sections. The request carries the init-issued user_id.
  5. useGloodAnalytics() + trackRender/View/Click/AddToCart — fire attribution events (through the recommendations app) to /v3/headless/events as 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

The GloodProvider component:
  1. Creates React Context - Provides the Glood client to all child components
  2. Fires Headless Init - On mount, a v3 client calls POST /api/storefront/v3/headless/init once to bootstrap the session. The response is exposed via useGloodInit(). For a v2 client this is a no-op.
  3. Subscribes to Analytics - Uses Hydrogen’s useAnalytics() hook to receive events
  4. Distributes Events - Routes events to the recommendations app based on its subscriptions
  5. Checks Consent - Verifies customer privacy permissions before sending pixels
  6. 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; use fetchRecommendationSections(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.
Full signature, parameters, and return fields are documented in the Recommendations API.

Usage

Provider Placement

Correct Placement

The GloodProvider must be placed correctly in your component tree:

Incorrect Placement

Debug Logging

Enable debug mode to see detailed component behavior:
Debug logs include:

Performance Considerations

Memoization

The provider uses React’s useMemo 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

Best Practices

1. Single Provider Instance

Use only one GloodProvider 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