Skip to content

chore(analytics): add PostHog backend events for AI survey creation an…#8334

Open
harshsbhat wants to merge 2 commits into
mainfrom
chore/add-survey-created-ai-event
Open

chore(analytics): add PostHog backend events for AI survey creation an…#8334
harshsbhat wants to merge 2 commits into
mainfrom
chore/add-survey-created-ai-event

Conversation

@harshsbhat

@harshsbhat harshsbhat commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Summary

  • 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.

What does this PR do?

Fixes #(issue)

How should this be tested?

  • Test A
  • Test B

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read How we Code at Formbricks
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand bits
  • Ran pnpm build
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues
  • First PR at Formbricks? Please sign the CLA! Without it we wont be able to merge it πŸ™

Appreciated

  • If a UI change was made: Added a screen recording or screenshots to this PR
  • Updated the Formbricks Docs if changes were necessary

…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>
@harshsbhat harshsbhat requested review from itsjavi and jobenjada June 23, 2026 05:46
@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The pull request adds PostHog event capture to five server-side create operations. getSessionUserId is exported from apps/web/app/api/v3/surveys/lib/operations.ts so the v3 survey generation route can resolve the current actor and emit a survey_created event with created_from: "ai", the prompt, and workspace identifiers. Four EE module server actions (createChartAction, createDashboardAction, createFeedbackDirectoryAction, createApiKeyAction) each gain an import of capturePostHogEvent and a call that fires a named event with the new entity id, organizationId, and workspaceId after audit logging context is set.

πŸš₯ Pre-merge checks | βœ… 2 | ❌ 3

❌ Failed checks (2 warnings, 1 inconclusive)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description includes a clear summary of the five analytics events being added but lacks proper completion of required sections like 'How should this be tested?' and the checklist is entirely unchecked. Complete the 'How should this be tested?' section with specific testing instructions and check off completed items in the required checklist section.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'chore(analytics): add PostHog backend events for AI survey creation an…' is truncated and incomplete, making it unclear what the full scope of changes includes beyond AI survey creation. Complete the title to clearly convey all changes (e.g., 'chore(analytics): add PostHog backend events for surveys, dashboards, charts, API keys, and feedback directories').
βœ… Passed checks (2 passed)
Check name Status Explanation
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.

✏️ 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.

❀️ Share

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 8076ada and 136ae6f.

πŸ“’ Files selected for processing (6)
  • apps/web/app/api/v3/surveys/generate/route.ts
  • apps/web/app/api/v3/surveys/lib/operations.ts
  • apps/web/modules/ee/analysis/charts/actions.ts
  • apps/web/modules/ee/analysis/dashboards/actions.ts
  • apps/web/modules/ee/feedback-directory/actions.ts
  • apps/web/modules/organization/settings/api-keys/actions.ts

Comment thread apps/web/app/api/v3/surveys/generate/route.ts
Comment thread apps/web/app/api/v3/surveys/generate/route.ts Outdated
Comment thread apps/web/modules/organization/settings/api-keys/actions.ts
@sonarqubecloud

Copy link
Copy Markdown

@jobenjada jobenjada requested review from Dhruwang and removed request for itsjavi and jobenjada June 23, 2026 13:36

@Dhruwang Dhruwang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

2 participants