feat: add before send callback#201
Conversation
|
Reviews (1): Last reviewed commit: "feat: add before send callback" | Re-trigger Greptile |
| public function testBeforeSendCanDropEvent(): void | ||
| { | ||
| $httpClient = new MockedHttpClient("app.posthog.com"); | ||
| $client = new Client( | ||
| self::FAKE_API_KEY, | ||
| ["batch_size" => 1, "before_send" => static fn(array $event): ?array => null], | ||
| $httpClient, | ||
| null, | ||
| false | ||
| ); | ||
|
|
||
| $this->assertFalse($client->capture([ | ||
| "distinctId" => "john", | ||
| "event" => "Module PHP Event", | ||
| ])); | ||
| $this->assertSame([], $httpClient->calls ?? []); | ||
| } |
There was a problem hiding this comment.
Missing tests for
applyBeforeSend error branches
applyBeforeSend has three distinct error/defensive paths — a non-callable before_send, a callback that throws, and a callback that returns a non-array non-null value — but none of these are exercised by the new tests. A misconfigured callback will silently drop every capture call with only an error_log entry, so verifying these branches is important. Given the project's preference for parameterised tests, a @dataProvider covering all five cases (modify, drop-via-null, non-callable, throws, non-array-return) in a single test method would also align with the established pattern in this file.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
Closing in favor of a PR from a branch on the PostHog repository. |
💡 Motivation and Context
Customers need a configuration hook to inspect, modify, or drop events before upload, matching the before-send behavior available in other PostHog SDKs.
This adds a
before_sendclient option. It receives the fully enriched event array after SDK metadata, server attribution, groups, timestamps, and feature flag properties have been applied. Returningnulldrops the event.💚 How did you test it?
./vendor/bin/phpunit --no-coverage --filter 'BeforeSend|BatchSizeOne' test/PostHogTest.phpcomposer api:check📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Implemented with the pi coding agent. The callback runs immediately before queueing the fully enriched capture payload; callbacks that return
null, return a non-array, or throw will drop only that event.