Skip to content

feat: add captureFeatureView and captureFeatureInteraction - #667

Merged
turnipdabeets merged 4 commits into
mainfrom
feat/flutter-capture-feature-view-interaction
Oct 26, 2026
Merged

turnipdabeets merged 4 commits into
mainfrom
feat/flutter-capture-feature-view-interaction

Conversation

@turnipdabeets

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

Adds Posthog().captureFeatureView(flag, {flagVariant}) and Posthog().captureFeatureInteraction(flag, {flagVariant}), matching posthog-ios captureFeatureView(flag:flagVariant:) / captureFeatureInteraction(flag:flagVariant:) and the posthog-android equivalents.

Event shape (same on every platform):

  • event $feature_view / $feature_interaction
  • properties feature_flag: <flag>, plus feature_flag_variant: <variant> when the variant is a string
  • $set: {"$feature_view/<flag>": <variant>} (or $feature_interaction/<flag>)

When flagVariant is omitted, the variant is the flag's current value (variant ?? enabled), read without capturing $feature_flag_called.

Platform Implementation
iOS/macOS PostHogSDK.shared.captureFeatureView/Interaction(flag:flagVariant:) (posthog-ios 3.47.0+, floor is 3.72.0)
Android PostHog.captureFeatureView/Interaction(flag, flagVariant) (posthog-android 3.32.1+, floor is 3.61.0)
Windows/Linux, web Default implementation on PosthogFlutterPlatformInterface: getFeatureFlagResult(sendEvent: false), then capture() with the shape above. posthog-js has no such API in the browser SDK (only the @posthog/react PostHogFeature helper, which builds the same shape)

No native version floor bumps needed.

🤔 Open questions for review

  • No flag value: the native SDKs disagree. posthog-ios skips the event and logs (a deliberate change in posthog-ios De-obfuscate Dart stack traces from obfuscated builds #487). posthog-android and @posthog/react fall back to true. Mobile keeps each native SDK's behavior. Desktop and web follow iOS and skip. The dartdoc spells out the difference. If we'd rather follow Android/React, it's a one-line change.
  • beforeSend: on desktop and web the event goes through capture(), so Dart beforeSend callbacks see it. On iOS/Android the native SDK captures it directly, so Dart beforeSend doesn't, the same as other native-generated events like $feature_flag_called.

💚 How did you test it?

  • New tests: desktop end-to-end against the local fake server (looked-up variant, explicit variant, unknown flag skipped, no $feature_flag_called), method-channel arguments, web mapping to posthog-js getFeatureFlagResult + capture with $set, and Posthog forwarding.
  • flutter analyze, flutter test, dart format --set-exit-if-changed lib test, scripts/check-api-dart.sh (snapshot updated), ktlint and swiftformat lint all pass.
  • flutter build apk --debug and flutter build ios --simulator --debug in example/ succeed.
  • flutter test --platform chrome on the web handler tests: the new test passes. Three app version tests fail locally on main too (they rely on Flutter 3.47+ build defines; local Flutter is 3.44.1).
  • Not tested on a device or simulator at runtime.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Claude Code implemented this as the third of four feature-flag parity PRs. The event shape was taken from posthog-ios captureFeatureEvent, posthog-android PostHog.kt, and @posthog/react PostHogFeature.tsx. Commits are signed.

🤖 Generated with Claude Code

@turnipdabeets
turnipdabeets requested a review from a team as a code owner October 23, 2026 17:04
@turnipdabeets turnipdabeets self-assigned this Oct 23, 2026

@posthog posthog 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.

PostHog Review · Standard · reviewed 0e7ffd8: 1 Should fix.

See this review and its findings in PostHog Review. Comment @posthog review --deep for a Deep review. A Deep review takes longer and looks deeper than a Standard review.

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

posthog-flutter Compliance Report

Date: 2026-10-26 17:39:50 UTC
Duration: 96697ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

✅ 29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields ✅ 136ms
Format Validation.Event Has Uuid ✅ 118ms
Format Validation.Event Has Lib Properties ✅ 114ms
Format Validation.Distinct Id Is String ✅ 114ms
Format Validation.Token Is Present ✅ 116ms
Format Validation.Custom Properties Preserved ✅ 116ms
Format Validation.Event Has Timestamp ✅ 116ms
Retry Behavior.Retries On 503 ✅ 5329ms
Retry Behavior.Does Not Retry On 400 ✅ 2118ms
Retry Behavior.Does Not Retry On 401 ✅ 2117ms
Retry Behavior.Respects Retry After Header ✅ 8122ms
Retry Behavior.Implements Backoff ✅ 15444ms
Retry Behavior.Retries On 500 ✅ 5224ms
Retry Behavior.Retries On 502 ✅ 5226ms
Retry Behavior.Retries On 504 ✅ 5223ms
Retry Behavior.Max Retries Respected ✅ 15437ms
Deduplication.Generates Unique Uuids ✅ 123ms
Deduplication.Preserves Uuid On Retry ✅ 5221ms
Deduplication.Preserves Uuid And Timestamp On Retry ✅ 10330ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 5229ms
Deduplication.No Duplicate Events In Batch ✅ 122ms
Deduplication.Different Events Have Different Uuids ✅ 114ms
Compression.Sends Gzip When Enabled ✅ 113ms
Batch Format.Uses Proper Batch Structure ✅ 112ms
Batch Format.Flush With No Events Sends Nothing ✅ 107ms
Batch Format.Multiple Events Batched Together ✅ 122ms
Error Handling.Does Not Retry On 403 ✅ 2114ms
Error Handling.Does Not Retry On 413 ✅ 2116ms
Error Handling.Retries On 408 ✅ 5223ms

Feature_Flags Tests

✅ 16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id ✅ 13ms
Request Payload.Flags Request Uses V2 Query Param ✅ 9ms
Request Payload.Flags Request Hits Flags Path Not Decide ✅ 10ms
Request Payload.Flags Request Omits Authorization Header ✅ 8ms
Request Payload.Token In Flags Body Matches Init ✅ 9ms
Request Payload.Groups Round Trip ✅ 9ms
Request Payload.Groups Default To Empty Object ✅ 9ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It ✅ 9ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False ✅ 8ms
Request Payload.Disable Geoip Omitted Defaults To False ✅ 9ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key ✅ 8ms
Request Lifecycle.No Flags Request On Init Alone ✅ 4ms
Request Lifecycle.No Flags Request On Normal Capture ✅ 111ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests ✅ 15ms
Request Lifecycle.Mock Response Value Is Returned To Caller ✅ 8ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event ✅ 114ms

Look up the flag and start the capture in one guard callback, so an
un-awaited captureFeatureView/captureFeatureInteraction followed by
reset() can't send the event and $set to the next user.
posthog-js has no current screen and these events skip Posthog.capture(),
the only place the Dart side adds $screen_name, so on web they were the
one platform without it. The web plugin now remembers the last screen()
until close(), like the facade, and adds it to both events.

The event shape moves to a helper shared with desktop; the platform
interface default goes back to UnimplementedError since every platform
now overrides it.

@posthog posthog 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.

PostHog Review · Standard · reviewed 4cce0eb: 1 Should fix.

Feature events took the raw screen() name even when beforeSend renamed
or dropped the $screen event. Store the final name of an accepted screen
event instead, as desktop's registerForSession does.
@turnipdabeets
turnipdabeets merged commit 60bc22b into main Oct 26, 2026
30 checks passed
@turnipdabeets
turnipdabeets deleted the feat/flutter-capture-feature-view-interaction branch October 26, 2026 17:51
turnipdabeets added a commit that referenced this pull request Oct 26, 2026
getAllFeatureFlags (#665) decoded with PostHogFeatureFlagResult.fromMap,
which #646 made private, and the web feature events (#667) passed
sendEvent:, which #647 renamed to sendFeatureFlagEvent:.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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