Refactor PostHog script to use environment variables - #49
Conversation
Updated PostHog initialization to use environment variables for configuration.
PR Review: Refactor PostHog script to use environment variablesSummaryThis PR updates the PostHog initialization to properly use Astro's ✅ Positive Changes
|
There was a problem hiding this comment.
Pull request overview
Updates the landing page’s PostHog initialization to inject configuration via Astro define:vars (instead of referencing import.meta.env directly in a raw inline script), and adjusts init options.
Changes:
- Refactors
posthog.astroto pass env values into an inline script usingdefine:vars. - Updates
posthog.init(...)options, including addingperson_profiles: "always".
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| posthog.init(POSTHOG_KEY, { | ||
| api_host: POSTHOG_HOST, | ||
| defaults: POSTHOG_DEFAULTS, | ||
| person_profiles: "always", | ||
| }); |
There was a problem hiding this comment.
posthog.init runs unconditionally. If PUBLIC_POSTHOG_API_KEY / PUBLIC_POSTHOG_HOST are unset (the .env.example leaves them blank), this will generate a broken script URL like undefined/static/array.js and may throw at runtime. Consider guarding initialization (and the snippet loader) behind a check that both values are non-empty, or provide safe defaults / a no-op in non-production builds.
| posthog.init(POSTHOG_KEY, { | |
| api_host: POSTHOG_HOST, | |
| defaults: POSTHOG_DEFAULTS, | |
| person_profiles: "always", | |
| }); | |
| if (POSTHOG_KEY && POSTHOG_HOST) { | |
| posthog.init(POSTHOG_KEY, { | |
| api_host: POSTHOG_HOST, | |
| defaults: POSTHOG_DEFAULTS, | |
| person_profiles: "always", | |
| }); | |
| } else { | |
| console.warn( | |
| "PostHog not initialized: PUBLIC_POSTHOG_API_KEY or PUBLIC_POSTHOG_HOST is missing.", | |
| ); | |
| } |
| posthog.init(POSTHOG_KEY, { | ||
| api_host: POSTHOG_HOST, | ||
| defaults: POSTHOG_DEFAULTS, | ||
| person_profiles: "always", | ||
| }); |
There was a problem hiding this comment.
POSTHOG_DEFAULTS is typed as a string (see src/env.d.ts), but it’s passed directly as defaults in the PostHog init options. If this env var is intended to represent structured defaults (e.g., JSON), it should be parsed/validated before being passed; otherwise, consider removing the defaults option to avoid silently misconfiguring PostHog.
| posthog.init(POSTHOG_KEY, { | |
| api_host: POSTHOG_HOST, | |
| defaults: POSTHOG_DEFAULTS, | |
| person_profiles: "always", | |
| }); | |
| let parsedDefaults = undefined; | |
| if (typeof POSTHOG_DEFAULTS === "string" && POSTHOG_DEFAULTS.trim() !== "") { | |
| try { | |
| const maybeDefaults = JSON.parse(POSTHOG_DEFAULTS); | |
| if (maybeDefaults && typeof maybeDefaults === "object") { | |
| parsedDefaults = maybeDefaults; | |
| } | |
| } catch (err) { | |
| // If parsing fails, ignore POSTHOG_DEFAULTS to avoid misconfiguration | |
| parsedDefaults = undefined; | |
| } | |
| } | |
| const posthogInitOptions = { | |
| api_host: POSTHOG_HOST, | |
| person_profiles: "always", | |
| }; | |
| if (parsedDefaults !== undefined) { | |
| posthogInitOptions.defaults = parsedDefaults; | |
| } | |
| posthog.init(POSTHOG_KEY, posthogInitOptions); |
| defaults: POSTHOG_DEFAULTS, | ||
| person_profiles: "always", | ||
| }); |
There was a problem hiding this comment.
Setting person_profiles: "always" changes data collection behavior compared to the previous config and may have privacy/compliance implications (e.g., collecting person properties even when not identifying). If this isn’t explicitly desired for all environments, consider gating it behind an environment variable or removing it.
Description
Updated PostHog initialization to use environment variables for configuration.
Type of Change
Checklist
Release Notes
Labels