hydrogen
hydrogen copied to clipboard
Analytics subscriptions stop firing if you go from checkout to store via back button
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
- Clone Hydrogen demo store, run
npm iin terminal, etc - In
PageLayout.tsxadd 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.`);
}, []);
- Run
npm run devin terminal - In
localhost:3000in browser while incognito, observe thePAGE_VIEWEDanalytics subscription log in the console - Add a product to the cart and observe the
PRODUCT_ADD_TO_CARTanalytics subscription log in the console - Navigate to checkout
- At checkout, navigate back to the Hydrogen store via the browser's back button
- Observe that analytics subscriptions no longer trigger and there are no logs in the console. The console log after
ready()in theuseEffectwill 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.