hydrogen icon indicating copy to clipboard operation
hydrogen copied to clipboard

Analytics subscriptions stop firing if you go from checkout to store via back button

Open jeremyagabriel opened this issue 7 months ago • 2 comments

What is the location of your example repository?

https://github.com/Shopify/hydrogen-demo-store

Which package or tool is having this issue?

Hydrogen

What version of that package or tool are you using?

2025.1.1

What version of Remix are you using?

2.15.3

Steps to Reproduce

  1. Clone Hydrogen demo store, run npm i in terminal, etc
  2. In PageLayout.tsx add the following code:
  import {AnalyticsEvent, useAnalytics} from '@shopify/hydrogen';

  ...

  const {register, subscribe} = useAnalytics();
  const ANALYTICS_NAME = 'CustomAnalytics';
  const ready = register(ANALYTICS_NAME).ready;

  useEffect(() => {
    subscribe(AnalyticsEvent.PAGE_VIEWED, (data) => {
      console.log(
        `${ANALYTICS_NAME}: 📥 subscribed to analytics for \`${AnalyticsEvent.PAGE_VIEWED}\`:`,
        data,
      );
    });
    subscribe(AnalyticsEvent.PRODUCT_ADD_TO_CART, (data) => {
      console.log(
        `${ANALYTICS_NAME}: 📥 subscribed to analytics for \`${AnalyticsEvent.PRODUCT_ADD_TO_CART}\`:`,
        data,
      );
    });

    ready();
    console.log(`${ANALYTICS_NAME}: 🔄 subscriptions are ready.`);
  }, []);
  1. Run npm run dev in terminal
  2. In localhost:3000 in browser while incognito, observe the PAGE_VIEWED analytics subscription log in the console
  3. Add a product to the cart and observe the PRODUCT_ADD_TO_CART analytics subscription log in the console
  4. Navigate to checkout
  5. At checkout, navigate back to the Hydrogen store via the browser's back button
  6. Observe that analytics subscriptions no longer trigger and there are no logs in the console. The console log after ready() in the useEffect will likely be the only thing logging

Expected Behavior

Upon navigating back to the Hydrogen store from checkout via the back button, analytics subscriptions should fire as expected

Actual Behavior

Analytics subscriptions cease to fire after navigating back to the store from the checkout via the back button. All subscription events no longer fire for the entire session in the browser's tab, meaning refreshing the page, or navigating to other pages, does not fix the subscriptions. Only starting a new session (e.g. new tab) will see the subscriptions work again.

jeremyagabriel avatar May 17 '25 00:05 jeremyagabriel