Skip to content

Defer site analytics initialization - #882

Merged
zahlekhan merged 3 commits into
thesysdev:mainfrom
zahlekhan:codex/defer-site-analytics
Jul 28, 2026
Merged

Defer site analytics initialization#882
zahlekhan merged 3 commits into
thesysdev:mainfrom
zahlekhan:codex/defer-site-analytics

Conversation

@zahlekhan

@zahlekhan zahlekhan commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Summary

  • load Google Analytics with Next.js Script and strategy="lazyOnload"
  • dynamically load PostHog's slim client during browser idle, with a 5-second idle timeout
  • centralize PostHog initialization and event capture behind one lib/analytics.ts facade
  • remove the document-wide link-attribution MutationObserver
  • decorate existing links once and refresh only the link being interacted with
  • lazily load PostHog when capturing create-CLI copy events

Why

PostHog, Google Analytics, session recording, and a body-wide observer were initialized on the critical path. This competed with the first pointer interaction and contributed to the reported 576 ms interaction delay.

The original version of this PR used custom first-interaction listeners, an animation frame, idle callbacks, and a 30-second fallback. The same intent is now expressed with Next.js's built-in script strategy for GA and one idle callback for PostHog.

Impact

This is the analytics slice of #879. In the prior combined optimized build, Total Blocking Time fell from 830 ms to 10 ms and max potential input delay fell from 660 ms to 70 ms.

Validation

  • pnpm --dir docs types:check
  • focused ESLint
  • Prettier and staged diff integrity checks

Analytics behavior change

The slim client disables session recording, surveys, external dependency loading, and feature-flag loading. Pageview and explicit event capture remain enabled. GA loads after the page's other resources during browser idle, and PostHog initializes during an idle period with a 5-second timeout.

Supersedes the analytics portion of #879.

@vishxrad
vishxrad marked this pull request as ready for review July 28, 2026 09:26
@zahlekhan
zahlekhan merged commit c812066 into thesysdev:main Jul 28, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants