Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

page_viewed Analytics events not firing when expected #2475

Open
danhemerlein opened this issue Aug 27, 2024 · 2 comments
Open

page_viewed Analytics events not firing when expected #2475

danhemerlein opened this issue Aug 27, 2024 · 2 comments
Labels
analytics Issues with analytics, dashboards, etc. Bug Something isn't working SEV-3

Comments

@danhemerlein
Copy link

danhemerlein commented Aug 27, 2024

What is the location of your example repository?

You can create a new Hydrogen project and it will have this issue. Please see the Steps to Reproduce.

Which package or tool is having this issue?

Hydrogen

What version of that package or tool are you using?

2024.7.4

What version of Remix are you using?

2.10.1

Steps to Reproduce

  1. npm create @shopify/hydrogen@latest -- --quickstart
  2. cd hydrogen-quickstart && npm run dev
  3. add the following code to a new file:app/components/ThirdPartyAnalyticsIntegration.tsx
import {useAnalytics} from '@shopify/hydrogen';
import {useEffect} from 'react';

export function ThirdPartyAnalyticsIntegration() {
  const {subscribe, register} = useAnalytics();
  const {ready} = register('Third Party Analytics Integration');

  useEffect(() => {
    subscribe('page_viewed', (data) => {
      console.log('ThirdPartyAnalyticsIntegration - Page viewed:', data);
    });

    ready();
  }, []);

  return null;
}
  1. Invoke ThirdPartyAnalyticsIntegration in app/root.tsx like so:
<Analytics.Provider
     cart={data.cart}
     shop={data.shop}
     consent={data.consent}
>
     <PageLayout {...data}>
          {children}
          <ThirdPartyAnalyticsIntegration />
     </PageLayout>
</Analytics.Provider>
  1. View the site in a new window or tab.

I want to note that I followed these docs to link a Shopify store. So I'm not using mock.shop

notes on my environment:
Node - v21.7.3
npm - 10.5.0

Expected Behavior

The page_viewed event should fire and call its callback when the page is viewed for the first time in a new tab or window.

Actual Behavior

Open browser console, the trace statement from the page_viewed callback does not appear on hard refresh, but does appear on soft refresh and normal page navigation. The page_viewed does not fire when the site is viewed from a new tab or window. However, the initial page view event will fire when you navigate to another page. This behavior is demonstrated in this video:

https://share.zight.com/QwuJYpGR

Additional Context:

Based on our experience with when this surfaced on our Hydrogen project, and in testing it on different versions of Hydrogen, we believe it might have come from the 2024.4.4 release. Looking at those release notes, we're wondering if it might have been unintentionally introduced in PR #2224. That said, while we see analytics-related changes, we aren't seeing any obvious changes to the page_viewed event. We could definitely be wrong, but just thought we'd share that in case it helps you track it down!

Happy to answer any questions. Thank you for any and all help when looking into this!

@scottdixon
Copy link
Contributor

Thanks for reporting this @danhemerlein. The screen recording was super helpful.

Can you confirm env.PUBLIC_CHECKOUT_DOMAIN is set?

I'm able to replicate inconsistencies when env.PUBLIC_CHECKOUT_DOMAIN isn't set. Feels like a race condition (can only consistently replicate when throttling a hard refresh). Always fires when navigating regardless of env var.

cc) @wizardlyhel

@scottdixon scottdixon added Bug Something isn't working analytics Issues with analytics, dashboards, etc. SEV-3 labels Sep 2, 2024
@danhemerlein
Copy link
Author

danhemerlein commented Sep 3, 2024

hey @scottdixon, thanks very much for looking into this. I did not have env.PUBLIC_CHECKOUT_DOMAIN set and, you're right, setting that variable does alleviate the bug in most scenarios.

However, even with the env var now set, it still seems like the callback or event are not running on the very first time the site is loaded in a new tab or window. The site exhibits the same behavior from the screen recording where there's no console.log on initial page load but both page view events do run once the site is navigated once or refreshed. I agree that it feels like a race condition.

Here's a screen recording of the behavior I'm seeing now that I've set the variable
https://s.chord.co/X6ujDd71

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analytics Issues with analytics, dashboards, etc. Bug Something isn't working SEV-3
Projects
None yet
Development

No branches or pull requests

2 participants