Skip to content

feat: add PostHog analytics integration - #82

Draft
posthog[bot] wants to merge 2 commits into
mainfrom
posthog/instrumentation-f5a1d6
Draft

feat: add PostHog analytics integration#82
posthog[bot] wants to merge 2 commits into
mainfrom
posthog/instrumentation-f5a1d6

Conversation

@posthog

@posthog posthog Bot commented Aug 4, 2026

Copy link
Copy Markdown

Summary

This PR ships the PostHog setup wizard's latest pass over the existing posthog-js integration (v1.288.0 → v1.410.10 within the same ^1.288.0 semver range) in vite-project:

  • src/main.tsx — guards the existing PostHogProvider wrap: renders the app without it (skipping the crash-when-missing-env-var mentioned below) instead of unconditionally passing possibly-empty apiKey/api_host, and throws a loud dev-only error if VITE_PUBLIC_POSTHOG_KEY/VITE_PUBLIC_POSTHOG_HOST are unset.
  • src/features/auth/AuthContext.tsx — adds an account-switch guard: posthog.reset() now fires when a different Firebase uid logs in, not just on logout, so identity doesn't get merged across accounts.
  • Event captures added at successful-action boundaries, all non-PII product properties:
    • src/components/elevationfinder/GpxUploader.tsxgpx_route_uploaded
    • src/features/elevation/components/RouteDashboard.tsxroute_analysis_settings_updated
    • src/features/fuel/components/AIPersonalization.tsxfuel_ai_advice_requested
    • src/features/fuel/components/FuelPlannerV2.tsxfuel_plan_calculated
    • src/features/fuel/hooks/useFuelPlanPersistence.tsfuel_plan_saved
    • src/features/goals/components/OnboardingFlow.tsxonboarding_completed
    • src/features/pace-calculator/components/PaceCalculatorV2.tsxpace_calculated
    • src/features/pace-calculator/hooks/usePacePlanPersistence.tspace_plan_saved
    • src/features/plan/components/PlanInputForm.tsxtraining_plan_generated
    • src/features/plan/hooks/useSavePlan.tstraining_plan_saved
  • .env.example — documents VITE_PUBLIC_POSTHOG_KEY / VITE_PUBLIC_POSTHOG_HOST.
  • package-lock.json — refreshes posthog-js and its transitive deps within the existing package.json version range.

Insights and dashboards created

Type Name Link
Dashboard Analytics basics (wizard) https://us.posthog.com/project/524328/dashboard/1951948
Insight Runner activity (wizard) https://us.posthog.com/project/524328/insights/DjdwINiL
Insight Pace save funnel (wizard) https://us.posthog.com/project/524328/insights/pdHOBIqL
Insight Fuel plan save funnel (wizard) https://us.posthog.com/project/524328/insights/GhVdZ5Se
Insight Training plan save funnel (wizard) https://us.posthog.com/project/524328/insights/5ah2hnB8
Insight Route and AI engagement (wizard) https://us.posthog.com/project/524328/insights/zu6vIQJu

How to verify

  1. Deploy this branch (or run it locally with the env vars below already set in vite-project/.env).
  2. Log in/out with a Google account and confirm no console errors from the main.tsx env-var guard.
  3. Exercise a few flows — calculate a pace, generate a fuel plan, upload a GPX route, finish onboarding.
  4. In PostHog, open Activity and confirm the corresponding events (pace_calculated, fuel_plan_calculated, gpx_route_uploaded, onboarding_completed, etc.) arrive within a minute.

Environment variables

TrainPace deploys to Vercel (per vercel.json / CLAUDE.md), which keeps environment variables in its dashboard rather than in a file committed to this repo, so I could not set these up automatically. VITE_PUBLIC_POSTHOG_KEY and VITE_PUBLIC_POSTHOG_HOST were already in use before this PR (see src/main.tsx on main), so production should already have them configured — this is a reminder to confirm, not a first-time setup:

Name Value
VITE_PUBLIC_POSTHOG_KEY phc_vM6GXKZAALbMzPuM8vfRsD2zaayjC7VL48kqLMWFZJNQ
VITE_PUBLIC_POSTHOG_HOST https://us.i.posthog.com

To confirm or set them:

  1. Open https://vercel.com/dashboard and select the TrainPace project.
  2. Go to SettingsEnvironment Variables.
  3. Confirm both rows above exist for the Production environment (add them if missing), then Save.
  4. If you add/change anything, redeploy (Deployments tab → "..." on the latest deployment → Redeploy) — env var changes only take effect on the next deployment.

Setting environment variables via a local CLI

echo "phc_vM6GXKZAALbMzPuM8vfRsD2zaayjC7VL48kqLMWFZJNQ" | npx vercel env add VITE_PUBLIC_POSTHOG_KEY production
echo "https://us.i.posthog.com" | npx vercel env add VITE_PUBLIC_POSTHOG_HOST production

Created with PostHog Code

@netlify

netlify Bot commented Aug 4, 2026

Copy link
Copy Markdown

Deploy Preview for trainpace ready!

Name Link
🔨 Latest commit 0cf98b2
🔍 Latest deploy log https://app.netlify.com/projects/trainpace/deploys/6a720e12362eed0008cb03e5
😎 Deploy Preview https://deploy-preview-82--trainpace.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 49
Accessibility: 89
Best Practices: 100
SEO: 100
PWA: 60
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@vercel

vercel Bot commented Aug 4, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
trainpace Ready Ready Preview Aug 4, 2026 4:07pm

@codacy-production

codacy-production Bot commented Aug 4, 2026

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 19 high

Alerts:
⚠ 19 issues (≤ 0 issues of at least minor severity)

Results:
19 new issues

Category Results
ErrorProne 19 high

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Generated-By: PostHog Code
Task-Id: 66b2f920-2441-47c3-9e9a-ec8b383c0c8d
Without VITE_PUBLIC_POSTHOG_KEY/HOST, main.tsx's new dev-mode guard throws on every page load in CI, since the e2e webServer runs `npm run dev` without them set.

Generated-By: PostHog Code
Task-Id: 66b2f920-2441-47c3-9e9a-ec8b383c0c8d
@posthog

posthog Bot commented Aug 4, 2026

Copy link
Copy Markdown
Author

Codacy Static Code Analysis shows action_required (19 new issues, 0 max.) — all 19 are no-unsafe-call / no-unsafe-member-access warnings on the new posthog.capture(...) lines this PR adds, e.g. AIPersonalization.tsx#L73.

This looks like a Codacy-isolated-analysis artifact rather than a real type error: npm run build (tsc -b) and npm run lint (eslint .) both pass cleanly on these exact lines in this repo's own toolchain, and it's not a required check here — recent merged PRs (#77, #78) show the same action_required conclusion on unrelated changes. Leaving this as-is rather than modifying the wizard-authored instrumentation code to work around a third-party tool's type resolution.

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.

0 participants