Skip to content

feat: add before send callback#202

Merged
marandaneto merged 4 commits into
mainfrom
feat/add-before-send
Jul 6, 2026
Merged

feat: add before send callback#202
marandaneto merged 4 commits into
mainfrom
feat/add-before-send

Conversation

@marandaneto

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
@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

posthog-php Compliance Report

Date: 2026-07-06 07:13:52 UTC
Duration: 95356ms

✅ All Tests Passed!

46/46 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 13ms
Format Validation.Event Has Uuid 6ms
Format Validation.Event Has Lib Properties 6ms
Format Validation.Distinct Id Is String 6ms
Format Validation.Token Is Present 5ms
Format Validation.Custom Properties Preserved 6ms
Format Validation.Event Has Timestamp 5ms
Retry Behavior.Retries On 503 5317ms
Retry Behavior.Does Not Retry On 400 2010ms
Retry Behavior.Does Not Retry On 401 2010ms
Retry Behavior.Respects Retry After Header 8019ms
Retry Behavior.Implements Backoff 15723ms
Retry Behavior.Retries On 500 5116ms
Retry Behavior.Retries On 502 5117ms
Retry Behavior.Retries On 504 5117ms
Retry Behavior.Max Retries Respected 16533ms
Deduplication.Generates Unique Uuids 13ms
Deduplication.Preserves Uuid On Retry 5114ms
Deduplication.Preserves Uuid And Timestamp On Retry 10323ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 5118ms
Deduplication.No Duplicate Events In Batch 13ms
Deduplication.Different Events Have Different Uuids 7ms
Compression.Sends Gzip When Enabled 6ms
Batch Format.Uses Proper Batch Structure 7ms
Batch Format.Flush With No Events Sends Nothing 3ms
Batch Format.Multiple Events Batched Together 9ms
Error Handling.Does Not Retry On 403 2008ms
Error Handling.Does Not Retry On 413 2010ms
Error Handling.Retries On 408 5117ms

Feature_Flags Tests

17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 11ms
Request Payload.Flags Request Uses V2 Query Param 6ms
Request Payload.Flags Request Hits Flags Path Not Decide 6ms
Request Payload.Flags Request Omits Authorization Header 6ms
Request Payload.Token In Flags Body Matches Init 4ms
Request Payload.Groups Round Trip 6ms
Request Payload.Groups Default To Empty Object 5ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 5ms
Request Payload.Disable Geoip Omitted Defaults To False 6ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 6ms
Request Lifecycle.No Flags Request On Init Alone 2ms
Request Lifecycle.No Flags Request On Normal Capture 6ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 8ms
Request Lifecycle.Mock Response Value Is Returned To Caller 5ms
Retry Behavior.Retries Flags On 502 108ms
Retry Behavior.Retries Flags On 504 109ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 33ms

@marandaneto marandaneto marked this pull request as ready for review July 5, 2026 12:30
@marandaneto marandaneto requested a review from a team as a code owner July 5, 2026 12:30
@greptile-apps

greptile-apps Bot commented Jul 5, 2026

Copy link
Copy Markdown

Reviews (2): Last reviewed commit: "test: cover before send edge cases" | Re-trigger Greptile

@marandaneto

Copy link
Copy Markdown
Member Author

Closes #178

@ioannisj ioannisj left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approving to unblock but currently before_send just runs for capture() and skips identify/alias or any event that doesn't have capture as an entrypoint

@marandaneto marandaneto merged commit fa53311 into main Jul 6, 2026
21 checks passed
@marandaneto marandaneto deleted the feat/add-before-send branch July 6, 2026 07:32
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