From 392184cec475aa23c1e5efd7eafffd0ec94026a2 Mon Sep 17 00:00:00 2001 From: Gabriel Stabile <68667679+GabrielRStabile@users.noreply.github.com> Date: Wed, 18 Dec 2024 11:37:01 -0300 Subject: [PATCH] feat: add get session id (#134) --- CHANGELOG.md | 1 + .../com/posthog/flutter/PosthogFlutterPlugin.kt | 12 ++++++++++++ example/web/index.html | 2 +- ios/Classes/PosthogFlutterPlugin.swift | 6 ++++++ lib/posthog_flutter_web.dart | 1 + lib/src/posthog.dart | 2 ++ lib/src/posthog_flutter_io.dart | 11 +++++++++++ lib/src/posthog_flutter_platform_interface.dart | 4 ++++ lib/src/posthog_flutter_web_handler.dart | 12 ++++++++++-- 9 files changed, 48 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c12edbd..7c0b765 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ ## Next + - feat: add getter for current session identifier ([#134](https://github.com/PostHog/posthog-flutter/pull/134)) ## 4.8.0 diff --git a/android/src/main/kotlin/com/posthog/flutter/PosthogFlutterPlugin.kt b/android/src/main/kotlin/com/posthog/flutter/PosthogFlutterPlugin.kt index caf8831..0e4c378 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: Throwable) { + result.error("PosthogFlutterException", e.localizedMessage, null) + } + } + // Call the `completion` closure if cast to map value with `key` and type `T` is successful. @Suppress("UNCHECKED_CAST") private fun Map.getIfNotNull( diff --git a/example/web/index.html b/example/web/index.html index fb68461..958badb 100644 --- a/example/web/index.html +++ b/example/web/index.html @@ -41,7 +41,7 @@ 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.dart b/lib/src/posthog.dart index 8829fec..6b23e03 100644 --- a/lib/src/posthog.dart +++ b/lib/src/posthog.dart @@ -125,5 +125,7 @@ class Posthog { return _posthog.close(); } + Future getSessionId() => _posthog.getSessionId(); + Posthog._internal(); } diff --git a/lib/src/posthog_flutter_io.dart b/lib/src/posthog_flutter_io.dart index 8535a5a..d8c8c05 100644 --- a/lib/src/posthog_flutter_io.dart +++ b/lib/src/posthog_flutter_io.dart @@ -229,4 +229,15 @@ class PosthogFlutterIO extends PosthogFlutterPlatformInterface { printIfDebug('Exeption on close: $exception'); } } + + @override + Future getSessionId() async { + try { + final 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 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..681c3b2 100644 --- a/lib/src/posthog_flutter_web_handler.dart +++ b/lib/src/posthog_flutter_web_handler.dart @@ -43,6 +43,7 @@ Future handleWebMethodCall(MethodCall call, JsObject context) async { break; case 'distinctId': final distinctId = analytics.callMethod('get_distinct_id'); + return distinctId; case 'reset': analytics.callMethod('reset'); @@ -54,8 +55,9 @@ Future handleWebMethodCall(MethodCall call, JsObject context) async { break; case 'isFeatureEnabled': final isFeatureEnabled = analytics.callMethod('isFeatureEnabled', [ - call.arguments['key'], - ]); + call.arguments['key'], + ]) as bool? ?? + false; return isFeatureEnabled; case 'group': analytics.callMethod('group', [ @@ -94,6 +96,12 @@ Future handleWebMethodCall(MethodCall call, JsObject context) async { call.arguments['key'], ]); break; + case 'getSessionId': + final sessionId = analytics.callMethod('get_session_id') as String?; + + if (sessionId?.isEmpty == true) return null; + + return sessionId; case 'flush': // not supported on Web // analytics.callMethod('flush');