From 5866f413f7e81775720a13d17e39d2d6bc0019e4 Mon Sep 17 00:00:00 2001 From: Gabriel Rocha Stabile <gabrielrostabile@outlook.com> Date: Tue, 17 Dec 2024 13:23:37 +0000 Subject: [PATCH] feat: add get session id --- .../com/posthog/flutter/PosthogFlutterPlugin.kt | 12 ++++++++++++ ios/Classes/PosthogFlutterPlugin.swift | 6 ++++++ lib/posthog_flutter_web.dart | 1 + lib/src/posthog_flutter_io.dart | 12 ++++++++++++ lib/src/posthog_flutter_platform_interface.dart | 4 ++++ lib/src/posthog_flutter_web_handler.dart | 3 +++ 6 files changed, 38 insertions(+) diff --git a/android/src/main/kotlin/com/posthog/flutter/PosthogFlutterPlugin.kt b/android/src/main/kotlin/com/posthog/flutter/PosthogFlutterPlugin.kt index caf8831..8c6ff6b 100644 --- a/android/src/main/kotlin/com/posthog/flutter/PosthogFlutterPlugin.kt +++ b/android/src/main/kotlin/com/posthog/flutter/PosthogFlutterPlugin.kt @@ -156,6 +156,9 @@ class PosthogFlutterPlugin : "isSessionReplayActive" -> { result.success(isSessionReplayActive()) } + "getSessionId" -> { + getSessionId(result) + } else -> { result.notImplemented() } @@ -500,6 +503,15 @@ class PosthogFlutterPlugin : } } + private fun getSessionId(result: Result) { + try { + val sessionId = PostHog.getSessionId() + result.success(sessionId.toString()) + } catch (e: Exception) { + result.error("ERROR", "Error getting session id", e.message) + } + } + // Call the `completion` closure if cast to map value with `key` and type `T` is successful. @Suppress("UNCHECKED_CAST") private fun <T> Map<String, Any>.getIfNotNull( diff --git a/ios/Classes/PosthogFlutterPlugin.swift b/ios/Classes/PosthogFlutterPlugin.swift index 5af59a2..61eec2f 100644 --- a/ios/Classes/PosthogFlutterPlugin.swift +++ b/ios/Classes/PosthogFlutterPlugin.swift @@ -170,6 +170,8 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin { sendFullSnapshot(call, result: result) case "isSessionReplayActive": isSessionReplayActive(result: result) + case "getSessionId": + getSessionId(result: result) default: result(FlutterMethodNotImplemented) } @@ -499,6 +501,10 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin { result(nil) } + private func getSessionId(result: @escaping FlutterResult) { + result(PostHogSDK.shared.getSessionId()) + } + // Return bad Arguments error private func _badArgumentError(_ result: @escaping FlutterResult) { result(FlutterError( diff --git a/lib/posthog_flutter_web.dart b/lib/posthog_flutter_web.dart index 83cccbe..3893277 100644 --- a/lib/posthog_flutter_web.dart +++ b/lib/posthog_flutter_web.dart @@ -3,6 +3,7 @@ // package as the core of your plugin. // ignore: avoid_web_libraries_in_flutter import 'dart:js'; + import 'package:flutter/services.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; diff --git a/lib/src/posthog_flutter_io.dart b/lib/src/posthog_flutter_io.dart index 8535a5a..00f99ff 100644 --- a/lib/src/posthog_flutter_io.dart +++ b/lib/src/posthog_flutter_io.dart @@ -229,4 +229,16 @@ class PosthogFlutterIO extends PosthogFlutterPlatformInterface { printIfDebug('Exeption on close: $exception'); } } + + @override + Future<String?> getSessionId() async { + try { + final String? sessionId = + await _methodChannel.invokeMethod('getSessionId'); + return sessionId; + } on PlatformException catch (exception) { + printIfDebug('Exception on getSessionId: $exception'); + return null; + } + } } diff --git a/lib/src/posthog_flutter_platform_interface.dart b/lib/src/posthog_flutter_platform_interface.dart index 7678282..ee4525d 100644 --- a/lib/src/posthog_flutter_platform_interface.dart +++ b/lib/src/posthog_flutter_platform_interface.dart @@ -120,5 +120,9 @@ abstract class PosthogFlutterPlatformInterface extends PlatformInterface { throw UnimplementedError('close() has not been implemented.'); } + Future<String?> getSessionId() async { + throw UnimplementedError('getSessionId() not implemented'); + } + // TODO: missing capture with more parameters } diff --git a/lib/src/posthog_flutter_web_handler.dart b/lib/src/posthog_flutter_web_handler.dart index bef4065..f66b818 100644 --- a/lib/src/posthog_flutter_web_handler.dart +++ b/lib/src/posthog_flutter_web_handler.dart @@ -94,6 +94,9 @@ Future<dynamic> handleWebMethodCall(MethodCall call, JsObject context) async { call.arguments['key'], ]); break; + case 'getSessionId': + final sessionId = analytics.callMethod('get_session_id'); + return sessionId; case 'flush': // not supported on Web // analytics.callMethod('flush');