Skip to content

fix: add missing PostHog events#7722

Merged
Dhruwang merged 5 commits into
mainfrom
fix/posthog-missing-events
Apr 13, 2026
Merged

fix: add missing PostHog events#7722
Dhruwang merged 5 commits into
mainfrom
fix/posthog-missing-events

Conversation

@pandeymangg
Copy link
Copy Markdown
Contributor

What does this PR do?

Adds missing PostHog events that were not firing in several flows:

  1. organization_created not firing during signuphandleOrganizationCreation in signup flow creates org but never captured the PostHog event. Only the
    /setup/organization/create action had it.
  2. integration_connected not firing on OAuth connect — Notion, Airtable, and Slack OAuth callbacks call createOrUpdateIntegration service directly, bypassing the
    server action that has the PostHog capture.
  3. survey_response_received firing inconsistently — Old logic used 60s cached response count + count === 1 || count % 100 === 0. Stale cache caused duplicate
    fires for early responses, then silence until 100th. Replaced with milestone-based sampling (1, 10, 50, 100, 500, 1000, ...) and extracted to testable lib.

Also removed debug console.logs from integrations action.

How should this be tested?

  • Signup org creation: Create new user on cloud instance → verify organization_created event appears in PostHog with organization_id and is_first_org properties
  • OAuth integrations: Connect Notion/Airtable/Slack via OAuth → verify integration_connected event appears in PostHog with correct integration_type and
    organization_id
  • Survey response milestones: Submit survey responses → verify survey_response_received fires at counts 1, 10, 50, 100, 500 and does NOT fire for counts 2-9, 11-49,
    etc.
  • Unit tests: npx vitest run apps/web/app/api/\(internal\)/pipeline/lib/posthog.test.ts — 10 tests covering milestone logic

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

Appreciated

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

…egrations

PostHog `organization_created` event was not firing during user signup flow,
and `integration_connected` was missing from OAuth callbacks (Notion, Airtable,
Slack). Also fixes survey_response_received sampling which was broken by stale
cache — extracted to lib with milestone-based firing (1, 10, 50, 100, 500+).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@pandeymangg pandeymangg marked this pull request as ready for review April 13, 2026 11:00
@pandeymangg pandeymangg requested a review from Dhruwang April 13, 2026 11:00
…s OAuth

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 13, 2026

Walkthrough

This pull request introduces PostHog analytics tracking across multiple integration and survey response flows. A new PostHog module is added with functions to determine milestone-based survey response capture (1st, 10th, 50th, 100th, and every 500th response) and to emit survey response events with metadata. PostHog event tracking is integrated into three integration callback handlers for Airtable, Notion, and Slack to log successful connections. A new tracking call is added to organization creation. The survey response tracking in the pipeline route is refactored to use the new helper function and remove response count caching. Comprehensive tests are included for the new survey response tracking logic.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
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.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: add missing PostHog events' is clear, concise, and directly summarizes the main change—adding missing PostHog event captures across multiple flows.
Description check ✅ Passed The PR description fully addresses the template requirements: it explains what the PR does (3 specific PostHog event issues fixed), provides detailed testing instructions for each change (signup org creation, OAuth integrations, survey response milestones, and unit tests), and includes a completed checklist with all required items marked.

✏️ 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 and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/web/app/api/`(internal)/pipeline/route.ts:
- Around line 303-313: The milestone count is being recomputed after
Promise.allSettled so it can be stale; instead obtain the exact response ordinal
during the response-creation DB transaction and pass that through to
captureSurveyResponsePostHogEvent (or implement an idempotent milestone-claim in
the same transaction). Concretely: modify the response creation flow to return
the created response's ordinal/id from the transaction, thread that value out of
the function that calls getResponseCountBySurveyId, and replace the call to
getResponseCountBySurveyId with the transaction-returned ordinal when invoking
captureSurveyResponsePostHogEvent (or call an idempotent claim function inside
the creation transaction to mark milestones).

In `@apps/web/app/api/v1/integrations/airtable/callback/route.ts`:
- Around line 92-96: The analytics lookup getOrganizationIdFromEnvironmentId is
on the critical path and can turn a successful Airtable connect into a 500 if it
fails; wrap the analytics side-effect so failures don't affect the response:
call getOrganizationIdFromEnvironmentId and capturePostHogEvent inside a
try/catch (or otherwise swallow/log errors) using authentication.user.id and the
integration metadata, and ensure any thrown error is logged but not rethrown so
the redirect still occurs; apply the same guard pattern to the matching Slack
and Notion callback handlers (i.e., protect their capturePostHogEvent calls
similarly).
🪄 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: e25ef5c4-628d-4bcf-b797-03ef947c2bab

📥 Commits

Reviewing files that changed from the base of the PR and between 4b009a8 and 3a3555e.

📒 Files selected for processing (7)
  • apps/web/app/api/(internal)/pipeline/lib/posthog.test.ts
  • apps/web/app/api/(internal)/pipeline/lib/posthog.ts
  • apps/web/app/api/(internal)/pipeline/route.ts
  • apps/web/app/api/v1/integrations/airtable/callback/route.ts
  • apps/web/app/api/v1/integrations/notion/callback/route.ts
  • apps/web/app/api/v1/integrations/slack/callback/route.ts
  • apps/web/modules/auth/signup/actions.ts

Comment thread apps/web/app/api/(internal)/pipeline/route.ts Outdated
Comment thread apps/web/app/api/v1/integrations/airtable/callback/route.ts Outdated
Dhruwang
Dhruwang previously approved these changes Apr 13, 2026
Copy link
Copy Markdown
Member

@Dhruwang Dhruwang left a comment

Choose a reason for hiding this comment

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

Looks good 🚀

@sonarqubecloud
Copy link
Copy Markdown

@Dhruwang Dhruwang added this pull request to the merge queue Apr 13, 2026
Merged via the queue into main with commit 2556f5e Apr 13, 2026
17 checks passed
@Dhruwang Dhruwang deleted the fix/posthog-missing-events branch April 13, 2026 12:12
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