-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option to disable screen view usage (#474)
- Loading branch information
Showing
15 changed files
with
331 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
datapipelines/src/main/kotlin/io/customer/datapipelines/config/ScreenView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.customer.datapipelines.config | ||
|
||
/** | ||
* Enum class to define how CustomerIO SDK should handle screen view events. | ||
*/ | ||
sealed class ScreenView(val name: String) { | ||
/** | ||
* Screen view events are sent to destinations for analytics purposes. | ||
* They are also used to display in-app messages based on page rules. | ||
*/ | ||
object All : ScreenView(name = "all") | ||
|
||
/** | ||
* Screen view events are kept on device only. They are used to display in-app messages based on | ||
* page rules. Events are not sent to our back end servers. | ||
*/ | ||
|
||
object InApp : ScreenView(name = "inapp") | ||
|
||
companion object { | ||
/** | ||
* Returns the [ScreenView] enum constant for the given name. | ||
* Returns fallback if the specified enum type has no constant with the given name. | ||
* Defaults to [All]. | ||
*/ | ||
@JvmOverloads | ||
fun getScreenView(screenView: String?, fallback: ScreenView = All): ScreenView { | ||
if (screenView.isNullOrBlank()) { | ||
return fallback | ||
} | ||
|
||
return listOf( | ||
All, | ||
InApp | ||
).find { value -> value.name.equals(screenView, ignoreCase = true) } ?: fallback | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
datapipelines/src/main/kotlin/io/customer/datapipelines/plugins/ScreenFilterPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.customer.datapipelines.plugins | ||
|
||
import com.segment.analytics.kotlin.core.Analytics | ||
import com.segment.analytics.kotlin.core.BaseEvent | ||
import com.segment.analytics.kotlin.core.ScreenEvent | ||
import com.segment.analytics.kotlin.core.platform.EventPlugin | ||
import com.segment.analytics.kotlin.core.platform.Plugin | ||
import io.customer.datapipelines.config.ScreenView | ||
|
||
/** | ||
* Plugin to filter screen events based on the configuration provided by customer app. | ||
* This plugin is used to filter out screen events that should not be processed further. | ||
*/ | ||
internal class ScreenFilterPlugin(private val screenViewUse: ScreenView) : EventPlugin { | ||
override lateinit var analytics: Analytics | ||
override val type: Plugin.Type = Plugin.Type.Enrichment | ||
|
||
override fun screen(payload: ScreenEvent): BaseEvent? { | ||
// Filter out screen events based on the configuration provided by customer app | ||
// Using when expression so it enforce right check for all possible values of ScreenView in future | ||
return when (screenViewUse) { | ||
ScreenView.All -> payload | ||
// Do not send screen events to server if ScreenView is not Analytics | ||
ScreenView.InApp -> null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
datapipelines/src/test/java/io/customer/datapipelines/config/ScreenViewTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.customer.datapipelines.config | ||
|
||
import io.customer.commontest.core.JUnit5Test | ||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.junit.jupiter.api.Test | ||
|
||
class ScreenViewTest : JUnit5Test() { | ||
@Test | ||
fun getScreenView_givenNamesWithMatchingCase_expectCorrectScreenView() { | ||
val screenViewAnalytics = ScreenView.getScreenView("All") | ||
val screenViewInApp = ScreenView.getScreenView("InApp") | ||
|
||
screenViewAnalytics shouldBeEqualTo ScreenView.All | ||
screenViewInApp shouldBeEqualTo ScreenView.InApp | ||
} | ||
|
||
@Test | ||
fun getScreenView_givenNamesWithDifferentCase_expectCorrectScreenView() { | ||
val screenViewAnalytics = ScreenView.getScreenView("all") | ||
val screenViewInApp = ScreenView.getScreenView("inapp") | ||
|
||
screenViewAnalytics shouldBeEqualTo ScreenView.All | ||
screenViewInApp shouldBeEqualTo ScreenView.InApp | ||
} | ||
|
||
@Test | ||
fun getScreenView_givenInvalidValue_expectFallbackScreenView() { | ||
val parsedValue = ScreenView.getScreenView("none") | ||
|
||
parsedValue shouldBeEqualTo ScreenView.All | ||
} | ||
|
||
@Test | ||
fun getScreenView_givenEmptyValue_expectFallbackScreenView() { | ||
val parsedValue = ScreenView.getScreenView(screenView = "", fallback = ScreenView.InApp) | ||
|
||
parsedValue shouldBeEqualTo ScreenView.InApp | ||
} | ||
|
||
@Test | ||
fun getScreenView_givenNull_expectFallbackScreenView() { | ||
val parsedValue = ScreenView.getScreenView(null) | ||
|
||
parsedValue shouldBeEqualTo ScreenView.All | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
datapipelines/src/test/java/io/customer/datapipelines/plugins/ScreenFilterPluginTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.customer.datapipelines.plugins | ||
|
||
import io.customer.commontest.config.TestConfig | ||
import io.customer.commontest.extensions.random | ||
import io.customer.datapipelines.config.ScreenView | ||
import io.customer.datapipelines.testutils.core.DataPipelinesTestConfig | ||
import io.customer.datapipelines.testutils.core.JUnitTest | ||
import io.customer.datapipelines.testutils.core.testConfiguration | ||
import io.customer.datapipelines.testutils.extensions.shouldMatchTo | ||
import io.customer.datapipelines.testutils.utils.OutputReaderPlugin | ||
import io.customer.datapipelines.testutils.utils.screenEvents | ||
import io.customer.sdk.data.model.CustomAttributes | ||
import org.amshove.kluent.shouldBeEmpty | ||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.amshove.kluent.shouldHaveSingleItem | ||
import org.junit.jupiter.api.Test | ||
|
||
class ScreenFilterPluginTest : JUnitTest() { | ||
private lateinit var outputReaderPlugin: OutputReaderPlugin | ||
|
||
override fun setup(testConfig: TestConfig) { | ||
// Keep setup empty to avoid calling super.setup() as it will initialize the SDK | ||
// and we want to test the SDK with different configurations in each test | ||
} | ||
|
||
private fun setupWithConfig(screenViewUse: ScreenView, testConfig: DataPipelinesTestConfig = testConfiguration {}) { | ||
super.setup( | ||
testConfiguration { | ||
analytics { add(ScreenFilterPlugin(screenViewUse = screenViewUse)) } | ||
} + testConfig | ||
) | ||
|
||
outputReaderPlugin = OutputReaderPlugin() | ||
analytics.add(outputReaderPlugin) | ||
} | ||
|
||
@Test | ||
fun process_givenScreenViewUseAnalytics_expectScreenEventWithoutPropertiesProcessed() { | ||
setupWithConfig(screenViewUse = ScreenView.All) | ||
|
||
val givenScreenTitle = String.random | ||
sdkInstance.screen(givenScreenTitle) | ||
|
||
val result = outputReaderPlugin.screenEvents.shouldHaveSingleItem() | ||
result.name shouldBeEqualTo givenScreenTitle | ||
result.properties.shouldBeEmpty() | ||
} | ||
|
||
@Test | ||
fun process_givenScreenViewUseAnalytics_expectScreenEventWithPropertiesProcessed() { | ||
setupWithConfig(screenViewUse = ScreenView.All) | ||
|
||
val givenScreenTitle = String.random | ||
val givenProperties: CustomAttributes = mapOf("source" to "push", "discount" to 10) | ||
sdkInstance.screen(givenScreenTitle, givenProperties) | ||
|
||
val screenEvent = outputReaderPlugin.screenEvents.shouldHaveSingleItem() | ||
screenEvent.name shouldBeEqualTo givenScreenTitle | ||
screenEvent.properties shouldMatchTo givenProperties | ||
} | ||
|
||
@Test | ||
fun process_givenScreenViewUseInApp_expectAllScreenEventsIgnored() { | ||
setupWithConfig(screenViewUse = ScreenView.InApp) | ||
|
||
for (i in 1..5) { | ||
sdkInstance.screen(String.random) | ||
} | ||
|
||
outputReaderPlugin.allEvents.shouldBeEmpty() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.