perf(storefront): defer PostHog until after window load#289

openedAug 25, 2026byAAviddaram

What happened

PostHog loads eagerly in the head on every page and competes with app hydration:

  • 197KB transferred across 8 requests, all to us-assets.i.posthog.com (US East), so Mongolian visitors pay a transpacific round trip during load
  • array.js took 1,251ms wall time on an unthrottled run from Ulaanbaatar
  • 186ms of main-thread time attributed to posthog.com, including a 199ms long task during the homepage LCP window
  • surveys.js (33KB) and posthog-recorder.js (61KB) load even when nothing is being recorded and no survey matches

Plan

  1. Move posthog.init to after window.load or on request-idle so it never runs inside the render/hydration window.
  2. Lazy-load session recording and surveys only when enabled/needed instead of upfront addons.
  3. Keep exception autocapture working; verify events still flow after the change.

Acceptance criteria

  • No posthog.com network requests before the load event on a cold page visit
  • No long tasks attributed to posthog during the initial render window
  • Events, exceptions, and web-vitals still captured and visible in PostHog