fix(analytics): run heatmap view mode in a window Studio opened

Merged
TH
6d ago
+22-11

Why

This keeps the analytics copy in sync with decocms/analytics#77. Some commerce platforms fix a store's frame-ancestors to the store's own domain, so Studio's Heatmap tab can never frame those stores. Studio now opens the store in a window instead, and the view chunk has to talk to the window that opened it.

What changes

  • runtime/src/index.ts: view mode starts when the URL has dq_view=1 and the page is framed or has an opener.
  • runtime/view/view.ts: the parent is window.parent when framed, else window.opener. A frame keeps the *.decocms.com allow-list. An opener must be Studio itself (studio.decocms.com, studio-stg.decocms.com or localhost): a window skips the store's frame-ancestors and carries the visitor's cookies, and the wildcard includes sandbox previews.

Until decocms/analytics builds the tag from this package (task 3.7), its runtime/ is what production serves. That is why the change lands in both copies.

Verification

  • node ops/build-tag.mjs --check --src <this repo>/packages/blocks/src/v8/analytics, run from decocms/analytics#77: "matches worker/tag.lock.json (10 bundles)".
  • bun run typecheck passed on every package.
  • biome check on src/v8/analytics/runtime is clean.
  • npx vitest run packages/blocks/src/v8/analytics: 48 passed.

Summary by cubic

Fixes analytics heatmap view mode to work on stores that can't be framed by Studio. Some commerce platforms fix a store's frame-ancestors to its own domain, so Studio now opens those stores in a window, and the heatmap view chunk must talk to the window that opened it. View mode starts when the URL carries dq_view=1 and the page is framed or has an opener; the parent is window.parent when framed, else window.opener. Because a window carries the visitor's cookies and skips the store's frame-ancestors, windows accept only Studio itself (studio.decocms.com, studio-stg.decocms.com, localhost) as opener, while frames keep the existing *.decocms.com allow-list. Mirrors decocms/analytics#77 so both copies build to the same bytes.