Skip to content

fix(analytics): fix cart analytics events silently dropped during initialization#3510

Draft
kdaviduik wants to merge 2 commits intomainfrom
02-25-fix_analytics_fix_cart_analytics_events_silently_dropped_during_initialization
Draft

fix(analytics): fix cart analytics events silently dropped during initialization#3510
kdaviduik wants to merge 2 commits intomainfrom
02-25-fix_analytics_fix_cart_analytics_events_silently_dropped_during_initialization

Conversation

@kdaviduik
Copy link
Contributor

Summary

Fixes cart analytics events (product_added_to_cart, cart_updated) being silently dropped since Hydrogen 2025.7.1. This affected every Hydrogen merchant using analytics — approximately 3 months of lost conversion data.

Root Cause

Three interacting bugs created a deterministic race condition:

  1. No-op publish gatepublish: canTrack() ? publish : () => {} in AnalyticsProvider evaluated canTrack() at useMemo time. Before the Customer Privacy SDK loaded, canTrack() returned false, making publish a no-op.

  2. CartAnalytics deduplication — Cart events fired into the no-op were marked as "sent" via lastEventId.current. When publish later became the real function, the dedup guard prevented re-emission.

  3. Map key collisionwaitForReadyQueue used a Map keyed by event name, silently overwriting duplicate event types.

Fix

  • Always pass the real publish function (consent is enforced at 3 downstream layers: register/ready queue timing, subscriber-level analyticsProcessingAllowed() check, and sendShopifyAnalytics hasUserConsent gate)
  • Change queue from Map to bounded Array (MAX=100) preserving all events in FIFO order
  • Reentrancy-safe drain loop with overflow protection
  • Remove dead analyticsLoaded state

Why page views were NOT affected

AnalyticsView has no deduplication guard — when publish changed from no-op to real function, page view events re-fired successfully. Cart events had the lastEventId ref that prevented this.

Closes #3467

Test plan

  • Added 2 new unit tests reproducing the race condition (TDD — tests fail on current main, pass with fix)
  • All 477 existing tests pass with zero regressions
  • E2E: Verify Monorail requests fire with correct product_added_to_cart events after adding to cart
  • Manual: Start skeleton dev server, add to cart, verify analytics in Network tab

…tialization

Cart analytics events (product_added_to_cart, cart_updated) were silently
dropped due to a race condition between consent initialization and cart
resolution introduced in the new cookie system (2025.7.1).

Three interacting bugs caused the issue:

1. The `publish` function in AnalyticsProvider was set to a no-op
   `() => {}` when `canTrack()` returned false during useMemo
   evaluation (before the Customer Privacy SDK loaded).

2. CartAnalytics deduplication (`lastEventId.current`) marked events
   as "sent" even when they went to the no-op, preventing re-emission
   when the real publish function became available.

3. The waitForReadyQueue used a Map keyed by event name, silently
   overwriting duplicate event types (e.g., two cart_updated events
   would lose the first).

The fix:
- Always pass the real `publish` function (consent is enforced at 3
  downstream layers: register/ready queue timing, subscriber-level
  analyticsProcessingAllowed() check, and sendShopifyAnalytics
  hasUserConsent gate)
- Change queue from Map to bounded Array (MAX=100) preserving all
  events in FIFO order with overflow protection
- Add reentrancy-safe drain loop
- Remove dead analyticsLoaded state

Closes #3467
@shopify
Copy link
Contributor

shopify bot commented Feb 26, 2026

Oxygen deployed a preview of your 02-25-fix_analytics_fix_cart_analytics_events_silently_dropped_during_initialization branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment February 26, 2026 9:37 PM

Learn more about Hydrogen's GitHub integration.

Patch bump for @Shopify/hydrogen to include the cart analytics event fix in the next release.
@kdaviduik
Copy link
Contributor Author

/snapit

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.

[Bug]: Add To Cart events not triggered [2025.7.1 and above]

1 participant