Skip to content

Commit 1a6b78e

Browse files
authored
Merge pull request #1 from segment-integrations/refactor-consent
Refactor consent
2 parents 060a692 + b37d064 commit 1a6b78e

File tree

11 files changed

+654
-218
lines changed

11 files changed

+654
-218
lines changed

README.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,32 +142,36 @@ Next you'll need to write some setup/init code where you have your
142142
Analytics setup:
143143

144144
```kotlin
145+
// Setup Analytics
145146
analytics = Analytics(SEGMENT_WRITE_KEY, applicationContext) {
146-
this.collectDeviceId = true
147-
this.trackApplicationLifecycleEvents = true
148-
this.trackDeepLinks = true
149-
this.flushPolicies = listOf(
150-
CountBasedFlushPolicy(5), // Flush after 5 events
151-
FrequencyFlushPolicy(5000) // Flush after 5 Seconds
152-
)
147+
this.collectDeviceId = true
148+
this.trackApplicationLifecycleEvents = true
149+
this.trackDeepLinks = true
150+
this.flushPolicies = listOf(
151+
CountBasedFlushPolicy(5), // Flush after 5 events
152+
FrequencyFlushPolicy(5000) // Flush after 5 Seconds
153+
)
153154
}
154155

155-
// List of categories we care about; we will query the CMP SDK locally on the status
156-
// of these categories when stamping an event with consent status.
157-
val categories = listOf<String>("C0001", "C0002")
158-
val consentCategoryProvider = MyCmpConsentCategoryProvider(cmpSDK, categories)
156+
// Add the myDestination plugin into the main timeline
157+
val myDestinationPlugin = myDestinationPlugin()
158+
analytics.add(myDestinationPlugin)
159+
160+
// Create the Consent Category Provider that will get the status of consent categories
161+
val consentCategoryProvider = MyConsentCategoryProvider(cmpSDK)
159162
val store = SynchronousStore() // Use only a Synchronous store here!
163+
160164
val consentPlugin = ConsentManagementPlugin(store, consentCategoryProvider)
161165

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

165-
// Add the myDestination plugin into the main timeline
166-
val myDestinationPlugin = myDestinationPlugin()
167-
analytics.add(myDestinationPlugin)
169+
// Use the CMP SDK to get the list of consent categories.
170+
consentCategoryProvider.setCategories(cmpSDK.getCategories())
168171

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

173177
## Building your own integration

lib/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ android {
4040

4141
dependencies {
4242
implementation("com.segment:sovran-kotlin:1.3.1")
43-
implementation("com.segment.analytics.kotlin:android:1.13.1")
43+
implementation("com.segment.analytics.kotlin:android:1.14.0")
4444
implementation("androidx.multidex:multidex:2.0.1")
4545
implementation("androidx.core:core-ktx:1.10.1")
4646

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import com.segment.analytics.kotlin.core.BaseEvent
55
import com.segment.analytics.kotlin.core.Settings
66
import com.segment.analytics.kotlin.core.TrackEvent
77
import com.segment.analytics.kotlin.core.platform.Plugin
8+
import com.segment.analytics.kotlin.destinations.consent.Constants.EVENT_SEGMENT_CONSENT_PREFERENCE
9+
import com.segment.analytics.kotlin.destinations.consent.Constants.SEGMENT_IO_KEY
810
import kotlinx.serialization.json.JsonObject
911
import sovran.kotlin.SynchronousStore
1012

1113

1214
internal const val CONSENT_SETTINGS = "consent"
1315
internal const val CATEGORY_PREFERENCE = "categoryPreference"
1416

15-
class ConsentBlockingPlugin(
17+
open class ConsentBlocker(
1618
var destinationKey: String,
1719
var store: SynchronousStore,
1820
var allowSegmentPreferenceEvent: Boolean = true
@@ -34,7 +36,7 @@ class ConsentBlockingPlugin(
3436
requiredConsentCategories.forEach {
3537
if (!consentJsonArray.contains(it)) {
3638

37-
if (allowSegmentPreferenceEvent && event is TrackEvent && event.event == ConsentManagementPlugin.EVENT_SEGMENT_CONSENT_PREFERENCE) {
39+
if (allowSegmentPreferenceEvent && event is TrackEvent && event.event == EVENT_SEGMENT_CONSENT_PREFERENCE) {
3840
// IF event is the SEGMENT CONSENT PREFERENCE event let it through
3941
return event
4042
} else {
@@ -51,8 +53,8 @@ class ConsentBlockingPlugin(
5153
return event
5254
}
5355

54-
private fun getConsentCategoriesFromEvent(event: BaseEvent): MutableList<String> {
55-
val consentJsonArray = mutableListOf<String>()
56+
private fun getConsentCategoriesFromEvent(event: BaseEvent): Set<String> {
57+
val consentJsonArray = HashSet<String>()
5658

5759
val consentSettingsJson = event.context[CONSENT_SETTINGS]
5860
if (consentSettingsJson != null) {
@@ -78,4 +80,7 @@ class ConsentBlockingPlugin(
7880
override fun update(settings: Settings, type: Plugin.UpdateType) {
7981
super.update(settings, type)
8082
}
81-
}
83+
}
84+
85+
86+
class SegmentConsentBlocker(store: SynchronousStore): ConsentBlocker(SEGMENT_IO_KEY, store) {}

lib/src/main/java/com/segment/analytics/kotlin/destinations/consent/ConsentManagementPlugin.kt

Lines changed: 0 additions & 129 deletions
This file was deleted.

0 commit comments

Comments
 (0)