Custom plugins are user-defined plugins that provide flexibility in extending and modifying event processing within the SDK.
Android (Kotlin)
This section lists the steps to create a custom plugin EventFilteringPlugin for filtering out specific track events in the Android (Kotlin) SDK.
Specifically, it filters out the Application Opened and Application Backgrounded events and prevents them from being tracked.
Add the plugin logic:
importcom.rudderstack.sdk.kotlin.core.Analyticsimportcom.rudderstack.sdk.kotlin.core.internals.logger.LoggerAnalyticsimportcom.rudderstack.sdk.kotlin.core.internals.models.Eventimportcom.rudderstack.sdk.kotlin.core.internals.models.TrackEventimportcom.rudderstack.sdk.kotlin.core.internals.plugins.PluginclassEventFilteringPlugin:Plugin{overridevalpluginType:Plugin.PluginType=Plugin.PluginType.OnProcessoverridelateinitvaranalytics:AnalyticsprivatelateinitvarlistOfEventsToBeFiltered:List<String>overridefunsetup(analytics:Analytics){super.setup(analytics)listOfEventsToBeFiltered=listOf("Application Opened","Application Backgrounded")}overridesuspendfunintercept(event:Event):Event?{if(eventisTrackEvent&&listOfEventsToBeFiltered.contains(event.event)){LoggerAnalytics.verbose("EventFilteringPlugin: Event ${event.event} is filtered out")returnnull}returnevent}overridefunteardown(){listOfEventsToBeFiltered=emptyList()}}
importandroidx.annotation.NonNull;importandroidx.annotation.Nullable;importcom.rudderstack.sdk.kotlin.core.Analytics;importcom.rudderstack.sdk.kotlin.core.internals.logger.LoggerAnalytics;importcom.rudderstack.sdk.kotlin.core.internals.models.Event;importcom.rudderstack.sdk.kotlin.core.internals.models.TrackEvent;importcom.rudderstack.sdk.kotlin.core.internals.plugins.Plugin;importjava.util.List;importkotlin.coroutines.Continuation;/**
* A custom Java Plugin demonstrating how to perform event filtering in Java.
*/publicclassJavaEventFilteringPluginimplementsPlugin{privateAnalyticsanalytics;privatestaticfinalPlugin.PluginTypepluginType=Plugin.PluginType.OnProcess;List<String>listOfEventsToBeFiltered;@OverridepublicvoidsetAnalytics(@NonNullAnalyticsanalytics){this.analytics=analytics;}@Overridepublicvoidsetup(@NonNullAnalyticsanalytics){this.analytics=analytics;listOfEventsToBeFiltered=List.of("Application Opened","Application Backgrounded");}@Nullable@OverridepublicObjectintercept(@NonNullEventevent,@NonNullContinuation<?superEvent>$completion){if(eventinstanceofTrackEvent&&listOfEventsToBeFiltered.contains(((TrackEvent)event).getEvent())){LoggerAnalytics.INSTANCE.verbose("EventFilteringPlugin: Event "+((TrackEvent)event).getEvent()+" is filtered out");returnnull;}returnevent;}@NonNull@OverridepublicAnalyticsgetAnalytics(){returnthis.analytics;}@NonNull@OverridepublicPlugin.PluginTypegetPluginType(){returnpluginType;}@Overridepublicvoidteardown(){listOfEventsToBeFiltered=null;}}
This section lists the steps to create a custom plugin EventFilteringPlugin for filtering out specific track events in the iOS (Swift) SDK.
Specifically, it filters out the Application Opened and Application Backgrounded events and prevents them from being tracked.
Add the plugin logic:
importRudderStackAnalyticsfinalclassEventFilteringPlugin:Plugin{varpluginType:PluginType=.onProcessvaranalytics:Analytics?privatevareventsToFilter=[String]()funcsetup(analytics:Analytics){self.analytics=analyticsself.eventsToFilter=["Application Opened","Application Backgrounded"]}funcintercept(event:anyEvent)->(anyEvent)?{iflettrackEvent=eventas?TrackEvent,self.eventsToFilter.contains(trackEvent.event){LoggerAnalytics.verbose(log:"EventFilteringPlugin: Event \"\(trackEvent.event)\" is filtered out.")returnnil}returnevent}functeardown(){self.eventsToFilter.removeAll()self.analytics=nil}}
@interfaceEventFilteringPlugin()@property(nonatomic,retain)RSSAnalytics*client;@property(nonatomic,retain)NSMutableArray*eventsToFilter;@end@implementationEventFilteringPlugin@synthesizepluginType;-(instancetype)init{self=[superinit];if(self){}returnself;}-(RSSPluginType)pluginType{returnRSSPluginTypeOnProcess;}-(void)setup:(RSSAnalytics*_Nonnull)analytics{self.client=analytics;self.eventsToFilter=[NSMutableArrayarrayWithArray:@[@"Application Opened",@"Application Backgrounded"]];}-(RSSEvent*_Nullable)intercept:(RSSEvent*_Nonnull)event{if([eventisKindOfClass:[RSSTrackEventclass]]){RSSTrackEvent*trackEvent=(RSSTrackEvent*)event;if([selfshouldFilterEvent:trackEvent]){[RSSLoggerAnalyticsverbose:[NSStringstringWithFormat:@"EventFilteringPlugin: Event \"%@\" is filtered out.",trackEvent.eventName]];returnnil;// Filter out this event
}}returnevent;// Allow event to pass through
}-(BOOL)shouldFilterEvent:(RSSTrackEvent*)trackEvent{return[self.eventsToFiltercontainsObject:trackEvent.eventName];}-(void)teardown{[self.eventsToFilterremoveAllObjects];self.eventsToFilter=nil;self.client=nil;}
In the above example, EventFilteringPlugin is a custom plugin that intercepts and filters the specified events before they are processed.
The high-level workflow is described below:
While initializing the plugin, you can specify a list of events to be filtered.
When an event is received, the plugin checks if it matches any of the events specified in the filter list.
If the event is present in the filter list, the plugin logs a message and discards it by returning null. Otherwise, it forwards the event for processing, as usual.
The teardown method clears the event list whenever you remove the plugin.
This site uses cookies to improve your experience while you navigate through the website. Out of
these
cookies, the cookies that are categorized as necessary are stored on your browser as they are as
essential
for the working of basic functionalities of the website. We also use third-party cookies that
help
us
analyze and understand how you use this website. These cookies will be stored in your browser
only
with
your
consent. You also have the option to opt-out of these cookies. But opting out of some of these
cookies
may
have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This
category only includes cookies that ensures basic functionalities and security
features of the website. These cookies do not store any personal information.
This site uses cookies to improve your experience. If you want to
learn more about cookies and why we use them, visit our cookie
policy. We'll assume you're ok with this, but you can opt-out if you wish Cookie Settings.