Skip to content

Conversation

@didiergarcia
Copy link
Contributor

Update to make consent more like the updated Swift consent.

@didiergarcia didiergarcia requested a review from bsneed November 29, 2023 17:29
* trigger the Segment Consent Preference event to be fired.
*/
fun notifyConsentChanged() {
consentChange?.let {

Choose a reason for hiding this comment

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

Suggested change
consentChange?.let {
consentChange?.invoke()

analytics?.process(event)
}

queuedEvents?.clear()

Choose a reason for hiding this comment

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

not sure how we gonna use the start method, but it's better to make queuedEvents a queue instead of list, so we can do

while (queueEvents is not empty) {
  poll from queue
  analytics.process
}

avoiding to clear at the end, thus avoiding concurrent modification issues.

@@ -0,0 +1,14 @@
package com.segment.analytics.kotlin.destinations.consent

class Constants {

Choose a reason for hiding this comment

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

maybe make it an object? so we don't need to define a companion object again.

Suggested change
class Constants {
object Constants {


// Look for a missing consent category
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!!

val segmentDestination = it.find(Constants.SEGMENT_IO_KEY)
segmentDestination.let {
val existingBlocker =
segmentDestination?.analytics?.find(SegmentConsentBlocker::class)

Choose a reason for hiding this comment

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

why not find from analytics directly?

Suggested change
segmentDestination?.analytics?.find(SegmentConsentBlocker::class)
analytics.find(SegmentConsentBlocker::class)


// Add Segment Destination blocker
val segmentDestination = it.find(Constants.SEGMENT_IO_KEY)
segmentDestination.let {

Choose a reason for hiding this comment

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

Suggested change
segmentDestination.let {
segmentDestination?.let {

val existingBlocker =
segmentDestination?.analytics?.find(SegmentConsentBlocker::class)
if (existingBlocker == null) {
segmentDestination?.add(SegmentConsentBlocker(store))

Choose a reason for hiding this comment

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

now you can use segmentDestination directly, since we did null check with let

Suggested change
segmentDestination?.add(SegmentConsentBlocker(store))
it.add(SegmentConsentBlocker(store))

val destinationKeys = state.destinationCategoryMap.keys
for (key in destinationKeys) {
val destination = analytics.find(key)
destination.let {

Choose a reason for hiding this comment

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

better to use the following

analytics.find(key)?.let { destination ->
  // then you don't have to do null check for the usage of destination
}

settings.toJsonElement().jsonObject.get(CONSENT_SETTINGS_KEY)?.let {
val jsonElement = it.jsonObject.get(HAS_UNMAPPED_DESTINATIONS_KEY)
println("hasUnmappedDestinations jsonElement: $jsonElement")
hasUnmappedDestinations = jsonElement.toString() == "true"

Choose a reason for hiding this comment

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

we have a set of utility method that can simplify operation like this. see here
for example:

  • get object: jsonObject.get("s")?.safeJsonObject
  • get string: jsonObject.getString("s")
  • get boolean: jsonObject.getBoolean("s")
  • etc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oooh very nice

@didiergarcia didiergarcia merged commit 1a6b78e into main Dec 1, 2023
@didiergarcia didiergarcia deleted the refactor-consent branch December 1, 2023 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants