Skip to content

Commit a2a1404

Browse files
authored
Make sure the Segment Consent Preference is always sent. (#22)
1 parent 101838e commit a2a1404

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ class SegmentConsentBlocker(store: SynchronousStore): ConsentBlocker(SEGMENT_IO_
9494
val hasUnmappedDestinations = currentState?.hasUnmappedDestinations
9595

9696
// IF we have no unmapped destinations and we have not consented to any categories block (drop)
97-
// the event.
98-
if (hasUnmappedDestinations == false) {
97+
// the event unless it is the Segment Consent Preference Event
98+
if (hasUnmappedDestinations == false && (event as? TrackEvent)?.event != EVENT_SEGMENT_CONSENT_PREFERENCE ) {
9999
val consentedCategoriesSet = getConsentedCategoriesFromEvent(event)
100100
if (consentedCategoriesSet.isEmpty()) {
101101
// Drop the event

lib/src/test/kotlin/com/segment/analytics/kotlin/consent/ConsentBlockerTests.kt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,54 @@ class ConsentBlockerTests {
9191
assertNull(processedEvent)
9292
}
9393

94+
@Test
95+
fun `blocks when no consent and no unmapped destinations`() {
96+
val store = SynchronousStore()
97+
store.provide(ConsentState.defaultState)
98+
var mappings: MutableMap<String, Array<String>> = HashMap()
99+
mappings["foo"] = arrayOf("cat1", "cat2")
100+
val state = ConsentState(mappings, false, mutableListOf<String>(),true)
101+
store.dispatch(UpdateConsentStateActionFull(state), ConsentState::class)
102+
val blockingPlugin = ConsentBlocker("foo", store)
103+
104+
// Stamped Event with all categories false
105+
var stamppedEvent = TrackEvent(properties = emptyJsonObject, event = "MyEvent")
106+
stamppedEvent.context = buildJsonObject {
107+
put(Constants.CONSENT_KEY, buildJsonObject {
108+
put(Constants.CATEGORY_PREFERENCE_KEY, buildJsonObject {
109+
put("cat1", JsonPrimitive(false))
110+
put("cat2", JsonPrimitive(false))
111+
})
112+
})
113+
}
114+
val processedEvent = blockingPlugin.execute(stamppedEvent)
115+
assertNull(processedEvent)
116+
}
117+
118+
@Test
119+
fun `does not block the Segment Consent Preference event`() {
120+
val store = SynchronousStore()
121+
store.provide(ConsentState.defaultState)
122+
var mappings: MutableMap<String, Array<String>> = HashMap()
123+
mappings["foo"] = arrayOf("cat1", "cat2")
124+
val state = ConsentState(mappings, false, mutableListOf<String>(),true)
125+
store.dispatch(UpdateConsentStateActionFull(state), ConsentState::class)
126+
val blockingPlugin = ConsentBlocker("foo", store)
127+
128+
// Stamped Event with all categories false
129+
var stamppedEvent = TrackEvent(properties = emptyJsonObject, event = Constants.EVENT_SEGMENT_CONSENT_PREFERENCE)
130+
stamppedEvent.context = buildJsonObject {
131+
put(Constants.CONSENT_KEY, buildJsonObject {
132+
put(Constants.CATEGORY_PREFERENCE_KEY, buildJsonObject {
133+
put("cat1", JsonPrimitive(false))
134+
put("cat2", JsonPrimitive(false))
135+
})
136+
})
137+
}
138+
val processedEvent = blockingPlugin.execute(stamppedEvent)
139+
assertNotNull(processedEvent)
140+
}
141+
94142
@Test
95143
fun `block when nothing in store`() {
96144
val store = SynchronousStore()

0 commit comments

Comments
 (0)