Skip to content

Commit

Permalink
chore: create ctor overloads for better Java DX (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Jun 28, 2024
1 parent a9bddc6 commit f934b9a
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 101 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- chore: create ctor overloads for better Java DX ([#148](https://github.com/PostHog/posthog-android/pull/148))

## 3.4.1 - 2024-06-27

- recording: `OnTouchEventListener` try catch guard to swallow unexpected errors ([#147](https://github.com/PostHog/posthog-android/pull/147))
Expand Down
10 changes: 10 additions & 0 deletions posthog-android/api/posthog-android.api
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public final class com/posthog/android/PostHogAndroid$Companion {
}

public class com/posthog/android/PostHogAndroidConfig : com/posthog/PostHogConfig {
public fun <init> (Ljava/lang/String;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;Z)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;ZZ)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZ)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZLcom/posthog/android/replay/PostHogSessionReplayConfig;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZLcom/posthog/android/replay/PostHogSessionReplayConfig;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getCaptureApplicationLifecycleEvents ()Z
Expand Down Expand Up @@ -48,6 +53,11 @@ public final class com/posthog/android/replay/PostHogReplayIntegration : com/pos

public final class com/posthog/android/replay/PostHogSessionReplayConfig {
public fun <init> ()V
public fun <init> (Z)V
public fun <init> (ZZ)V
public fun <init> (ZZZ)V
public fun <init> (ZZZLcom/posthog/android/replay/PostHogDrawableConverter;)V
public fun <init> (ZZZLcom/posthog/android/replay/PostHogDrawableConverter;Z)V
public fun <init> (ZZZLcom/posthog/android/replay/PostHogDrawableConverter;ZJ)V
public synthetic fun <init> (ZZZLcom/posthog/android/replay/PostHogDrawableConverter;ZJILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getCaptureLogcat ()Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import com.posthog.android.replay.PostHogSessionReplayConfig
* @property captureDeepLinks captures deep links events
* @property captureScreenViews captures screen views events
*/
public open class PostHogAndroidConfig(
apiKey: String,
host: String = DEFAULT_HOST,
public var captureApplicationLifecycleEvents: Boolean = true,
public var captureDeepLinks: Boolean = true,
public var captureScreenViews: Boolean = true,
@PostHogExperimental
public var sessionReplayConfig: PostHogSessionReplayConfig = PostHogSessionReplayConfig(),
) : PostHogConfig(apiKey, host)
public open class PostHogAndroidConfig
@JvmOverloads
constructor(
apiKey: String,
host: String = DEFAULT_HOST,
public var captureApplicationLifecycleEvents: Boolean = true,
public var captureDeepLinks: Boolean = true,
public var captureScreenViews: Boolean = true,
@PostHogExperimental
public var sessionReplayConfig: PostHogSessionReplayConfig = PostHogSessionReplayConfig(),
) : PostHogConfig(apiKey, host)
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,47 @@ package com.posthog.android.replay
import com.posthog.PostHogExperimental

@PostHogExperimental
public class PostHogSessionReplayConfig(
/**
* Enable masking of all text input fields
* Defaults to true
*/
@PostHogExperimental
public var maskAllTextInputs: Boolean = true,
/**
* Enable masking of all images to a placeholder
* Defaults to true
*/
@PostHogExperimental
public var maskAllImages: Boolean = true,
/**
* Enable capturing of logcat as console events
* Defaults to true
*/
@PostHogExperimental
public var captureLogcat: Boolean = true,
/**
* Converts custom Drawable to Bitmap
* By default PostHog tries to convert the Drawable to Bitmap, the supported types are
* BitmapDrawable, ColorDrawable, GradientDrawable, InsetDrawable, LayerDrawable, RippleDrawable
*/
@PostHogExperimental
public var drawableConverter: PostHogDrawableConverter? = null,
/**
* By default Session replay will capture all the views on the screen as a wireframe,
* By enabling this option, PostHog will capture the screenshot of the screen.
* The screenshot may contain sensitive information, use with caution.
*/
@PostHogExperimental
public var screenshot: Boolean = false,
/**
* Deboucer delay used to reduce the number of snapshots captured and reduce performance impact
* This is used for capturing the view as a wireframe or screenshot
* The lower the number more snapshots will be captured but higher the performance impact
* Defaults to 500ms
*/
@PostHogExperimental
public var debouncerDelayMs: Long = 500,
)
public class PostHogSessionReplayConfig
@JvmOverloads
constructor(
/**
* Enable masking of all text input fields
* Defaults to true
*/
@PostHogExperimental
public var maskAllTextInputs: Boolean = true,
/**
* Enable masking of all images to a placeholder
* Defaults to true
*/
@PostHogExperimental
public var maskAllImages: Boolean = true,
/**
* Enable capturing of logcat as console events
* Defaults to true
*/
@PostHogExperimental
public var captureLogcat: Boolean = true,
/**
* Converts custom Drawable to Bitmap
* By default PostHog tries to convert the Drawable to Bitmap, the supported types are
* BitmapDrawable, ColorDrawable, GradientDrawable, InsetDrawable, LayerDrawable, RippleDrawable
*/
@PostHogExperimental
public var drawableConverter: PostHogDrawableConverter? = null,
/**
* By default Session replay will capture all the views on the screen as a wireframe,
* By enabling this option, PostHog will capture the screenshot of the screen.
* The screenshot may contain sensitive information, use with caution.
*/
@PostHogExperimental
public var screenshot: Boolean = false,
/**
* Deboucer delay used to reduce the number of snapshots captured and reduce performance impact
* This is used for capturing the view as a wireframe or screenshot
* The lower the number more snapshots will be captured but higher the performance impact
* Defaults to 500ms
*/
@PostHogExperimental
public var debouncerDelayMs: Long = 500,
)
102 changes: 52 additions & 50 deletions posthog/src/main/java/com/posthog/PostHogOkHttpInterceptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,66 @@ import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response

public class PostHogOkHttpInterceptor(private val captureNetworkTelemetry: Boolean = true) : Interceptor {
private val isSessionReplayEnabled: Boolean
get() = PostHog.getConfig<PostHogConfig>()?.sessionReplay == true && PostHog.isSessionActive()
public class PostHogOkHttpInterceptor
@JvmOverloads
constructor(private val captureNetworkTelemetry: Boolean = true) : Interceptor {
private val isSessionReplayEnabled: Boolean
get() = PostHog.getConfig<PostHogConfig>()?.sessionReplay == true && PostHog.isSessionActive()

override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()

try {
val response = chain.proceed(originalRequest)
try {
val response = chain.proceed(originalRequest)

captureNetworkEvent(originalRequest, response)
captureNetworkEvent(originalRequest, response)

return response
} catch (e: Throwable) {
throw e
return response
} catch (e: Throwable) {
throw e
}
}
}

private fun captureNetworkEvent(
request: Request,
response: Response,
) {
// do not capture network logs if session replay is disabled
if (!captureNetworkTelemetry || !isSessionReplayEnabled) {
return
}
val url = request.url.toString()
val method = request.method
val statusCode = response.code
val start = response.sentRequestAtMillis
val end = response.receivedResponseAtMillis
val transferSize = (response.body?.contentLength() ?: 0) + (request.body?.contentLength() ?: 0)
private fun captureNetworkEvent(
request: Request,
response: Response,
) {
// do not capture network logs if session replay is disabled
if (!captureNetworkTelemetry || !isSessionReplayEnabled) {
return
}
val url = request.url.toString()
val method = request.method
val statusCode = response.code
val start = response.sentRequestAtMillis
val end = response.receivedResponseAtMillis
val transferSize = (response.body?.contentLength() ?: 0) + (request.body?.contentLength() ?: 0)

val requestMap = mutableMapOf<String, Any>()
val requestMap = mutableMapOf<String, Any>()

var cache = false
response.cacheResponse?.let {
cache = true
}
if (transferSize >= 0) {
// the UI special case if the transferSize is 0 as coming from cache
requestMap["transferSize"] = if (!cache) transferSize else 0
}
var cache = false
response.cacheResponse?.let {
cache = true
}
if (transferSize >= 0) {
// the UI special case if the transferSize is 0 as coming from cache
requestMap["transferSize"] = if (!cache) transferSize else 0
}

requestMap.putAll(
mapOf(
"name" to url,
"method" to method,
"responseStatus" to statusCode,
"timestamp" to end,
"duration" to (end - start),
"initiatorType" to "fetch",
"entryType" to "resource",
),
)
val requests = listOf(requestMap)
val payload = mapOf<String, Any>("requests" to requests)
requestMap.putAll(
mapOf(
"name" to url,
"method" to method,
"responseStatus" to statusCode,
"timestamp" to end,
"duration" to (end - start),
"initiatorType" to "fetch",
"entryType" to "resource",
),
)
val requests = listOf(requestMap)
val payload = mapOf<String, Any>("requests" to requests)

listOf(RRPluginEvent("rrweb/network@1", payload, end)).capture()
listOf(RRPluginEvent("rrweb/network@1", payload, end)).capture()
}
}
}

0 comments on commit f934b9a

Please sign in to comment.