Skip to content

feat: add before send callback#161

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

feat: add before send callback#161
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 config callback. It receives the fully enriched event map after global/system properties, UUID, timestamp, and redaction have been applied. Returning nil drops the event.

💚 How did you test it?

  • mix format --check-formatted
  • mix test test/posthog_test.exs
  • mix posthog.public_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 sampo add to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Implemented with the pi coding agent. The callback is applied just before handing the enriched event to the sender, with invalid return values or raised exceptions dropping 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 lib/posthog.ex
Comment on lines +91 to 113
defp run_before_send(before_send, event) when is_function(before_send, 1) do
case before_send.(event) do
nil ->
nil

%{} = event ->
event

other ->
Logger.error(
"PostHog before_send callback returned #{inspect(other)} instead of an event map or nil; dropping event"
)

nil
end
rescue
exception ->
Logger.error(
"PostHog before_send callback raised; dropping event: #{Exception.message(exception)}"
)

nil
end

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 Error-handling paths in run_before_send are untested

run_before_send/2 has two error branches — when the callback returns a non-nil, non-map value and when the callback raises an exception — but neither is exercised by the new tests. Per the team's preference for parameterised tests, these could live alongside the existing before_send tests as additional cases (e.g., @tag test table entries, or describe-level setup with different callbacks). Without coverage, a future refactor of the rescue block or the other -> branch won't be caught by the test suite.

Context Used: Do not attempt to comment on incorrect alphabetica... (source)

Comment thread lib/posthog.ex
}

PostHog.Sender.send(event, name)
case run_before_send(Map.get(config, :before_send), event) do

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 Map.get(config, :before_send) is inconsistent with how all other config keys are accessed in bare_capture (e.g., config.global_properties). Since before_send always exists in the validated config (defaulting to nil), dot-access is sufficient and matches the surrounding style.

Suggested change
case run_before_send(Map.get(config, :before_send), event) do
case run_before_send(config.before_send, event) do

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