Skip to content

Commit

Permalink
refactor: add IntegrationsManagementPlugin by default in the PluginChain
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishal Gupta committed Jan 30, 2025
1 parent b5ec459 commit f873b32
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Analytics(

internal val activityLifecycleManagementPlugin = ActivityLifecycleManagementPlugin()
internal val processLifecycleManagementPlugin = ProcessLifecycleManagementPlugin()
private var integrationsManagementPlugin: IntegrationsManagementPlugin? = null
private var integrationsManagementPlugin = IntegrationsManagementPlugin()
private val sessionTrackingPlugin = SessionTrackingPlugin()

init {
Expand Down Expand Up @@ -133,15 +133,15 @@ class Analytics(
super.reset(clearAnonymousId)

sessionTrackingPlugin.refreshSession()
this.integrationsManagementPlugin?.reset()
integrationsManagementPlugin.reset()
}

override fun flush() {
if (!isAnalyticsActive()) return

super.flush()

this.integrationsManagementPlugin?.flush()
integrationsManagementPlugin.flush()
}

/**
Expand Down Expand Up @@ -226,9 +226,7 @@ class Analytics(
fun addIntegration(plugin: IntegrationPlugin) {
if (!isAnalyticsActive()) return

initIntegrationsManagementPlugin()

integrationsManagementPlugin?.addIntegration(plugin)
integrationsManagementPlugin.addIntegration(plugin)
}

/**
Expand All @@ -239,15 +237,7 @@ class Analytics(
fun removeIntegration(plugin: IntegrationPlugin) {
if (!isAnalyticsActive()) return

integrationsManagementPlugin?.removeIntegration(plugin)
}

private fun initIntegrationsManagementPlugin() {
synchronized(this) {
if (integrationsManagementPlugin == null) {
integrationsManagementPlugin = IntegrationsManagementPlugin().also { add(it) }
}
}
integrationsManagementPlugin.removeIntegration(plugin)
}

private fun setup() {
Expand All @@ -261,6 +251,7 @@ class Analytics(
add(ScreenInfoPlugin())
add(TimezoneInfoPlugin())
add(sessionTrackingPlugin)
add(integrationsManagementPlugin)

// Add these plugins at last in chain
add(AndroidLifecyclePlugin())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ abstract class IntegrationPlugin : EventPlugin {
* This method must return true if the destination was created successfully, false otherwise.
*
* @param destinationConfig The configuration for the destination.
* @return true if the destination was created successfully, false otherwise.
*/
protected abstract fun create(destinationConfig: JsonObject)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,9 @@ internal class IntegrationsManagementPlugin : Plugin {
queuedEventsChannel.trySend(event)
}

private fun processEvents() {
analytics.withIntegrationsDispatcher {
for (event in queuedEventsChannel) {
integrationPluginChain.process(event)
}
private suspend fun processEvents() {
for (event in queuedEventsChannel) {
integrationPluginChain.process(event)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.rudderstack.sampleapp.analytics.customplugins
import com.rudderstack.sdk.kotlin.android.plugins.devicemode.IntegrationPlugin
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.ScreenEvent
import com.rudderstack.sdk.kotlin.core.internals.models.TrackEvent
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -34,25 +33,18 @@ class SampleIntegrationPlugin : IntegrationPlugin() {
}

override fun track(payload: TrackEvent): Event {
LoggerAnalytics.debug("SampleIntegrationPlugin: track event $payload")
LoggerAnalytics.debug("SampleIntegrationPlugin: destinationConfig $destinationConfig")

destinationSdk?.track(payload.event, payload.properties)
return payload
}

override fun screen(payload: ScreenEvent): Event? {
LoggerAnalytics.debug("SampleIntegrationPlugin: screen event $payload")
return super.screen(payload)
}

override fun flush() {
LoggerAnalytics.debug("SampleIntegrationPlugin: flush")
super.flush()
destinationSdk?.flush()
}

override fun reset() {
LoggerAnalytics.debug("SampleIntegrationPlugin: reset")
super.reset()
destinationSdk?.reset()
}
}

Expand All @@ -63,6 +55,14 @@ class SampleDestinationSdk private constructor(private val key: String) {
LoggerAnalytics.debug("SampleAmplitudeSdk: track event $event with properties $properties")
}

fun flush() {
LoggerAnalytics.debug("SampleAmplitudeSdk: flush")
}

fun reset() {
LoggerAnalytics.debug("SampleAmplitudeSdk: reset")
}

companion object {

fun create(key: String): SampleDestinationSdk {
Expand Down

0 comments on commit f873b32

Please sign in to comment.