perf(storefront): defer PostHog until after window load#289
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.jstook 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) andposthog-recorder.js(61KB) load even when nothing is being recorded and no survey matches
Plan
- Move
posthog.initto afterwindow.loador on request-idle so it never runs inside the render/hydration window. - Lazy-load session recording and surveys only when enabled/needed instead of upfront addons.
- 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