fix: chain beforeSend hooks and fall back on exception instead of dropping - #697
Draft
posthog[bot] wants to merge 1 commit into
Draft
fix: chain beforeSend hooks and fall back on exception instead of dropping#697posthog[bot] wants to merge 1 commit into
posthog[bot] wants to merge 1 commit into
Conversation
…of dropping `buildEvent()` in `PostHogStateless` called `beforeSend.run(postHogEvent)` on every loop iteration instead of `beforeSend.run(eventChecked)`, so with 2+ registered hooks each one transformed the *original* event rather than the previous hook's output — mutations from an earlier hook were invisible to later hooks. A throwing hook also dropped the event outright (`return null`) instead of falling back to the last good value. Both bugs affect `posthog-android` and `posthog-server`, which share this code via `PostHogStateless`. Generated-By: PostHog Desktop Task-Id: ea027967-6134-4b68-bcce-ef6a81e507b0
Contributor
posthog-android Compliance ReportDate: 2026-08-11 16:25:40 UTC ✅ All Tests Passed!46/46 tests passed Capture Tests✅ 29/29 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 Motivation and Context
Brings the
Before Send Hookcontract closer to compliance with thebefore-send-hookspec tracked incompliance/posthog-android.mdnote n17 (and the identicalposthog-java.mdnote n2, since bothposthog-androidandposthog-servershare this code path viaPostHogStateless).The spec requires that when multiple
beforeSendhooks are registered, each one receives the previous hook's output, and that a hook throwing an exception must not silently drop the event.PostHogStateless.buildEvent()had two bugs:beforeSend.run(postHogEvent)(the original, pre-chain event) on every iteration instead ofbeforeSend.run(eventChecked)(the running value). With 2+ hooks, hook Bugfix: fix screen event name back to screen and change key to screen… #2 never saw hook Change screen key to $screen_name to match ios #1's mutation.nullfrombuildEvent(), dropping the event entirely, rather than falling back to the last good (pre-exception) event.Why this is backward-compatible
Added as a
patchchangeset for bothposthogandposthog-server(they share the fixed code).💚 How did you test it?
PostHogBeforeSendTest.kt: one asserting a second hook sees the first hook's mutation, one asserting the event is still delivered (with the last-good properties) when a hook throws.PostHogStatelessTest.kt > beforeSend error handling does not crashtest, which had asserted the old (buggy) drop-on-exception behavior — it now asserts the event is delivered../gradlew :posthog:test :posthog-server:test,spotlessCheck, and:posthog:apiCheck :posthog-server:apiChecklocally — all green, no public API surface changes.📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
Autonomy: Fully autonomous
This PR was opened by an unattended daily SDK-compliance-implementer loop (Claude Code) that reads the compliance matrices in PostHog/sdk-specs and implements the single best-scoped, backward-compatible gap each run. This contract/gap was selected over ~90 other open gaps across 12 SDKs for being backward-compatible, high-impact (core capture pipeline correctness), narrowly scoped (a two-bug fix confined to one function), and not already covered by an open PR. No human directed this specific change — please assign an owner on review.
Created with PostHog Desktop