Skip to content

An hydrogen analytics pkg#3728

Draft
andguy95 wants to merge 3 commits intomainfrom
an-hydrogen-analytics-pkg
Draft

An hydrogen analytics pkg#3728
andguy95 wants to merge 3 commits intomainfrom
an-hydrogen-analytics-pkg

Conversation

@andguy95
Copy link
Copy Markdown
Collaborator

WHY are these changes introduced?

Hydrogen's analytics layer is currently tightly coupled to React via <Analytics.Provider>. This blocks adoption by non-React frameworks and creates unnecessary dependency for what is fundamentally a pub/sub + consent + Monorail dispatch pipeline. Extracting a framework-agnostic package unlocks headless analytics for any JavaScript storefront.

WHAT is this pull request doing?

Introduces @shopify/hydrogen-analytics — a new, zero-dependency package that extracts the analytics event bus out of @shopify/hydrogen into a standalone, framework-agnostic module.

New package (packages/hydrogen-analytics/):

  • Event bus (createAnalyticsBus) — isolated pub/sub with register/ready gating, consent-aware publish, and cleanup via destroy()
  • Cart tracking — automatic line-item diffing via updateCart() that emits granular product_added_to_cart / product_removed_from_cart events
  • Consent management — intercepts Shopify's privacy SDK (window.Shopify.customerPrivacy) to inject headless storefront configuration before the SDK initializes
  • Shopify analytics dispatch — page view and e-commerce events sent to Monorail with proper domain, consent, and session handling
  • Deprecated cookie bridge — backward-compatible _shopify_y / _shopify_s cookie management (marked for removal)
  • PerfKit integration — web vitals reporting gated behind consent collection
  • ~1,400 lines of tests across 5 test files covering the bus, consent, deprecated cookies, tracking values, and analytics dispatch

HOW to test your changes?

  1. pnpm install from the repo root
  2. cd packages/hydrogen-analytics && pnpm test — runs the full unit test suite
  3. pnpm build from the package to verify the tsup build produces valid ESM output
  4. To verify the bus in a browser context you can stub out another templates/commerce template (if available) or create a minimal HTML page that imports createAnalyticsBus and publishes a page_viewed event — confirm Monorail requests fire in the Network tab

Checklist

  • I've read the Contributing Guidelines
  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've added a changeset if this PR contains user-facing or functional changes. Test changes or internal-only config changes do not require a changeset.
  • I've added tests to cover my changes
  • I've added or updated the documentation

@shopify
Copy link
Copy Markdown
Contributor

shopify Bot commented Apr 17, 2026

Oxygen deployed a preview of your an-hydrogen-analytics-pkg branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment April 17, 2026 3:16 AM

Learn more about Hydrogen's GitHub integration.

@fredericoo
Copy link
Copy Markdown
Contributor

So we’d need a server/proxy doing the server timings to pass it down because today it works like:

  1. Browser → Hydrogen server → Shopify Storefront API
  2. Shopify's API sets _shopify_y / _shopify_s as HTTP-only cookies on the response
  3. The Hydrogen server reads those cookies and injects them into Server-Timing response headers going back to the browser
  4. Browser-side JS reads Server-Timing entries via the Performance API
  5. Those tokens populate every Monorail event payload

It doesn’t have to be server timings, but we need to make the tokens available client-side, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants