Skip to content

feat: add before send callback#202

Open
marandaneto wants to merge 2 commits into
mainfrom
feat/add-before-send
Open

feat: add before send callback#202
marandaneto wants to merge 2 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-05 11:41:14 UTC
Duration: 95301ms

✅ All Tests Passed!

46/46 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 11ms
Format Validation.Event Has Uuid 5ms
Format Validation.Event Has Lib Properties 6ms
Format Validation.Distinct Id Is String 5ms
Format Validation.Token Is Present 6ms
Format Validation.Custom Properties Preserved 5ms
Format Validation.Event Has Timestamp 5ms
Retry Behavior.Retries On 503 5314ms
Retry Behavior.Does Not Retry On 400 2009ms
Retry Behavior.Does Not Retry On 401 2009ms
Retry Behavior.Respects Retry After Header 8014ms
Retry Behavior.Implements Backoff 15728ms
Retry Behavior.Retries On 500 5114ms
Retry Behavior.Retries On 502 5114ms
Retry Behavior.Retries On 504 5114ms
Retry Behavior.Max Retries Respected 16528ms
Deduplication.Generates Unique Uuids 12ms
Deduplication.Preserves Uuid On Retry 5114ms
Deduplication.Preserves Uuid And Timestamp On Retry 10322ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 5117ms
Deduplication.No Duplicate Events In Batch 11ms
Deduplication.Different Events Have Different Uuids 7ms
Compression.Sends Gzip When Enabled 5ms
Batch Format.Uses Proper Batch Structure 6ms
Batch Format.Flush With No Events Sends Nothing 3ms
Batch Format.Multiple Events Batched Together 10ms
Error Handling.Does Not Retry On 403 2007ms
Error Handling.Does Not Retry On 413 2009ms
Error Handling.Retries On 408 5114ms

Feature_Flags Tests

17/17 tests passed

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

@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

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