Skip to content

page_viewed Analytics events not firing when expected #2475

@danhemerlein

Description

@danhemerlein

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't workingSEV-3analyticsIssues with analytics, dashboards, etc.inactive

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions