analytics-next icon indicating copy to clipboard operation
analytics-next copied to clipboard

How to discard events from plugins

Open Adeboye opened this issue 3 years ago • 3 comments

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 avatar Oct 25 '22 03:10 Adeboye

@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 }))
  }
}

chrisradek avatar Oct 25 '22 04:10 chrisradek

Thanks for the answer, I was looking in the docs to find where this is documented, could you point me to the area @chrisradek

Adeboye avatar Oct 25 '22 14:10 Adeboye

@chrisradek tried the above solution you provided, but I still see the retries happening, any ideas why this is till happening ?

Adeboye avatar Oct 25 '22 19:10 Adeboye