Skip to content

feat: add before send callback#201

Closed
marandaneto wants to merge 1 commit into
PostHog:mainfrom
marandaneto:feat/add-before-send
Closed

feat: add before send callback#201
marandaneto wants to merge 1 commit into
PostHog:mainfrom
marandaneto:feat/add-before-send

Conversation

@marandaneto

@marandaneto marandaneto commented Jul 5, 2026

Copy link
Copy Markdown
Member

💡 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_send client option. It receives the fully enriched event array after SDK metadata, server attribution, groups, timestamps, and feature flag properties have been applied. Returning null drops the event.

💚 How did you test it?

  • ./vendor/bin/phpunit --no-coverage --filter 'BeforeSend|BatchSizeOne' test/PostHogTest.php
  • composer api:check

📝 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)

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.

@marandaneto marandaneto self-assigned this Jul 5, 2026
@greptile-apps

greptile-apps Bot commented Jul 5, 2026

Copy link
Copy Markdown

Reviews (1): Last reviewed commit: "feat: add before send callback" | Re-trigger Greptile

Comment thread test/PostHogTest.php
Comment on lines +529 to +545
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 ?? []);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 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!

@marandaneto

Copy link
Copy Markdown
Member Author

Closing in favor of a PR from a branch on the PostHog repository.

@marandaneto marandaneto closed this Jul 5, 2026
@marandaneto marandaneto deleted the feat/add-before-send branch July 5, 2026 11:18
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.

1 participant