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 18 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
16 changes: 11 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ 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
run: |
flutter pub get
cd example
flutter pub get

- name: SDK format check
run: |
make installKtLint
make checkFormatDart
make analyzeDart
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
14 changes: 10 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,9 @@ formatSwift:

formatDart:
dart format .

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

analyzeDart:
dart analyze .
1 change: 1 addition & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
.cxx

/.settings
.project
34 changes: 0 additions & 34 deletions android/.project

This file was deleted.

6 changes: 3 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
group 'com.posthog.posthog_flutter'
group 'com.posthog.flutter'
version '1.0-SNAPSHOT'

buildscript {
Expand Down Expand Up @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'

android {
if (project.android.hasProperty("namespace")) {
namespace 'com.posthog.posthog_flutter'
namespace 'com.posthog.flutter'
}

compileSdkVersion 33
Expand Down 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
2 changes: 1 addition & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.posthog.posthog_flutter">
package="com.posthog.flutter">
</manifest>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.posthog.posthog_flutter
package com.posthog.flutter

internal val postHogVersion = "4.6.0"
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.posthog.posthog_flutter
package com.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

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
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
63 changes: 63 additions & 0 deletions android/src/main/kotlin/com/posthog/flutter/SnapshotSender.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.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()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.posthog.posthog_flutter
package com.posthog.flutter

import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
Expand Down
6 changes: 2 additions & 4 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if (flutterVersionName == null) {
}

android {
namespace "com.example.posthog_flutter_example"
namespace "com.example.flutter"
compileSdkVersion flutter.compileSdkVersion
ndkVersion flutter.ndkVersion

Expand All @@ -41,8 +41,7 @@ android {
}

defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.posthog_flutter_example"
applicationId "com.example.flutter"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion 21
Expand All @@ -56,7 +55,6 @@ android {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
shrinkResources true
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
Expand Down
8 changes: 5 additions & 3 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:label="posthog_flutter_example"
android:label="PostHog Flutter Example"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
Expand Down 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>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.posthog_flutter_example
package com.example.flutter

import io.flutter.embedding.android.FlutterActivity

Expand Down
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.
Loading