Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,32 +142,36 @@ Next you'll need to write some setup/init code where you have your
Analytics setup:

```kotlin
// Setup Analytics
analytics = Analytics(SEGMENT_WRITE_KEY, applicationContext) {
this.collectDeviceId = true
this.trackApplicationLifecycleEvents = true
this.trackDeepLinks = true
this.flushPolicies = listOf(
CountBasedFlushPolicy(5), // Flush after 5 events
FrequencyFlushPolicy(5000) // Flush after 5 Seconds
)
this.collectDeviceId = true
this.trackApplicationLifecycleEvents = true
this.trackDeepLinks = true
this.flushPolicies = listOf(
CountBasedFlushPolicy(5), // Flush after 5 events
FrequencyFlushPolicy(5000) // Flush after 5 Seconds
)
}

// List of categories we care about; we will query the CMP SDK locally on the status
// of these categories when stamping an event with consent status.
val categories = listOf<String>("C0001", "C0002")
val consentCategoryProvider = MyCmpConsentCategoryProvider(cmpSDK, categories)
// Add the myDestination plugin into the main timeline
val myDestinationPlugin = myDestinationPlugin()
analytics.add(myDestinationPlugin)

// Create the Consent Category Provider that will get the status of consent categories
val consentCategoryProvider = MyConsentCategoryProvider(cmpSDK)
val store = SynchronousStore() // Use only a Synchronous store here!

val consentPlugin = ConsentManagementPlugin(store, consentCategoryProvider)

// Add the Consent Plugin directly to analytics
analytics.add(consentPlugin)

// Add the myDestination plugin into the main timeline
val myDestinationPlugin = myDestinationPlugin()
analytics.add(myDestinationPlugin)
// Use the CMP SDK to get the list of consent categories.
consentCategoryProvider.setCategories(cmpSDK.getCategories())

// Add the blocking plugin to this destination
webhookDestinationPlugin.add(ConsentBlockingPlugin("my-destination", store))
// Once the categories have been set we can start processing events by starting
// the Consent Management plugin
consentPlugin.start()
```

## Building your own integration
Expand Down
2 changes: 1 addition & 1 deletion lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ android {

dependencies {
implementation("com.segment:sovran-kotlin:1.3.1")
implementation("com.segment.analytics.kotlin:android:1.13.1")
implementation("com.segment.analytics.kotlin:android:1.14.0")
implementation("androidx.multidex:multidex:2.0.1")
implementation("androidx.core:core-ktx:1.10.1")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import com.segment.analytics.kotlin.core.BaseEvent
import com.segment.analytics.kotlin.core.Settings
import com.segment.analytics.kotlin.core.TrackEvent
import com.segment.analytics.kotlin.core.platform.Plugin
import com.segment.analytics.kotlin.destinations.consent.Constants.EVENT_SEGMENT_CONSENT_PREFERENCE
import com.segment.analytics.kotlin.destinations.consent.Constants.SEGMENT_IO_KEY
import kotlinx.serialization.json.JsonObject
import sovran.kotlin.SynchronousStore


internal const val CONSENT_SETTINGS = "consent"
internal const val CATEGORY_PREFERENCE = "categoryPreference"

class ConsentBlockingPlugin(
open class ConsentBlocker(
var destinationKey: String,
var store: SynchronousStore,
var allowSegmentPreferenceEvent: Boolean = true
Expand All @@ -34,7 +36,7 @@ class ConsentBlockingPlugin(
requiredConsentCategories.forEach {
if (!consentJsonArray.contains(it)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe make consentJsonArray a set instead of a list. reduce the complexity of contains from O(n) to O(1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion!!


if (allowSegmentPreferenceEvent && event is TrackEvent && event.event == ConsentManagementPlugin.EVENT_SEGMENT_CONSENT_PREFERENCE) {
if (allowSegmentPreferenceEvent && event is TrackEvent && event.event == EVENT_SEGMENT_CONSENT_PREFERENCE) {
// IF event is the SEGMENT CONSENT PREFERENCE event let it through
return event
} else {
Expand All @@ -51,8 +53,8 @@ class ConsentBlockingPlugin(
return event
}

private fun getConsentCategoriesFromEvent(event: BaseEvent): MutableList<String> {
val consentJsonArray = mutableListOf<String>()
private fun getConsentCategoriesFromEvent(event: BaseEvent): Set<String> {
val consentJsonArray = HashSet<String>()

val consentSettingsJson = event.context[CONSENT_SETTINGS]
if (consentSettingsJson != null) {
Expand All @@ -78,4 +80,7 @@ class ConsentBlockingPlugin(
override fun update(settings: Settings, type: Plugin.UpdateType) {
super.update(settings, type)
}
}
}


class SegmentConsentBlocker(store: SynchronousStore): ConsentBlocker(SEGMENT_IO_KEY, store) {}

This file was deleted.

Loading