`shopify:page:view` fires when `history.replaceState` changes only the query, as a variant selection does

Open
#4,382 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

@Peerapatfc is already working on this.

Since Nov 18, 2026.

  • #4395 by @Peerapatfc — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
53/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
react, typescript
Domain
analytics, web-dev

Research direction

Start with packages/hydrogen/src/core/shopify-scripts/page-view.test.ts and compare its existing navigation tests with the provided query-only replaceState reproduction. Then read packages/hydrogen/src/core/shopify-scripts/page-view.ts and utils/navigation.ts to trace both observer paths. First determine the intended behavior from the conflicting docs and recipes; done means the behavior is documented and covered by tests for the relevant navigation cases.

Written by the indexing model from the issue text.

Description

What is the location of your example repository?

None. The test below reproduces it in page-view.test.ts.

Which package or tool is having this issue?

Hydrogen

What version of that package or tool are you using?

2026.10.3

What version of React Router 7 are you using?

Not applicable: the core entry's initializeShopifyScripts.

Steps to Reproduce
  1. On a page that has called initializeShopifyScripts(), listen for the standard event:
    document.addEventListener('shopify:page:view', (event) => console.log(event.page.url));
    
  2. Write a selected option into the URL in place, as a product form does for a variant selection:
    history.replaceState(history.state, '', '?Size=m');
    

As a test beside the others in packages/hydrogen/src/core/shopify-scripts/page-view.test.ts:

it("ignores a query replaced in place", async () => {
  initialize();
  await flushPageViews();
  pageViews.length = 0;

  window.history.replaceState({}, "", "/?Size=m");
  await flushPageViews();

  expect(pageViews).toHaveLength(0);
});
Expected Behavior

No page view, since the buyer is still on the same page. #3917 describes one shopify:page:view for the initial page and one for each SPA navigation, and #4376 notes that variant changes belong to shopify:product:select.

Actual Behavior

A shopify:page:view for ?Size=m on the next animation frame, in both of the observer's paths:

  • With the Navigation API, the observer listens to currententrychange, which history.replaceState fires too (navigation.ts L17–L22).
  • Without it, the observer patches history.replaceState (L24–L25).

Either way, page-view.ts counts any change of the path or the query (L48–L60). The existing tests cover a push, a currententrychange, and changes to the state or the hash only. None covers a query replaced in place.

So a product page that writes its selected variant into a bare URL after initializeShopifyScripts has run sends two page views for one visit, and one more for each selection after that. Tools that read Standard Events, such as PerfKit (#4376), see each of them as a new page.

Hydrogen's skills count a selection the same way in their React Router and Next.js recipes. There, PAGE_VIEWED is keyed by the path and the query, and the variant form writes the selection with a router replace. But the analytics skill's framework-neutral rule publishes PAGE_VIEWED "on each page transition".

This may be intended: the initializeShopifyScripts docs say it "sends a page view event on load and after each navigation that changes the path or query", and the Navigation API reports history.replaceState as a replace navigation. Possible directions, depending on the intended semantics:

  • Skip a replacement that keeps the path, which is how storefronts write UI state into the URL, and key the recipes' PAGE_VIEWED the same way. Keep counting pushes, traversals and replacements to another path, such as a redirect.
  • Let the storefront decide: an option to initializeShopifyScripts that turns off the automatic event, and a function that dispatches it from the same route boundary that publishes PAGE_VIEWED.
  • If every change of the path or the query should count, say so in the docs and in the analytics skill's framework-neutral rule.
Dominant language
TypeScript
Stars
2.2k
Forks
466
Avg merge
3d 18h
Merged PRs (30d)
59

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Shopify/hydrogen

All issues in Shopify/hydrogen

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.