Learn about the plugins architecture in Kotlin SDK.
4 minute read
This guide covers RudderStack’s plugin architecture available for the Kotlin SDK.
Overview
RudderStack’s plugin architecture lets you extend and customize event processing without modifying the core SDK. It provides flexibility by allowing you to add custom plugins at the different stages of event handling.
Using this architecture, you can filter, enhance, or modify events (identify, track, screen, group, and alias) based on your requirement. Whether it’s pre-processing data, transforming events, or applying integration-specific modifications, you have full control over how events are handled - making the integration seamless and ensuring events are processed exactly as required.
In this architecture, the plugins execute in a defined sequence, with each stage refining the data before passing it to the next.
Supported plugin types
Plugin type
Description
PreProcess
Executes at the beginning to prepare data, like adding context.
OnProcess
Executes while applying transformations or validations early in the pipeline.
Terminal
Executes just before sending the events to the integrations or RudderStack backend (server).
Utility
Executes only when manually triggered, like with a lifecycle plugin.
Plugin
A plugin is a modular component that lets you customize, enhance, or modify event processing at different stages without changing the core SDK. The plugin interface provides three optional methods that you can override as per your requirement:
Method
Description
setup(analytics: Analytics)
Initializes the plugin by associating it with an Analytics instance. Use this method to also set up any plugin-specific logic, like configuring internal variables or loading the necessary data.
suspend fun intercept(event: Event): Event?
Processes and (optionally) modifies the event before it moves through the pipeline. Returns the updated event or null to discard it.
Note: This method is not applicable for the Utility plugin type.
fun teardown()
Handles cleanup when the plugin is removed or when Analytics shuts down. You can override this method to reset or clear any internal states.
Custom plugins
Custom plugins are user-defined plugins that provide flexibility in extending and modifying event processing within the SDK. Unlike built-in plugins which are predefined within the SDK, custom plugins are entirely developed, managed, and added or removed by you after the SDK initialization.
How custom plugins work
You can create and register a custom plugin at runtime, allowing for dynamic modifications without altering the core SDK.
These plugins can belong to any of the existing plugin types (PreProcess, OnProcess, Terminal, or Utility) and are executed at their respective stages.
By leveraging custom plugins, you gain greater control over event handling, making it possible to apply transformations, enrich events with additional data, filter out unwanted events, or customize behavior for specific use cases.
Sample custom plugin
The following steps show how to create sample a custom plugin for filtering the track event type:
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;}}
In the above example, EventFilteringPlugin is a custom plugin that intercepts and filters the specified events before they are processed. Specifically, it filters out the Application Opened and Application Backgrounded events and prevents them from being tracked.
The high-level workflow is as follows:
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 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.