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

chore: support to session replay #123

Merged
merged 34 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ jobs:
with:
channel: 'stable'

- name: SDK format check
run: dart format --set-exit-if-changed ./

- name: Install dependencies
working-directory: ./example
run: flutter pub get

- name: SDK format check
run: |
make installKtLint
make checkFormatDart
make formatKotlin

- name: Test
run: flutter test

Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- chore: flutter session replay (Android and iOS) ([#123](https://github.com/PostHog/posthog-flutter/pull/123))

## 4.6.0

- chore: change host to new address ([#106](https://github.com/PostHog/posthog-flutter/pull/106))
Expand Down
15 changes: 11 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
.PHONY: formatKotlin formatSwift formatDart
.PHONY: formatKotlin formatSwift formatDart checkDart installKtLint

installKtLint:
brew install ktlint

# brew install ktlint
# TODO: add ktlint steps in CI
formatKotlin:
ktlint --format
ktlint --format --baseline=ktlint-baseline.xml

# brew install swiftlint
# TODO: add swiftlint steps in CI
Expand All @@ -13,4 +14,10 @@ formatSwift:

formatDart:
dart format .

checkFormatDart:
dart format --set-exit-if-changed ./

# TODO: add analyze steps in CI
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
analyzeDart:
dart analyze .
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ android {
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'org.mockito:mockito-core:5.0.0'
// + Version 3.+ and the versions up to 4.0, not including 4.0 and higher
implementation 'com.posthog:posthog-android:3.+'
implementation 'com.posthog:posthog-android:3.9.3'
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
}

testOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.posthog.posthog_flutter

import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import com.posthog.PersonProfiles
import com.posthog.PostHog
import com.posthog.PostHogConfig
import com.posthog.android.PostHogAndroid
import com.posthog.android.PostHogAndroidConfig
import com.posthog.android.internal.getApplicationInfo
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
Expand All @@ -29,6 +27,8 @@ class PosthogFlutterPlugin :

private lateinit var applicationContext: Context

private val snapshotSender = SnapshotSender()

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

Expand All @@ -38,22 +38,6 @@ class PosthogFlutterPlugin :
channel.setMethodCallHandler(this)
}

// TODO: expose on the android SDK instead
@Throws(PackageManager.NameNotFoundException::class)
private fun getApplicationInfo(context: Context): ApplicationInfo =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context
.packageManager
.getApplicationInfo(
context.packageName,
PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong()),
)
} else {
context
.packageManager
.getApplicationInfo(context.packageName, PackageManager.GET_META_DATA)
}

private fun initPlugin() {
try {
val ai = getApplicationInfo(applicationContext)
Expand Down Expand Up @@ -163,12 +147,44 @@ class PosthogFlutterPlugin :
"close" -> {
close(result)
}
"sendMetaEvent" -> {
handleMetaEvent(call, result)
}
"sendFullSnapshot" -> {
handleSendFullSnapshot(call, result)
}
"isSessionReplayActive" -> {
result.success(isSessionReplayActive())
}
else -> {
result.notImplemented()
}
}
}

private fun isSessionReplayActive(): Boolean = PostHog.isSessionReplayActive()

private fun handleMetaEvent(
call: MethodCall,
result: Result,
) {
try {
val width = call.argument<Int>("width") ?: 0
val height = call.argument<Int>("height") ?: 0
val screen = call.argument<String>("screen") ?: ""

if (width == 0 || height == 0) {
result.error("INVALID_ARGUMENT", "Width or height is 0", null)
return
}

snapshotSender.sendMetaEvent(width, height, screen)
result.success(null)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

private fun setup(
call: MethodCall,
result: Result,
Expand Down Expand Up @@ -235,6 +251,12 @@ class PosthogFlutterPlugin :
"identifiedOnly" -> personProfiles = PersonProfiles.IDENTIFIED_ONLY
}
}
posthogConfig.getIfNotNull<Boolean>("sessionReplay") {
sessionReplay = it
}

this.sessionReplayConfig.captureLogcat = false
Copy link
Member Author

Choose a reason for hiding this comment

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

flutter has its own logging system, so no need to watch logcat


sdkName = "posthog-flutter"
sdkVersion = postHogVersion
}
Expand All @@ -245,6 +267,26 @@ class PosthogFlutterPlugin :
channel.setMethodCallHandler(null)
}

private fun handleSendFullSnapshot(
call: MethodCall,
result: Result,
) {
try {
val imageBytes = call.argument<ByteArray>("imageBytes")
val id = call.argument<Int>("id") ?: 1
val x = call.argument<Int>("x") ?: 0
val y = call.argument<Int>("y") ?: 0
Comment on lines +276 to +278
Copy link
Member Author

Choose a reason for hiding this comment

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

its ok to have a fallback here but it should never be the case anyway, x and y are always 0, and the id is ok to be 1

Copy link
Member Author

Choose a reason for hiding this comment

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

what is really important is imageBytes

Choose a reason for hiding this comment

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

yes make sense

if (imageBytes != null) {
snapshotSender.sendFullSnapshot(imageBytes, id, x, y)
result.success(null)
} else {
result.error("INVALID_ARGUMENT", "Image bytes are null", null)
}
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

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

import android.graphics.BitmapFactory
import com.posthog.android.internal.base64
import com.posthog.internal.replay.RREvent
import com.posthog.internal.replay.RRFullSnapshotEvent
import com.posthog.internal.replay.RRMetaEvent
import com.posthog.internal.replay.RRStyle
import com.posthog.internal.replay.RRWireframe
import com.posthog.internal.replay.capture

class SnapshotSender {
fun sendFullSnapshot(
imageBytes: ByteArray,
id: Int,
x: Int,
y: Int,
) {
val bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
val base64String = bitmap.base64()

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

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

listOf(snapshotEvent).capture()
}

fun sendMetaEvent(
width: Int,
height: Int,
screen: String,
) {
val metaEvent =
RRMetaEvent(
href = screen,
width = width,
height = height,
timestamp = System.currentTimeMillis(),
)

val events = mutableListOf<RREvent>()
events.add(metaEvent)

events.capture()
}
}
7 changes: 7 additions & 0 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ if (flutterVersionName == null) {
flutterVersionName = '1.0'
}

allprojects {
repositories {
google()
mavenCentral()
}
}

android {
namespace "com.example.posthog_flutter_example"
compileSdkVersion flutter.compileSdkVersion
Expand Down
6 changes: 4 additions & 2 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
<meta-data
android:name="com.posthog.posthog.DEBUG"
android:value="true" />
<!-- <meta-data android:name="com.posthog.posthog.AUTO_INIT"
android:value="false" /> -->

<meta-data
android:name="com.posthog.posthog.AUTO_INIT"
android:value="false" />
</application>
</manifest>
1 change: 1 addition & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
org.gradle.jvmargs=-Xmx4G
android.useAndroidX=true
android.suppressUnsupportedCompileSdk=34
Copy link
Member Author

@marandaneto marandaneto Nov 27, 2024

Choose a reason for hiding this comment

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

needed for latest versions and we want to keep back compatibility so not bumping the sample

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.
2 changes: 1 addition & 1 deletion example/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit
import Flutter

@UIApplicationMain
@main
Copy link
Member Author

Choose a reason for hiding this comment

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

this is new but its the sample so its ok

@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
Expand Down
Loading