How to Create a Custom Plugin in Mobile SDKs

Create a custom plugin in the Android (Kotlin) and iOS (Swift) SDKs.

This guide lists the steps to create a sample custom plugin in the Android (Kotlin) and iOS (Swift) SDKs.

Overview

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.

  1. Add the plugin logic:
kotlin
import com.rudderstack.sdk.kotlin.core.Analytics
import com.rudderstack.sdk.kotlin.core.internals.logger.LoggerAnalytics
import com.rudderstack.sdk.kotlin.core.internals.models.Event
import com.rudderstack.sdk.kotlin.core.internals.models.TrackEvent
import com.rudderstack.sdk.kotlin.core.internals.plugins.Plugin

class EventFilteringPlugin : Plugin {

    override val pluginType: Plugin.PluginType = Plugin.PluginType.OnProcess
    override lateinit var analytics: Analytics

    private lateinit var listOfEventsToBeFiltered: List<String>

    override fun setup(analytics: Analytics) {
        super.setup(analytics)
        listOfEventsToBeFiltered = listOf("Application Opened", "Application Backgrounded")
    }

    override suspend fun intercept(event: Event): Event? {
        if (event is TrackEvent && listOfEventsToBeFiltered.contains(event.event)) {
            LoggerAnalytics.verbose("EventFilteringPlugin: Event ${event.event} is filtered out")
            return null
        }
        return event
    }

    override fun teardown() {
        listOfEventsToBeFiltered = emptyList()
    }
}
  1. Create an EventFilteringPlugin plugin:
kotlin
val eventFilteringPlugin = EventFilteringPlugin()
  1. Add the plugin after initializing the SDK:
kotlin
analytics.add(eventFilteringPlugin)
  1. To remove the plugin:
kotlin
analytics.remove(eventFilteringPlugin)

iOS (Swift)

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.

  1. Add the plugin logic:
swift
import RudderStackAnalytics

final class EventFilteringPlugin: Plugin {
    var pluginType: PluginType = .onProcess
    var analytics: Analytics?
    
    private var eventsToFilter = [String]()
    
    func setup(analytics: Analytics) {
        self.analytics = analytics
        self.eventsToFilter = ["Application Opened", "Application Backgrounded"]
    }
    
    func intercept(event: any Event) -> (any Event)? {
        if let trackEvent = event as? TrackEvent, self.eventsToFilter.contains(trackEvent.event) {
            LoggerAnalytics.verbose(log: "EventFilteringPlugin: Event \"\(trackEvent.event)\" is filtered out.")
            return nil
        }
        return event
    }
    
    func teardown() {
        self.eventsToFilter.removeAll()
        self.analytics = nil
    }
}
  1. Create an EventFilteringPlugin plugin:
swift
let eventFilteringPlugin = EventFilteringPlugin()
  1. Add the plugin after initializing the SDK:
swift
analytics.add(plugin: eventFilteringPlugin)
  1. To remove the plugin:
swift
analytics.remove(plugin: eventFilteringPlugin)

You can also use custom plugins with device mode integrations.

See Add custom plugins to integrations for more information.

Workflow overview

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.

Questions? Let's figure it out together.

Join the RudderStack Slack community to connect with other users, customers, and the RudderStack team — or reach out for direct support.