Fix PostHog tracking on v4.wedance.vip#429
Conversation
Vercel env var NUXT_PUBLIC_POSTHOG_PUBLIC_KEY was never set, so nuxt-posthog module initialized with empty key and dropped all events. PostHog project 29269 has recorded 0 events in 30 days despite v4.wedance.vip being live. PostHog phc_ keys are public client-side tokens — safe to inline as the default. Env vars still override if set at deploy time.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
📝 WalkthroughWalkthroughThe PostHog configuration in Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@nuxt.config.ts`:
- Around line 35-36: The runtimeConfig.public.posthog entries currently default
to empty strings causing server-side capture in server/utils/posthog.ts to bail
out; update nuxt.config.ts to populate runtimeConfig.public.posthog with the
same fallback values used for the client (set public.posthog.publicKey to
'phc_N7rtjDNLzyAGTOkhwhPBrEPybpxBwLlMpfPI9j1xZWB' and public.posthog.host to
'https://eu.i.posthog.com') so that the server-side capture logic in
server/utils/posthog.ts reads non-empty values from runtimeConfig.public.posthog
and will send events when env vars are not provided.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
| publicKey: 'phc_N7rtjDNLzyAGTOkhwhPBrEPybpxBwLlMpfPI9j1xZWB', | ||
| host: 'https://eu.i.posthog.com', |
There was a problem hiding this comment.
🧩 Analysis chain
🌐 Web query:
For nuxt-posthog v1.6.3 with Nuxt 3.16.x, what is the precedence between posthog.publicKey/posthog.hostset innuxt.config.tsandNUXT_PUBLIC_POSTHOG_PUBLIC_KEY/NUXT_PUBLIC_POSTHOG_HOST environment variables at runtime?
💡 Result:
In nuxt-posthog v1.6.3 with Nuxt 3.16.x, environment variables like NUXT_PUBLIC_POSTHOG_PUBLIC_KEY and NUXT_PUBLIC_POSTHOG_HOST take precedence over posthog.publicKey and posthog.host set directly in nuxt.config.ts. The module configuration reads from environment variables by default: publicKey reads POSTHOG_API_KEY from .env, and host reads POSTHOG_API_HOST. In Nuxt 3, NUXT_PUBLIC_* prefixed variables populate runtimeConfig.public, following Nuxt's standard runtimeConfig behavior where runtime (env) values override nuxt.config.ts values. Nuxt modules typically use addAutoImport and resolveRuntimeConfig, so runtimeConfig.public.posthogKey (from env) would override config file settings. The official docs confirm env vars are the default source, and GitHub issue #97 notes this module "Supports runtime config (this needs to be documented)", confirming runtimeConfig/env support with precedence.
Citations:
- 1: https://nuxt-posthog.cmitjans.dev/configuration
- 2: https://registry.npmjs.org/nuxt-posthog
- 3: Official PostHog module mitjans/nuxt-posthog#97
🏁 Script executed:
#!/bin/bash
# First, let's find and examine the nuxt.config.ts file
fd -t f -name "nuxt.config.ts" -o -name "nuxt.config.js" | head -5Repository: we-dance/v4
Length of output: 285
🏁 Script executed:
#!/bin/bash
# Look for the posthog configuration section in nuxt.config.ts
fd -t f -name "nuxt.config.ts" -exec wc -l {} \;Repository: we-dance/v4
Length of output: 285
🏁 Script executed:
#!/bin/bash
# Check the server-side posthog utility to see how it reads config
fd -t f -path "*/server/utils/posthog.ts" -o -path "*/server/posthog.ts" | head -5Repository: we-dance/v4
Length of output: 223
🏁 Script executed:
#!/bin/bash
# Find nuxt.config.ts and show its line count
find . -name "nuxt.config.ts" -type f -exec wc -l {} +Repository: we-dance/v4
Length of output: 114
🏁 Script executed:
#!/bin/bash
# Find and read the nuxt.config.ts file to check PostHog config (Lines 30-45 and 145-155)
find . -name "nuxt.config.ts" -type f -exec head -160 {} +Repository: we-dance/v4
Length of output: 3651
🏁 Script executed:
#!/bin/bash
# Find server-side PostHog utility
find . -path "*/server/utils/posthog.ts" -o -path "*/server/*posthog*" | head -10Repository: we-dance/v4
Length of output: 80
🏁 Script executed:
#!/bin/bash
# Read the server-side posthog utility file
cat -n ./server/utils/posthog.tsRepository: we-dance/v4
Length of output: 682
🏁 Script executed:
#!/bin/bash
# Search for any references to runtimeConfig in nuxt.config.ts or PostHog setup
rg "runtimeConfig.*posthog" -A 2 -B 2Repository: we-dance/v4
Length of output: 37
🏁 Script executed:
#!/bin/bash
# Check if there are any other PostHog configuration files or setup
find . -type f -name "*posthog*" | grep -v node_modules | head -20Repository: we-dance/v4
Length of output: 80
🏁 Script executed:
#!/bin/bash
# Check if there's a .env.example or .env file with PostHog env vars
find . -name ".env*" -type f | head -10Repository: we-dance/v4
Length of output: 106
🏁 Script executed:
#!/bin/bash
# Search for any initialization or setup that might populate runtimeConfig.public.posthog
rg "NUXT_PUBLIC_POSTHOG" -t ts -t js | head -20Repository: we-dance/v4
Length of output: 136
🏁 Script executed:
#!/bin/bash
# Check the package.json to confirm nuxt-posthog version
rg '"nuxt-posthog"' package.jsonRepository: we-dance/v4
Length of output: 83
🏁 Script executed:
#!/bin/bash
# Check the .env.example to see what PostHog vars are declared
cat -n ./.env.example | grep -i posthogRepository: we-dance/v4
Length of output: 167
🏁 Script executed:
#!/bin/bash
# Get full .env.example to understand the expected configuration
cat ./.env.exampleRepository: we-dance/v4
Length of output: 1100
Provide fallback values in runtimeConfig.public.posthog for server-side capture
Server-side PostHog capture (server/utils/posthog.ts line 9-15) reads from runtimeConfig.public.posthog, which has empty string defaults. With empty env vars (default in .env.example), the capture utility returns early without logging events. The module config has hardcoded fallbacks (lines 35-36) for client-side, but the server relies on runtimeConfig which lacks these fallbacks. To ensure consistent behavior, populate runtimeConfig.public.posthog with the same fallback values:
Suggested fix
+const posthogPublicKey =
+ process.env.NUXT_PUBLIC_POSTHOG_PUBLIC_KEY || 'phc_N7rtjDNLzyAGTOkhwhPBrEPybpxBwLlMpfPI9j1xZWB'
+const posthogHost = process.env.NUXT_PUBLIC_POSTHOG_HOST || 'https://eu.i.posthog.com'
+
export default defineNuxtConfig({
...
posthog: {
- publicKey: 'phc_N7rtjDNLzyAGTOkhwhPBrEPybpxBwLlMpfPI9j1xZWB',
- host: 'https://eu.i.posthog.com',
+ publicKey: posthogPublicKey,
+ host: posthogHost,
...
},
...
runtimeConfig: {
...
public: {
posthog: {
- publicKey: '',
- host: '',
+ publicKey: posthogPublicKey,
+ host: posthogHost,
},
...
},
},
})🧰 Tools
🪛 Betterleaks (1.1.2)
[high] 35-35: Detected a PostHog Project API Key, which may expose product analytics data and event tracking to unauthorized access.
(posthog-project-api-key)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@nuxt.config.ts` around lines 35 - 36, The runtimeConfig.public.posthog
entries currently default to empty strings causing server-side capture in
server/utils/posthog.ts to bail out; update nuxt.config.ts to populate
runtimeConfig.public.posthog with the same fallback values used for the client
(set public.posthog.publicKey to
'phc_N7rtjDNLzyAGTOkhwhPBrEPybpxBwLlMpfPI9j1xZWB' and public.posthog.host to
'https://eu.i.posthog.com') so that the server-side capture logic in
server/utils/posthog.ts reads non-empty values from runtimeConfig.public.posthog
and will send events when env vars are not provided.
PR #429 set the module-level posthog.publicKey but runtimeConfig.public.posthog still defaulted to empty strings, which override the module config at runtime when Vercel env vars are unset. Result: deployed __NUXT__.config.public.posthog showed publicKey:'' and the client never initialized. Set the same public phc_ key here so both layers agree.
PR #429 set the module-level posthog.publicKey but runtimeConfig.public.posthog still defaulted to empty strings, which override the module config at runtime when Vercel env vars are unset. Result: deployed __NUXT__.config.public.posthog showed publicKey:'' and the client never initialized. Set the same public phc_ key here so both layers agree.
PostHog project 29269 has received zero events in 30 days.
Root cause: Vercel env var
NUXT_PUBLIC_POSTHOG_PUBLIC_KEYwas never set, sonuxt-posthogmodule initialized with an empty key and dropped every event.Fix: Inline the public
phc_key as the default innuxt.config.ts. PostHog phc_ keys are public client-side tokens — this is the same pattern used by ikigai-team and brievcase after today's parallel fix. Env var override still works if someone wants to point v4 at a different project later.Verification: After merge, watch
eu.i.posthog.comproject 29269 for incoming$pageviewevents within a few minutes of Vercel redeploy.Summary by CodeRabbit