Skip to content

Commit

Permalink
feat: add get session id
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielRStabile committed Dec 17, 2024
1 parent 049f53b commit 5866f41
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ class PosthogFlutterPlugin :
"isSessionReplayActive" -> {
result.success(isSessionReplayActive())
}
"getSessionId" -> {
getSessionId(result)
}
else -> {
result.notImplemented()
}
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 6 additions & 0 deletions ios/Classes/PosthogFlutterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions lib/posthog_flutter_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
12 changes: 12 additions & 0 deletions lib/src/posthog_flutter_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
4 changes: 4 additions & 0 deletions lib/src/posthog_flutter_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 3 additions & 0 deletions lib/src/posthog_flutter_web_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 5866f41

Please sign in to comment.