Skip to content

Commit

Permalink
chore: added screen processor
Browse files Browse the repository at this point in the history
  • Loading branch information
thisames committed Sep 26, 2024
1 parent e2c2d86 commit 8bf3765
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.posthog.android.internal.PostHogAndroidLogger
import com.posthog.android.internal.PostHogAndroidNetworkStatus
import com.posthog.android.internal.PostHogAppInstallIntegration
import com.posthog.android.internal.PostHogLifecycleObserverIntegration
import com.posthog.android.internal.PostHogScreenProcessor
import com.posthog.android.internal.PostHogSharedPreferences
import com.posthog.android.internal.appContext
import com.posthog.android.replay.PostHogReplayIntegration
Expand Down Expand Up @@ -88,6 +89,10 @@ public class PostHogAndroid private constructor() {
if (config.captureDeepLinks || config.captureScreenViews || config.sessionReplay) {
config.addIntegration(PostHogActivityLifecycleCallbackIntegration(context, config))
}
// if the processor depends on captureScreenViews, only adds if its enabled
if (config.captureScreenViews) {
config.addProcessor(PostHogScreenProcessor())
}
}
if (config.captureApplicationLifecycleEvents) {
config.addIntegration(PostHogAppInstallIntegration(context, config))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ internal class PostHogActivityLifecycleCallbackIntegration(

if (!screenName.isNullOrEmpty()) {
PostHog.screen(screenName)
ScreenTracker.setCurrentScreen(screenName)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.posthog.android.internal

import com.posthog.PostHogPropertiesProcessor

internal class PostHogScreenProcessor : PostHogPropertiesProcessor {
override fun process(properties: MutableMap<String, Any>): Map<String, Any> {
if (properties.containsKey("\$screen_name")) {
return properties
}

val currentScreen = ScreenTracker.getCurrentScreenName()

properties["\$screen_name"] = currentScreen

return properties
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.posthog.android.internal

public object ScreenTracker {
@Volatile private lateinit var currentScreen: String

public fun setCurrentScreen(screenName: String) {
currentScreen = screenName
}

public fun getCurrentScreenName(): String {
return currentScreen
}
}
16 changes: 7 additions & 9 deletions posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ public class PostHog private constructor(
private var isIdentifiedLoaded: Boolean = false
private var isPersonProcessingLoaded: Boolean = false

private lateinit var currentScreen: String

public override fun <T : PostHogConfig> setup(config: T) {
synchronized(setupLock) {
try {
Expand Down Expand Up @@ -319,10 +317,6 @@ public class PostHog private constructor(
}
}

currentScreen.let {
props["\$screen_name"] = currentScreen
}

userProperties?.let {
props["\$set"] = it
}
Expand Down Expand Up @@ -447,8 +441,14 @@ public class PostHog private constructor(
appendGroups = !groupIdentify,
)

var currentProperties = mergedProperties
// has to run before the sanitizer so people can remove stuff processors add
config?.processors?.forEach {
currentProperties = it.process(currentProperties.toMutableMap())
}

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

val postHogEvent =
PostHogEvent(
Expand Down Expand Up @@ -512,8 +512,6 @@ public class PostHog private constructor(
val props = mutableMapOf<String, Any>()
props["\$screen_name"] = screenTitle

currentScreen = screenTitle

properties?.let {
props.putAll(it)
}
Expand Down
18 changes: 18 additions & 0 deletions posthog/src/main/java/com/posthog/PostHogConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ public open class PostHogConfig(
private val integrationsList: MutableList<PostHogIntegration> = mutableListOf()
private val integrationLock = Any()

private val processorsList: MutableList<PostHogPropertiesProcessor> = mutableListOf()
private val processorsLock = Any()

/**
* The integrations list
*/
Expand Down Expand Up @@ -188,6 +191,21 @@ public open class PostHogConfig(
}
}

public val processors: List<PostHogPropertiesProcessor>
get() {
val list: List<PostHogPropertiesProcessor>
synchronized(processorsLock) {
list = processorsList.toList()
}
return list
}

public fun addProcessor(processor: PostHogPropertiesProcessor) {
synchronized(processorsLock) {
processorsList.add(processor)
}
}

public companion object {
public const val DEFAULT_HOST: String = "https://us.i.posthog.com"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.posthog

public fun interface PostHogPropertiesProcessor {
public fun process(properties: MutableMap<String, Any>): Map<String, Any>
}

0 comments on commit 8bf3765

Please sign in to comment.