pub type BeforeSendFn = Box<dyn FnMut(Event) -> Option<Event> + Send + 'static>;Expand description
Hook that can modify or discard events before sending.
Each hook receives an owned Event and can:
- Return
Some(event)to pass the (possibly modified) event to the next hook - Return
Noneto discard the event and stop further processing
Hooks are executed in order after the event is enriched with library/OS context. If any hook panics, the event is discarded and an error is logged.
§Thread Safety
Hooks run in a background worker thread, so they must be Send + 'static.
If you need randomness (e.g., for sampling), use a Send-compatible RNG like fastrand::Rng initialized before the closure.
§Example
let options = better_posthog::ClientOptions {
api_key: Some("phc_your_api_key".into()),
before_send: vec![{
// Initialize a scoped `Send`-compatible RNG.
let mut rng = fastrand::Rng::new();
// Return a `before_send` hook.
Box::new(move |event| {
let sample_rate = match event.event.as_str() {
"button_click" => 0.5, // Process only a half of `button_click` events.
_ => 1.0, // Process all other events.
};
if rng.f64() < sample_rate { Some(event) } else { None }
})
}],
..Default::default()
};Aliased Type§
pub struct BeforeSendFn(/* private fields */);