Skip to main content

Basic Setup Example

This example shows how to set up the Glood Hydrogen SDK with minimal configuration for a typical e-commerce store.

Complete Implementation

1. Environment Variables

2. Create the Glood Client (app/lib/glood.ts)

Create the client once and register the recommendations app. The apiKey is a public storefront token (gl_sf_*), so it is safe to ship to the browser.

3. Root Layout (app/root.tsx)

Wrap your app with GloodProvider inside Shopify’s Analytics.Provider and pass the root loader data. GloodProvider subscribes to Shopify’s analytics events and, on a v3 client, automatically fires POST /v3/headless/init on mount to bootstrap the session (see below).
GloodProvider must render inside Shopify’s Analytics.Provider so it can subscribe to the storefront’s analytics events.

What This Setup Provides

✅ Automatic Session Init (v3)

On page load, a v3 client automatically calls POST /api/storefront/v3/headless/init via GloodProvider. This bootstrap:
  • replays a persisted visitor id and returns/echoes user_id (stored in the _glood_user_id cookie so returning visitors are recognized),
  • creates a per-visit visit_id (_glood_visit_id, a session cookie),
  • returns the visitor’s browsed / cart / purchased history, shop config (currency, money format, GraphQL version, integrations, analytics flag) and a storefront token.
The init response is available anywhere via the useGloodInit() hook. For a v2 client, init is a no-op (the endpoint is v3-only) and useGloodInit() returns null.

✅ Automatic Event Tracking

Once wrapped, GloodProvider automatically tracks Shopify analytics events (page views, product views, collection views, cart views, add/remove from cart) and forwards them to the recommendations app.

✅ Default Endpoint

  • Recommendations: https://storefront.glood.ai — the v3 headless suite (init, sections, events, recommendations) lives under this endpoint.

✅ Debug Logging

In development mode (debug: true), you’ll see console logs like:

Testing the Setup

1. Verify Installation

Check that the SDK is working by looking for debug logs in the browser console.

2. Verify Session Init

On first load, confirm a POST /api/storefront/v3/headless/init request succeeds, and that the _glood_user_id and _glood_visit_id cookies are set.

3. Test Event Tracking

  1. Navigate to a product page – should see product_viewed events
  2. Add item to cart – should see product_added_to_cart events
  3. View cart – should see cart_viewed events

Customizing the Basic Setup

Add Custom Settings

Environment-Specific Debug

Using the Glood Client

Access the Init Response

Access the Client in Components

Common Issues and Solutions

Issue: No Debug Logs

Check:
  1. Debug mode is enabled: debug: true
  2. Browser console is open
  3. Events are being triggered (navigate to product pages)

Issue: CSP Violations

Check:
  1. https://storefront.glood.ai is in connectSrc (see Content Security Policy)
  2. CSP headers are being applied correctly
  3. No typos in domain names

Issue: No Network Requests

Check:
  1. Customer consent is granted (try allowing all cookies)
  2. The recommendations app is registered with .use(recommendations())
  3. GloodProvider renders inside Analytics.Provider

Issue: TypeScript Errors

Solution:

Next Steps

Support

If you need help with the basic setup: