-
Notifications
You must be signed in to change notification settings - Fork 110
feat(analytics): event bridge common package #246
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
Closed
Closed
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2b5736f
WIP: event bridge wiring and mixpanel_flutter_common package
tylerjroach 53ef6d1
chore(android): move EventBridge subscriber to Dispatchers.IO
tylerjroach 4a0c020
feat(event-bridge): start native subscription lazily on first listener
tylerjroach dabae99
updates
tylerjroach 05ed1b9
chore(mixpanel_flutter): revert dart format churn in mixpanel_flutter…
tylerjroach 7306d59
test(jsonlogic): fail loudly on malformed fixture entries
tylerjroach 4193db7
fix(android): bump Kotlin to 2.1.0 to consume mixpanel-android-common
tylerjroach af6d8c4
fix(android): use Kotlin 2.0.0 instead of 2.1.0
tylerjroach 6d20500
fix(android): bump example app's Kotlin to 2.0.0
tylerjroach b869ae5
fix(android): skip Kotlin metadata version check for plugin compile
tylerjroach da196be
revert(android): undo unhelpful Kotlin version bumps
tylerjroach 5c2ac31
Merge branch 'main' into feat/event-bridge-common-package
tylerjroach 581a14d
optimization
tylerjroach 286723c
lazy event bridge
tylerjroach dd59d1f
pr updates
tylerjroach 5dedbbf
docs: cite mixpanel-swift-common as the precedent for === int precision
tylerjroach 0e4f50c
test: cover MissingPluginException, lifecycle error swallowing, and i…
tylerjroach 36033fe
event bridge testing
tylerjroach 06efb5b
Merge branch 'main' into feat/event-bridge-common-package
tylerjroach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...el_flutter/android/src/main/kotlin/com/mixpanel/mixpanel_flutter/EventBridgeSubscriber.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package com.mixpanel.mixpanel_flutter | ||
|
|
||
| import android.util.Log | ||
| import com.mixpanel.android.eventbridge.MixpanelEventBridge | ||
| import io.flutter.plugin.common.MethodChannel | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.SupervisorJob | ||
| import kotlinx.coroutines.flow.collect | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.plus | ||
| import org.json.JSONException | ||
| import org.json.JSONObject | ||
|
|
||
| /** | ||
| * Subscribes to the native Mixpanel SDK's [MixpanelEventBridge] (a Kotlin | ||
| * `SharedFlow`) and forwards each event to the Dart side via the existing | ||
| * Flutter MethodChannel. | ||
| * | ||
| * The Java plugin calls [start] from `onAttachedToEngine` and [stop] from | ||
| * `onDetachedFromEngine`. This object is a singleton because the native | ||
| * SharedFlow itself is a singleton — we never want more than one active | ||
| * subscription per process. | ||
| */ | ||
| object EventBridgeSubscriber { | ||
|
|
||
| private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main) | ||
| private var job: Job? = null | ||
|
|
||
| @JvmStatic | ||
| fun start(channel: MethodChannel) { | ||
| if (job != null) return | ||
| job = scope.launch { | ||
| MixpanelEventBridge.events().collect { event -> | ||
| val properties = event.properties?.let { safelyConvert(it) } | ||
| channel.invokeMethod( | ||
| "onMixpanelEvent", | ||
| mapOf( | ||
| "eventName" to event.eventName, | ||
| "properties" to properties, | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun stop() { | ||
| job?.cancel() | ||
| job = null | ||
| } | ||
|
|
||
| private fun safelyConvert(json: JSONObject): Map<String, Any?>? = try { | ||
| MixpanelFlutterHelper.toMap(json) | ||
| } catch (e: JSONException) { | ||
| // A malformed properties payload should not abort the whole | ||
| // subscription — drop this event's properties and keep collecting. | ||
| Log.w("EventBridgeSubscriber", "Failed to convert event properties", e) | ||
| null | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,18 +4,29 @@ import Flutter | |
| import FlutterMacOS | ||
| #endif | ||
| import Mixpanel | ||
| import MixpanelSwiftCommon | ||
|
|
||
| #if os(macOS) | ||
| public typealias MixpanelFlutterPlugin = SwiftMixpanelFlutterPlugin | ||
| #endif | ||
|
|
||
| public class SwiftMixpanelFlutterPlugin: NSObject, FlutterPlugin { | ||
|
|
||
| private var instance: MixpanelInstance? | ||
| var token: String? | ||
| var mixpanelProperties: [String: String]? | ||
| let defaultFlushInterval = 60.0 | ||
|
|
||
|
|
||
| // Held so `startEventBridge` (invoked lazily from Dart) can fan native | ||
| // events back through the same channel that delivers regular method | ||
| // calls. Released on `deinit` along with the task. | ||
| private var channel: FlutterMethodChannel? | ||
|
|
||
| // The long-lived AsyncStream consumer that forwards native | ||
| // MixpanelEventBridge events to Dart. Created on first | ||
| // `startEventBridge` and cancelled by `stopEventBridge` / `deinit`. | ||
| private var eventBridgeTask: Task<Void, Never>? | ||
|
|
||
| public static func register(with registrar: FlutterPluginRegistrar) { | ||
| let readWriter = MixpanelReaderWriter() | ||
| let codec = FlutterStandardMethodCodec(readerWriter: readWriter) | ||
|
|
@@ -25,7 +36,43 @@ public class SwiftMixpanelFlutterPlugin: NSObject, FlutterPlugin { | |
| let channel = FlutterMethodChannel(name: "mixpanel_flutter", binaryMessenger: registrar.messenger, codec: codec) | ||
| #endif | ||
| let instance = SwiftMixpanelFlutterPlugin() | ||
| instance.channel = channel | ||
| registrar.addMethodCallDelegate(instance, channel: channel) | ||
|
|
||
| // The native EventBridge subscription is started lazily from Dart | ||
| // via `startEventBridge` when the first listener attaches to | ||
| // `MixpanelEventBridge.events`. Apps that never consume events | ||
| // never pay the cost of the AsyncStream consumer task. | ||
| } | ||
|
|
||
| deinit { | ||
| eventBridgeTask?.cancel() | ||
| } | ||
|
|
||
| private func handleStartEventBridge(_ result: @escaping FlutterResult) { | ||
| guard eventBridgeTask == nil, let channel = channel else { | ||
| result(nil) | ||
| return | ||
| } | ||
| if #available(iOS 13.0, macOS 10.15, *) { | ||
| eventBridgeTask = Task { | ||
| for await event in MixpanelEventBridge.shared.eventStream() { | ||
| await MainActor.run { | ||
| channel.invokeMethod("onMixpanelEvent", arguments: [ | ||
| "eventName": event.eventName, | ||
| "properties": event.properties, | ||
| ]) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| result(nil) | ||
| } | ||
|
Comment on lines
+62
to
+80
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need, we should be bumping to 13 minimum soon on iOS anyway. This is just a sanity check. |
||
|
|
||
| private func handleStopEventBridge(_ result: @escaping FlutterResult) { | ||
| eventBridgeTask?.cancel() | ||
| eventBridgeTask = nil | ||
| result(nil) | ||
| } | ||
|
|
||
| public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { | ||
|
|
@@ -174,6 +221,12 @@ public class SwiftMixpanelFlutterPlugin: NSObject, FlutterPlugin { | |
| case "getAllVariants": | ||
| handleGetAllVariants(call, result: result) | ||
| break | ||
| case "startEventBridge": | ||
| handleStartEventBridge(result) | ||
| break | ||
| case "stopEventBridge": | ||
| handleStopEventBridge(result) | ||
| break | ||
| default: | ||
| result(FlutterMethodNotImplemented) | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't warn.