Skip to content

feat: add before send callback#259

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

feat: add before send callback#259
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 PostHogOptions.BeforeSend, invoked after event enrichment (SDK properties, super properties, $is_server, groups, and feature flags) and before batch serialization. Returning null drops the event.

💚 How did you test it?

  • dotnet test tests/UnitTests/UnitTests.csproj --filter 'FullyQualifiedName~PostHogClientTests.TheCaptureMethod'
  • dotnet build src/PostHog/PostHog.csproj

📝 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 hook runs after the event is fully enriched by moving before-send processing into the batch resolution path, so feature flag enrichment is visible to the callback before serialization. A dropped event is filtered out of the outgoing batch.

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

greptile-apps Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

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

Comment on lines +456 to +470
[Fact]
public async Task BeforeSendCanDropEventBeforeUpload()
{
var container = new TestContainer(services => services.Configure<PostHogOptions>(options =>
{
options.BeforeSend = _ => null;
}));
var requestHandler = container.FakeHttpMessageHandler.AddBatchResponse();
var client = container.Activate<PostHogClient>();

Assert.True(client.Capture("test-user", "drop-me"));
await client.FlushAsync();

Assert.Empty(requestHandler.ReceivedRequests);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Missing test for exception thrown by BeforeSend

ApplyBeforeSend wraps the callback in a try/catch that logs an error and drops the event when the callback throws. That catch branch is never exercised by the two tests added here. If something changes in that error path (logging, return value, re-throw), it would go undetected. A third test that configures BeforeSend to throw and then asserts the event is not sent would fully cover this path.

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