Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wip): support to session replay #116

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.posthog.posthog_flutter
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import android.util.Log
Expand All @@ -11,11 +13,19 @@ import com.posthog.PostHog
import com.posthog.PostHogConfig
import com.posthog.android.PostHogAndroid
import com.posthog.android.PostHogAndroidConfig
import com.posthog.internal.replay.RRFullSnapshotEvent
import com.posthog.internal.replay.RRIncrementalMutationData
import com.posthog.internal.replay.RRIncrementalSnapshotEvent
import com.posthog.internal.replay.RRMutatedNode
import com.posthog.internal.replay.RRStyle
import com.posthog.internal.replay.RRWireframe
import com.posthog.internal.replay.capture
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import java.io.ByteArrayOutputStream

/** PosthogFlutterPlugin */
class PosthogFlutterPlugin :
Expand All @@ -29,6 +39,9 @@ class PosthogFlutterPlugin :

private lateinit var applicationContext: Context

private val snapshotSender = SnapshotSender()


override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "posthog_flutter")

Expand Down Expand Up @@ -163,6 +176,12 @@ class PosthogFlutterPlugin :
"close" -> {
close(result)
}
"sendFullSnapshot" -> {
handleSendFullSnapshot(call, result)
}
"sendIncrementalSnapshot" -> {
handleSendIncrementalSnapshot(call, result)
}
else -> {
result.notImplemented()
}
Expand Down Expand Up @@ -235,8 +254,19 @@ class PosthogFlutterPlugin :
"identifiedOnly" -> personProfiles = PersonProfiles.IDENTIFIED_ONLY
}
}
posthogConfig.getIfNotNull<Boolean>("enableSessionReplay") {
sessionReplay = it
}

posthogConfig.getIfNotNull<Map<String, Any>>("sessionReplayConfig") { sessionReplayConfig ->
sessionReplayConfig.getIfNotNull<Long>("androidDebouncerDelayMs") {
this.sessionReplayConfig.debouncerDelayMs = it
}
}

sdkName = "posthog-flutter"
sdkVersion = postHogVersion

}
PostHogAndroid.setup(applicationContext, config)
}
Expand All @@ -245,6 +275,28 @@ class PosthogFlutterPlugin :
channel.setMethodCallHandler(null)
}

private fun handleSendFullSnapshot(call: MethodCall, result: Result) {
val imageBytes = call.argument<ByteArray>("imageBytes")
val id = call.argument<Int>("id") ?: 1
if (imageBytes != null) {
snapshotSender.sendFullSnapshot(imageBytes, id)
result.success(null)
} else {
result.error("INVALID_ARGUMENT", "Image bytes are null", null)
}
}

private fun handleSendIncrementalSnapshot(call: MethodCall, result: Result) {
val imageBytes = call.argument<ByteArray>("imageBytes")
val id = call.argument<Int>("id") ?: 1
if (imageBytes != null) {
snapshotSender.sendIncrementalSnapshot(imageBytes, id)
result.success(null)
} else {
result.error("INVALID_ARGUMENT", "Image bytes are null", null)
}
}

private fun getFeatureFlag(
call: MethodCall,
result: Result,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.posthog.posthog_flutter

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Log
import com.posthog.internal.replay.*
import java.io.ByteArrayOutputStream

/*
* TEMPORARY CLASS FOR TESTING PURPOSES
* This function sends a screenshot to PostHog.
* It should be removed or refactored in the other version.
*/
class SnapshotSender {

fun sendFullSnapshot(imageBytes: ByteArray, id: Int = 1) {
val bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
val base64String = bitmapToBase64(bitmap)

val wireframe = RRWireframe(
id = id,
x = 0,
y = 0,
width = bitmap.width,
height = bitmap.height,
type = "screenshot",
base64 = base64String,
style = RRStyle()
)

val snapshotEvent = RRFullSnapshotEvent(
listOf(wireframe),
initialOffsetTop = 0,
initialOffsetLeft = 0,
timestamp = System.currentTimeMillis()
)

Log.d("Snapshot", "Sending Full Snapshot")
listOf(snapshotEvent).capture()
}

fun sendIncrementalSnapshot(imageBytes: ByteArray, id: Int = 1) {
val bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
val base64String = bitmapToBase64(bitmap)

val wireframe = RRWireframe(
id = id,
x = 0,
y = 0,
width = bitmap.width,
height = bitmap.height,
type = "screenshot",
base64 = base64String,
style = RRStyle()
)

val mutatedNode = RRMutatedNode(wireframe, parentId = null)
val updatedNodes = listOf(mutatedNode)

val incrementalMutationData = RRIncrementalMutationData(
adds = null,
removes = null,
updates = updatedNodes
)

val incrementalSnapshotEvent = RRIncrementalSnapshotEvent(
mutationData = incrementalMutationData,
timestamp = System.currentTimeMillis()
)

Log.d("Snapshot", "Sending Incremental Snapshot")
listOf(incrementalSnapshotEvent).capture()
}

private fun bitmapToBase64(bitmap: Bitmap): String? {
ByteArrayOutputStream().use { byteArrayOutputStream ->
bitmap.compress(
Bitmap.CompressFormat.JPEG,
30,
byteArrayOutputStream
)
val byteArray = byteArrayOutputStream.toByteArray()
return android.util.Base64.encodeToString(byteArray, android.util.Base64.NO_WRAP)
}
}
}
4 changes: 1 addition & 3 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
special context set for you by the time it is initialized. -->
<meta-data
android:name="com.posthog.posthog.API_KEY"
android:value="phc_QFbR1y41s5sxnNTZoyKG2NJo2RlsCIWkUfdpawgb40D" />
android:value="_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI" />
<meta-data
android:name="com.posthog.posthog.POSTHOG_HOST"
android:value="https://us.i.posthog.com" />
Expand All @@ -46,7 +46,5 @@
<meta-data
android:name="com.posthog.posthog.DEBUG"
android:value="true" />
<!-- <meta-data android:name="com.posthog.posthog.AUTO_INIT"
android:value="false" /> -->
</application>
</manifest>
Binary file added example/assets/posthog_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/assets/training_posthog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading