Skip to content

fix(website): bundle the PostHog init so analytics actually loads#5449

Merged
mmabrouk merged 1 commit into
mainfrom
fix/website-posthog-bundled-init
Jul 22, 2026
Merged

fix(website): bundle the PostHog init so analytics actually loads#5449
mmabrouk merged 1 commit into
mainfrom
fix/website-posthog-bundled-init

Conversation

@mmabrouk

Copy link
Copy Markdown
Member

What

Analytics never initialized on the live site: the init script was emitted inline (via define:vars), and an inline script's import("posthog-js") is a bare module specifier the browser cannot resolve — it threw an unhandled promise rejection on every page load. Zero events reached PostHog.

Fix

Make the script a bundled Astro client script. Vite statically replaces the PUBLIC_POSTHOG_KEY / PUBLIC_NOINDEX reads in bundled client code, so the inline value handoff was never needed. The host guard (agenta.ai / www only) and DNT handling are unchanged. Verified: the built Analytics chunk contains posthog-js and pages reference it; live event delivery confirmed on agenta.ai after a direct production deploy.

https://claude.ai/code/session_013Qe1Vf2yvj33BVd5W1q7HB

@vercel

vercel Bot commented Jul 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agenta-documentation Ready Ready Preview, Comment Jul 22, 2026 3:20pm

Request Review

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. Frontend labels Jul 22, 2026
…oads

define:vars made the script inline, where import("posthog-js") is a bare
specifier the browser cannot resolve — analytics never initialized. Bundled
client scripts get PUBLIC_* env statically replaced, so no value handoff is
needed.

Claude-Session: https://claude.ai/code/session_013Qe1Vf2yvj33BVd5W1q7HB
@mmabrouk
mmabrouk force-pushed the fix/website-posthog-bundled-init branch from c25ccc3 to 75af427 Compare July 22, 2026 15:19
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Jul 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Website preview

Preview URL: https://pr-5449-agenta-website-preview.mahmoud-637.workers.dev

Built from 75af427b8c27f9669b3edc1fefa7a677b7965ee9. This comment updates in place on every push.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: af1d4a9a-d833-4179-8f00-517f5735e82f

📥 Commits

Reviewing files that changed from the base of the PR and between 7b38504 and 75af427.

📒 Files selected for processing (1)
  • website/src/components/Analytics.astro

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Improved analytics initialization reliability in supported browsers.
    • Preserved existing privacy controls, including Do Not Track respect, no-index handling, and identified-only profiles.
    • Maintained existing pageview, pageleave, and autocapture behavior.

Walkthrough

Changes

PostHog client bootstrap

Layer / File(s) Summary
Bundle and initialize PostHog
website/src/components/Analytics.astro
The client script reads PostHog environment values via import.meta.env, uses the bundled posthog-js import, and directly calls posthog.init with the existing guarded configuration.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: bundling the PostHog init so analytics loads correctly.
Description check ✅ Passed The description directly matches the change and explains the inline-script bundling fix and its effect.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/website-posthog-bundled-init

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@mmabrouk
mmabrouk merged commit e3a6a34 into main Jul 22, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Frontend size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants