Skip to content

Commit

Permalink
Properties sanitizer
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Nov 14, 2023
1 parent fe28b8d commit ec2314a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.posthog.android.sample
import android.app.Application
import android.os.StrictMode
import com.posthog.PostHogOnFeatureFlags
import com.posthog.PostHogPropertiesSanitizer
import com.posthog.android.PostHogAndroid
import com.posthog.android.PostHogAndroidConfig

Expand All @@ -17,6 +18,11 @@ class MyApp : Application() {
flushAt = 5
maxBatchSize = 5
onFeatureFlags = PostHogOnFeatureFlags { print("feature flags loaded") }
propertiesSanitizer = PostHogPropertiesSanitizer { properties ->
properties.apply {
remove("\$device_name")
}
}
}
PostHogAndroid.setup(this, config)
}
Expand Down
10 changes: 8 additions & 2 deletions posthog/api/posthog.api
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public final class com/posthog/PostHog$Companion : com/posthog/PostHogInterface
public class com/posthog/PostHogConfig {
public static final field Companion Lcom/posthog/PostHogConfig$Companion;
public static final field defaultHost Ljava/lang/String;
public fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogEncryption;Lcom/posthog/PostHogOnFeatureFlags;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogEncryption;Lcom/posthog/PostHogOnFeatureFlags;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogEncryption;Lcom/posthog/PostHogOnFeatureFlags;Lcom/posthog/PostHogPropertiesSanitizer;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogEncryption;Lcom/posthog/PostHogOnFeatureFlags;Lcom/posthog/PostHogPropertiesSanitizer;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun addIntegration (Lcom/posthog/PostHogIntegration;)V
public final fun getApiKey ()Ljava/lang/String;
public final fun getCachePreferences ()Lcom/posthog/internal/PostHogPreferences;
Expand All @@ -72,6 +72,7 @@ public class com/posthog/PostHogConfig {
public final fun getOnFeatureFlags ()Lcom/posthog/PostHogOnFeatureFlags;
public final fun getOptOut ()Z
public final fun getPreloadFeatureFlags ()Z
public final fun getPropertiesSanitizer ()Lcom/posthog/PostHogPropertiesSanitizer;
public final fun getSdkName ()Ljava/lang/String;
public final fun getSdkVersion ()Ljava/lang/String;
public final fun getSendFeatureFlagEvent ()Z
Expand All @@ -92,6 +93,7 @@ public class com/posthog/PostHogConfig {
public final fun setOnFeatureFlags (Lcom/posthog/PostHogOnFeatureFlags;)V
public final fun setOptOut (Z)V
public final fun setPreloadFeatureFlags (Z)V
public final fun setPropertiesSanitizer (Lcom/posthog/PostHogPropertiesSanitizer;)V
public final fun setSdkName (Ljava/lang/String;)V
public final fun setSdkVersion (Ljava/lang/String;)V
public final fun setSendFeatureFlagEvent (Z)V
Expand Down Expand Up @@ -183,6 +185,10 @@ public abstract interface class com/posthog/PostHogOnFeatureFlags {
public abstract fun loaded ()V
}

public abstract interface class com/posthog/PostHogPropertiesSanitizer {
public abstract fun sanitize (Ljava/util/Map;)Ljava/util/Map;
}

public abstract interface annotation class com/posthog/PostHogVisibleForTesting : java/lang/annotation/Annotation {
}

Expand Down
17 changes: 11 additions & 6 deletions posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,20 @@ public class PostHog private constructor(

val newDistinctId = distinctId ?: this.distinctId

val mergedProperties = buildProperties(
properties = properties,
userProperties = userProperties,
userPropertiesSetOnce = userPropertiesSetOnce,
groupProperties = groupProperties,
)

// sanitize the properties or fallback to the original properties
val sanitizedProperties = config?.propertiesSanitizer?.sanitize(mergedProperties.toMutableMap()) ?: mergedProperties

val postHogEvent = PostHogEvent(
event,
newDistinctId,
properties = buildProperties(
properties = properties,
userProperties = userProperties,
userPropertiesSetOnce = userPropertiesSetOnce,
groupProperties = groupProperties,
),
properties = sanitizedProperties,
)
queue?.add(postHogEvent)
}
Expand Down
6 changes: 6 additions & 0 deletions posthog/src/main/java/com/posthog/PostHogConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public open class PostHogConfig(
* Defaults to no callback
*/
public var onFeatureFlags: PostHogOnFeatureFlags? = null,

/**
* Hook that allows to sanitize the event properties
* The hook is called before the event is cached or sent over the wire
*/
public var propertiesSanitizer: PostHogPropertiesSanitizer? = null,
) {
// fix me: https://stackoverflow.com/questions/53866865/leaking-this-in-constructor-warning-should-apply-to-final-classes-as-well-as
@PostHogInternal
Expand Down
13 changes: 13 additions & 0 deletions posthog/src/main/java/com/posthog/PostHogPropertiesSanitizer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.posthog

/**
* Hook to sanitize the event properties
*/
public fun interface PostHogPropertiesSanitizer {
/**
* Sanitizes the event properties
* @param properties the event properties to sanitize
* @return the sanitized properties
*/
public fun sanitize(properties: MutableMap<String, Any>): Map<String, Any>
}

0 comments on commit ec2314a

Please sign in to comment.