chore(analytics): add PostHog backend events for AI survey creation anβ¦#8334
chore(analytics): add PostHog backend events for AI survey creation anβ¦#8334harshsbhat wants to merge 2 commits into
Conversation
β¦d key product actions - Fire survey_created (created_from: ai, prompt) when AI survey generation succeeds - Fire api_key_created (label) when an API key is created - Fire dashboard_created (dashboard_id) when a dashboard is created - Fire chart_created (chart_id) when a chart is created - Fire feedback_directory_created (feedback_directory_id) when a feedback directory is created All events use PostHog group context for org/workspace-level analytics. Exports getSessionUserId from operations.ts to avoid duplicating the auth-type guard. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
WalkthroughThe pull request adds PostHog event capture to five server-side create operations. π₯ Pre-merge checks | β 2 | β 3β Failed checks (2 warnings, 1 inconclusive)
β Passed checks (2 passed)
βοΈ Tip: You can configure your own custom pre-merge checks in the settings. 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: 3
π€ Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@apps/web/app/api/v3/surveys/generate/route.ts`:
- Around line 62-66: The capturePostHogEvent call in the route handler is
emitting a "survey_created" event for AI draft generation, but this same event
name is also used in apps/web/app/api/v3/surveys/lib/operations.ts for persisted
survey creation with different properties, causing analytics schema drift and
double-counting. Replace the "survey_created" event name in the
capturePostHogEvent call with a distinct event name that specifically indicates
AI draft generation (such as "survey_draft_generated_by_ai" or similar) to
differentiate this action from actual survey persistence.
- Line 65: The code at line 65 in the survey generation route is sending the raw
user-generated prompt text (body.prompt) directly to PostHog analytics. This is
a privacy and security concern as it exposes potentially sensitive
user-generated content to third-party services. Replace the prompt field in the
PostHog event object with a derived, non-sensitive metric instead, such as the
character count of the prompt, the prompt length category (e.g., "short",
"medium", "long"), or another sanitized categorical signal that preserves
analytics usefulness without exposing raw user content.
In `@apps/web/modules/organization/settings/api-keys/actions.ts`:
- Around line 67-71: The capturePostHogEvent call in the api-keys/actions.ts
file is sending the user-defined label directly to PostHog analytics, which can
expose sensitive information and create high-cardinality telemetry. Replace the
label property (parsedInput.apiKeyData.label) with a stable, non-sensitive
identifier such as the api_key_id or other derived metadata that does not expose
the raw user-provided label text. This ensures the analytics data remains
consistent and non-sensitive per the coding guidelines.
πͺ Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
βΉοΈ Review info
βοΈ Run configuration
Configuration used: Repository UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: f58c21dd-fe43-487d-954a-ff38335b1797
π Files selected for processing (6)
apps/web/app/api/v3/surveys/generate/route.tsapps/web/app/api/v3/surveys/lib/operations.tsapps/web/modules/ee/analysis/charts/actions.tsapps/web/modules/ee/analysis/dashboards/actions.tsapps/web/modules/ee/feedback-directory/actions.tsapps/web/modules/organization/settings/api-keys/actions.ts
|
Dhruwang
left a comment
There was a problem hiding this comment.
Thanks for the PR, it looks good. π
Just a small comment: The 'survey_created' event fires at the 'generate' step, but the route only returns a draft. The survey isn't persisted until the 'createV3Survey' runs later, and validation can still reject it in between. This counts surveys that may never exist. We can change this to 'survey_generated'. But this is not a very big hurdle. If you think that's fine, then we can merge this PR.



Summary
All events use PostHog group context for org/workspace-level analytics. Exports getSessionUserId from operations.ts to avoid duplicating the auth-type guard.
What does this PR do?
Fixes #(issue)
How should this be tested?
Checklist
Required
pnpm buildconsole.logsgit pull origin mainAppreciated