analytics-next
analytics-next copied to clipboard
How to discard events from plugins
Hi,
I am using plugins to do some validations on events, there are certain events I will like to discard if a certain condition is met. I have tried using ctx.cancel or throwing an error hoping the event pipeline stops but its just retries. How do I go about discarding events ? Refer to the example code below
const filterEvents: Plugin = {
name: 'Filter events',
type: 'before',
version: '1.0.0',
isLoaded: () => true,
load: () => Promise.resolve(),
track: (ctx: Context) => {
// if some condition is met I want to discard this event and stop the pipeline processing
if (condition met) {
ctx.cancel();
}
return ctx;
};
};
@Adeboye Thanks for opening this issue.
In plugins that are before type, you can call ctx.cancel with a ContextCancelation that turns off retries. That would look like this:
import { Context, ContextCancelation } from '@segment/analytics-next
// ...
track: (ctx: Context) => {
if (condition_met) {
ctx.cancel(new ContextCancelation({ retry: false }))
}
}
Thanks for the answer, I was looking in the docs to find where this is documented, could you point me to the area @chrisradek
@chrisradek tried the above solution you provided, but I still see the retries happening, any ideas why this is till happening ?