Skip to content

Commit

Permalink
fix code snippets for android
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Jan 4, 2024
1 parent 05c4fc6 commit 2f2173e
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 406 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ jobs:
- uses: subosito/flutter-action@v2
with:
channel: 'stable'

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

- name: SDK analyze check
working-directory: ./lib
run: flutter analyze

- name: Install dependencies
working-directory: ./example
run: flutter pub get
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@

## 1.9.0

- Posthog client library for Flutter is released!
- PostHog client library for Flutter is released!
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ To use this plugin, add `posthog_flutter` as a [dependency in your pubspec.yaml
| `capture` | X | X | X |
| `screen` | X | X | X |
| `alias` | X | X | X |
| `getAnonymousId` | X | X | X |
| `getDistinctId` | X | X | X |
| `reset` | X | X | X |
| `disable` | X | X | |
| `enable` | X | X | |
| `debug` | X\* | X | X |
| `debug` | X | X | X |
| `setContext` | X | X | |
| `isFeatureEnabled` | X | X | X |
| `getFeatureFlag` | X | X | X |
| `getFeatureFlagPayload` | X | X | X |
| `getFeatureFlagAndPayload`| X | X | X |

\* Debugging must be set as a configuration parameter in `AndroidManifest.xml` (see below). The official posthog library does not offer the debug method for Android.

### Example

Expand Down Expand Up @@ -116,7 +113,7 @@ Remember that the application lifecycle events won't have any special context se
</plist>
```

For `debug` mode on iOS, you can use the following snippet:
For `debug` mode on Android and iOS, you can use the following snippet:

```dart
PostHog().debug(true);
Expand Down
3 changes: 1 addition & 2 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
org.gradle.jvmargs=-Xmx1536M

org.gradle.jvmargs=-Xmx4G
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.pm.PackageManager
import android.util.Log
import com.posthog.PostHog
import com.posthog.PostHogConfig
import com.posthog.android.PostHogAndroid
import com.posthog.android.PostHogAndroidConfig
import io.flutter.embedding.engine.plugins.FlutterPlugin
Expand All @@ -30,15 +31,22 @@ class PosthogFlutterPlugin : FlutterPlugin, MethodCallHandler {

private fun initPlugin(applicationContext: Context) {
try {
// TODO: replace deprecated method API 33
val ai = applicationContext.packageManager.getApplicationInfo(applicationContext.packageName, PackageManager.GET_META_DATA)
val bundle = ai.metaData
val apiKey = bundle.getString("com.posthog.posthog.API_KEY", "")
val posthogHost = bundle.getString("com.posthog.posthog.POSTHOG_HOST", "")
val apiKey = bundle.getString("com.posthog.posthog.API_KEY", null)

if (apiKey.isNullOrEmpty()) {
Log.e("PostHog", "com.posthog.posthog.API_KEY is missing!")
return
}

val host = bundle.getString("com.posthog.posthog.POSTHOG_HOST", PostHogConfig.defaultHost)
val trackApplicationLifecycleEvents = bundle.getBoolean("com.posthog.posthog.TRACK_APPLICATION_LIFECYCLE_EVENTS", false)
val enableDebug = bundle.getBoolean("com.posthog.posthog.DEBUG", false)

// Init PostHog
val config = PostHogAndroidConfig(apiKey, posthogHost).apply {
val config = PostHogAndroidConfig(apiKey, host).apply {
captureScreenViews = false
captureDeepLinks = false
captureApplicationLifecycleEvents = trackApplicationLifecycleEvents
Expand All @@ -47,16 +55,13 @@ class PosthogFlutterPlugin : FlutterPlugin, MethodCallHandler {
PostHogAndroid.setup(applicationContext, config)

} catch (e: Throwable) {
e.localizedMessage?.let { Log.e("initPlugin", it) }
e.localizedMessage?.let { Log.e("PostHog", "initPlugin error: $it") }
}
}

override fun onMethodCall(call: MethodCall, result: Result) {

when (call.method) {
"getPlatformVersion" -> {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
}

"identify" -> {
identify(call, result)
Expand Down Expand Up @@ -110,13 +115,12 @@ class PosthogFlutterPlugin : FlutterPlugin, MethodCallHandler {
getFeatureFlagPayload(call, result)
}

"getFeatureFlagAndPayload" -> {
getFeatureFlagAndPayload(call, result)
}

"register" -> {
register(call, result)
}
"debug" -> {
debug(call, result)
}

else -> {
result.notImplemented()
Expand All @@ -131,103 +135,72 @@ class PosthogFlutterPlugin : FlutterPlugin, MethodCallHandler {

private fun getFeatureFlag(call: MethodCall, result: Result) {
try {
val featureFlagKey: String? = call.argument("key")
val value: Any? = PostHog.getFeatureFlag(featureFlagKey!!)
result.success(value)
val featureFlagKey: String = call.argument("key")!!
val flag = PostHog.getFeatureFlag(featureFlagKey)
result.success(flag)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

private fun getFeatureFlagPayload(call: MethodCall, result: Result) {
try {
val featureFlagKey: String? = call.argument("key")
val value: Any? = PostHog.getFeatureFlagPayload(featureFlagKey!!)
result.success(value)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

private fun getFeatureFlagAndPayload(call: MethodCall, result: Result) {
try {
val featureFlagKey: String? = call.argument("key")
val status: Any? = PostHog.getFeatureFlag(featureFlagKey!!)
val payload: Any? = PostHog.getFeatureFlagPayload(featureFlagKey)
val featureAndPayload = mutableMapOf<String, Any?>()

when (status) {
null -> {
featureAndPayload["isEnabled"] = false
}

is String -> {
featureAndPayload["isEnabled"] = true
featureAndPayload["variant"] = status
}

else -> {
featureAndPayload["isEnabled"] = status
}
}
featureAndPayload["data"] = payload

result.success(featureAndPayload)
val featureFlagKey: String = call.argument("key")!!
val flag = PostHog.getFeatureFlagPayload(featureFlagKey)
result.success(flag)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

private fun identify(call: MethodCall, result: Result) {
try {
val userId = call.argument("userId") as? String
val propertiesData: HashMap<String, Any>? = call.argument("properties")
val optionsData: HashMap<String, Any>? = call.argument("options")
PostHog.identify(userId!!, propertiesData, optionsData)
result.success(true)
val userId: String = call.argument("userId")!!
val userProperties: Map<String, Any>? = call.argument("userProperties")
val userPropertiesSetOnce: Map<String, Any>? = call.argument("userPropertiesSetOnce")
PostHog.identify(userId, userProperties, userPropertiesSetOnce)
result.success(null)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

private fun capture(call: MethodCall, result: Result) {
try {
val eventName: String? = call.argument("eventName")
val distinctId: String? = call.argument("distinctId")
val propertiesData: HashMap<String, Any>? = call.argument("properties")
val optionsData: HashMap<String, Any>? = call.argument("options")
PostHog.capture(eventName!!, distinctId, propertiesData, optionsData)
result.success(true)
val eventName: String = call.argument("eventName")!!
val properties: Map<String, Any>? = call.argument("properties")
PostHog.capture(eventName, properties = properties)
result.success(null)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

private fun screen(call: MethodCall, result: Result) {
try {
val screenName: String? = call.argument("screenName")
val propertiesData: HashMap<String, Any>? = call.argument("properties")
PostHog.screen(screenName!!, propertiesData)
result.success(true)
val screenName: String = call.argument("screenName")!!
val properties: Map<String, Any>? = call.argument("properties")
PostHog.screen(screenName, properties)
result.success(null)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

private fun alias(call: MethodCall, result: Result) {
try {
val alias: String? = call.argument("alias")
PostHog.alias(alias!!)
result.success(true)
val alias: String = call.argument("alias")!!
PostHog.alias(alias)
result.success(null)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

private fun distinctId(result: Result) {
try {
val anonymousId: String = PostHog.distinctId()
result.success(anonymousId)
val distinctId: String = PostHog.distinctId()
result.success(distinctId)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
Expand All @@ -236,38 +209,44 @@ class PosthogFlutterPlugin : FlutterPlugin, MethodCallHandler {
private fun reset(result: Result) {
try {
PostHog.reset()
result.success(true)
result.success(null)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

// There is no enable method at this time for PostHog on Android.
// Instead, we use optOut as a proxy to achieve the same result.
private fun enable(result: Result) {
try {
PostHog.optIn()
result.success(true)
result.success(null)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

private fun debug(call: MethodCall, result: Result) {
try {
val debug: Boolean = call.argument("debug")!!
PostHog.debug(debug)
result.success(null)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

// There is no disable method at this time for PostHog on Android.
// Instead, we use optOut as a proxy to achieve the same result.
private fun disable(result: Result) {
try {
PostHog.optOut()
result.success(true)
result.success(null)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

private fun isFeatureEnabled(call: MethodCall, result: Result) {
try {
val key: String? = call.argument("key")
val isEnabled: Boolean = PostHog.isFeatureEnabled(key!!)
val key: String = call.argument("key")!!
val isEnabled = PostHog.isFeatureEnabled(key)
result.success(isEnabled)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
Expand All @@ -277,30 +256,30 @@ class PosthogFlutterPlugin : FlutterPlugin, MethodCallHandler {
private fun reloadFeatureFlags(result: Result) {
try {
PostHog.reloadFeatureFlags()
result.success(true)
result.success(null)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

private fun group(call: MethodCall, result: Result) {
try {
val groupType: String? = call.argument("groupType")
val groupKey: String? = call.argument("groupKey")
val propertiesData: HashMap<String, Any>? = call.argument("groupProperties")
PostHog.group(groupType!!, groupKey!!, propertiesData)
result.success(true)
val groupType: String = call.argument("groupType")!!
val groupKey: String = call.argument("groupKey")!!
val groupProperties: Map<String, Any>? = call.argument("groupProperties")
PostHog.group(groupType, groupKey, groupProperties)
result.success(null)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

private fun register(call: MethodCall, result: Result) {
try {
val key: String? = call.argument("key")
val value: Any? = call.argument("value")
PostHog.register(key!!, value!!)
result.success(true)
val key: String = call.argument("key")!!
val value: Any = call.argument("value")!!
PostHog.register(key, value)
result.success(null)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ import org.mockito.Mockito
*/

internal class PosthogFlutterPluginTest {
@Test
fun onMethodCall_getPlatformVersion_returnsExpectedValue() {
val plugin = PosthogFlutterPlugin()

val call = MethodCall("getPlatformVersion", null)
val mockResult: MethodChannel.Result = Mockito.mock(MethodChannel.Result::class.java)
plugin.onMethodCall(call, mockResult)

Mockito.verify(mockResult).success("Android " + android.os.Build.VERSION.RELEASE)
}

@Test
fun onMethodCall_identify_returnsExpectedValue() {
Expand Down
24 changes: 0 additions & 24 deletions example/integration_test/plugin_integration_test.dart

This file was deleted.

Loading

0 comments on commit 2f2173e

Please sign in to comment.