React Components
The SDK provides React components and hooks for seamless integration with your Hydrogen application.GloodProvider
A React context provider that automatically subscribes to Shopify Analytics events and manages pixel tracking across all registered Glood apps.Signature
Props
Prop | Type | Required | Description |
---|---|---|---|
client | GloodClient | Yes | Glood client instance created with createGlood() |
children | React.ReactNode | Yes | Child components to wrap |
Basic Usage
How It Works
TheGloodProvider
component:
- Creates React Context - Provides the Glood client to all child components
- Subscribes to Analytics - Uses Hydrogen’s
useAnalytics()
hook to receive events - Distributes Events - Routes events to interested apps based on their subscriptions
- Checks Consent - Verifies customer privacy permissions before sending pixels
- Handles Errors - Provides comprehensive error handling and debug logging
Event Subscription Flow
Error Handling
The provider includes comprehensive error handling:Server-Side Rendering
The provider automatically handles SSR:Context Integration
The provider creates a React Context to share the client:useGloodAnalytics
A React hook that provides access to the Glood client from context.Signature
Returns
Type | Description |
---|---|
GloodClient | null | The Glood client instance or null if used outside provider |
Usage
Custom Integrations
Use the hook for custom integrations with Glood apps:Conditional Rendering
Use the hook for conditional rendering based on Glood availability: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 - Context usage patterns