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

Aperta
#4,382 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

@Peerapatfc ci sta già lavorando.

Dal 18/11/2026.

  • #4395 di @Peerapatfc — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
53/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
react, typescript
Ambito
analytics, web-dev

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.
Lingua principale
TypeScript
Stelle
2.2k
Fork
466
Merge medio
3g 18h
PR unite (30g)
59

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Shopify/hydrogen

Tutte le issue di Shopify/hydrogen

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.