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 0695569 commit 05c4fc6
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 118 deletions.
107 changes: 56 additions & 51 deletions lib/posthog_flutter_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,54 @@ class MethodChannelPosthogFlutter extends PosthogFlutterPlatform {
final methodChannel = const MethodChannel('posthog_flutter');

@override
Future<String?> getPlatformVersion() async {
final version =
await methodChannel.invokeMethod<String>('getPlatformVersion');
return version;
}

Future<void> identify({
required String userId,
Map<String, dynamic>? properties,
Map<String, Object>? userProperties,
Map<String, Object>? userPropertiesSetOnce,
}) async {
try {
await methodChannel.invokeMethod('identify', {
'userId': userId,
'properties': properties ?? {},
if (userProperties != null) 'userProperties': userProperties,
if (userPropertiesSetOnce != null)
'userPropertiesSetOnce': userPropertiesSetOnce,
});
} on PlatformException catch (exception) {
print(exception);
_printIfDebug('Exeption on identify: $exception');
}
}

@override
Future<void> capture({
required String eventName,
Map<String, dynamic>? properties,
Map<String, Object>? properties,
}) async {
try {
await methodChannel.invokeMethod(
'capture', {'eventName': eventName, 'properties': properties ?? {}});
await methodChannel.invokeMethod('capture', {
'eventName': eventName,
if (properties != null) 'properties': properties,
});
} on PlatformException catch (exception) {
print(exception);
_printIfDebug('Exeption on capture: $exception');
}
}

@override
Future<void> screen({
required String screenName,
Map<String, dynamic>? properties,
Map<String, Object>? properties,
}) async {
try {
await methodChannel.invokeMethod('screen', {
'screenName': screenName,
'properties': properties ?? {},
if (properties != null) 'properties': properties,
});
} on PlatformException catch (exception) {
print(exception);
_printIfDebug('Exeption on screen: $exception');
}
}

@override
Future<void> alias({
required String alias,
}) async {
Expand All @@ -64,56 +66,67 @@ class MethodChannelPosthogFlutter extends PosthogFlutterPlatform {
'alias': alias,
});
} on PlatformException catch (exception) {
print(exception);
_printIfDebug('Exeption on alias: $exception');
}
}

Future<String?> get getDistinctId async {
return await methodChannel.invokeMethod('getDistinctId');
@override
Future<String> get getDistinctId async {
try {
return await methodChannel.invokeMethod('getDistinctId');
} on PlatformException catch (exception) {
_printIfDebug('Exeption on reset: $exception');
return "";
}
}

@override
Future<void> reset() async {
try {
await methodChannel.invokeMethod('reset');
} on PlatformException catch (exception) {
print(exception);
_printIfDebug('Exeption on reset: $exception');
}
}

@override
Future<void> disable() async {
try {
await methodChannel.invokeMethod('disable');
} on PlatformException catch (exception) {
print(exception);
_printIfDebug('Exeption on disable: $exception');
}
}

@override
Future<void> enable() async {
try {
await methodChannel.invokeMethod('enable');
} on PlatformException catch (exception) {
print(exception);
_printIfDebug('Exeption on enable: $exception');
}
}

@override
Future<void> debug(bool enabled) async {
try {
await methodChannel.invokeMethod('debug', {
'debug': enabled,
});
} on PlatformException catch (exception) {
print(exception);
_printIfDebug('Exeption on debug: $exception');
}
}

Future<bool?> isFeatureEnabled(String key) async {
@override
Future<bool> isFeatureEnabled(String key) async {
try {
return await methodChannel.invokeMethod('isFeatureEnabled', {
'key': key,
});
} on PlatformException catch (exception) {
print(exception);
return null;
_printIfDebug('Exeption on isFeatureEnabled: $exception');
return false;
}
}

Expand All @@ -122,76 +135,68 @@ class MethodChannelPosthogFlutter extends PosthogFlutterPlatform {
try {
await methodChannel.invokeMethod('reloadFeatureFlags');
} on PlatformException catch (exception) {
print(exception);
_printIfDebug('Exeption on reloadFeatureFlags: $exception');
}
}

@override
Future<void> group({
required String groupType,
required String groupKey,
Map<String, dynamic>? groupProperties,
Map<String, Object>? groupProperties,
}) async {
try {
await methodChannel.invokeMethod('group', {
'groupType': groupType,
'groupKey': groupKey,
'groupProperties': groupProperties ?? {},
if (groupProperties != null) 'groupProperties': groupProperties,
});
} on PlatformException catch (exception) {
print(exception);
_printIfDebug('Exeption on group: $exception');
}
}

@override
Future<dynamic> getFeatureFlag({
Future<Object?> getFeatureFlag({
required String key,
}) async {
try {
return await methodChannel.invokeMethod('getFeatureFlag', {
'key': key,
});
} on PlatformException catch (exception) {
print(exception);
_printIfDebug('Exeption on getFeatureFlag: $exception');
return null;
}
}

@override
Future<Map?> getFeatureFlagPayload({
Future<Object?> getFeatureFlagPayload({
required String key,
}) async {
try {
return await methodChannel.invokeMethod('getFeatureFlagPayload', {
'key': key,
});
} on PlatformException catch (exception) {
print(exception);
return {};
_printIfDebug('Exeption on getFeatureFlagPayload: $exception');
return null;
}
}

@override
Future<Map?> getFeatureFlagAndPayload({
required String key,
}) async {
Future<void> register(String key, Object value) async {
try {
return await methodChannel.invokeMethod('getFeatureFlagAndPayload', {
'key': key,
});
return await methodChannel
.invokeMethod('register', {'key': key, 'value': value});
} on PlatformException catch (exception) {
if (kDebugMode) {
print('Exeption on getFeatureFlagAndPayload(): $exception');
}
rethrow;
_printIfDebug('Exeption on register: $exception');
}
}

Future<void> register(String key, dynamic value) async {
try {
return await methodChannel
.invokeMethod('register', {'key': key, 'value': value});
} on PlatformException catch (exception) {
print(exception);
void _printIfDebug(String message) {
if (kDebugMode) {
print(message);
}
}
}
33 changes: 13 additions & 20 deletions lib/posthog_flutter_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,23 @@ abstract class PosthogFlutterPlatform extends PlatformInterface {
_instance = instance;
}

Future<String?> getPlatformVersion() {
throw UnimplementedError('platformVersion() has not been implemented.');
}

Future<void> identify(
{required String userId, Map<String, dynamic>? properties}) {
{required String userId,
Map<String, Object>? userProperties,
Map<String, Object>? userPropertiesSetOnce}) {
throw UnimplementedError('identify() has not been implemented.');
}

Future<void> capture({
required String eventName,
Map<String, dynamic>? properties,
Map<String, Object>? properties,
}) {
throw UnimplementedError('capture() has not been implemented.');
}

Future<void> screen({
required String screenName,
Map<String, dynamic>? properties,
Map<String, Object>? properties,
}) {
throw UnimplementedError('screen() has not been implemented.');
}
Expand All @@ -52,8 +50,8 @@ abstract class PosthogFlutterPlatform extends PlatformInterface {
throw UnimplementedError('alias() has not been implemented.');
}

Future<String?> get getDistinctId {
throw UnimplementedError('getAnonymousId() has not been implemented.');
Future<String> get getDistinctId {
throw UnimplementedError('getDistinctId() has not been implemented.');
}

Future<void> reset() {
Expand All @@ -72,11 +70,11 @@ abstract class PosthogFlutterPlatform extends PlatformInterface {
throw UnimplementedError('debug() has not been implemented.');
}

Future<void> register(String key, dynamic value) {
Future<void> register(String key, Object value) {
throw UnimplementedError('register() has not been implemented.');
}

Future<bool?> isFeatureEnabled(String key) {
Future<bool> isFeatureEnabled(String key) {
throw UnimplementedError('isFeatureEnabled() has not been implemented.');
}

Expand All @@ -87,28 +85,23 @@ abstract class PosthogFlutterPlatform extends PlatformInterface {
Future<void> group({
required String groupType,
required String groupKey,
Map<String, dynamic>? groupProperties,
Map<String, Object>? groupProperties,
}) {
throw UnimplementedError('group() has not been implemented.');
}

Future<dynamic> getFeatureFlag({
Future<Object?> getFeatureFlag({
required String key,
}) {
throw UnimplementedError('getFeatureFlag() has not been implemented.');
}

Future<Map?> getFeatureFlagPayload({
Future<Object?> getFeatureFlagPayload({
required String key,
}) {
throw UnimplementedError(
'getFeatureFlagPayload() has not been implemented.');
}

Future<Map?> getFeatureFlagAndPayload({
required String key,
}) {
throw UnimplementedError(
'getFeatureFlagAndPayload() has not been implemented.');
}
// TODO: missing unregister, flush, capture with more parameters, close
}
17 changes: 3 additions & 14 deletions lib/posthog_flutter_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// of your plugin as a separate package, instead of inlining it in the same
// package as the core of your plugin.
// ignore: avoid_web_libraries_in_flutter
import 'dart:html' as html show window;
import 'dart:js';
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
Expand All @@ -24,13 +23,6 @@ class PosthogFlutterWeb extends PosthogFlutterPlatform {
channel.setMethodCallHandler(instance.handleMethodCall);
}

/// Returns a [String] containing the version of the platform.
@override
Future<String?> getPlatformVersion() async {
final version = html.window.navigator.userAgent;
return version;
}

Future<dynamic> handleMethodCall(MethodCall call) async {
final analytics = JsObject.fromBrowserObject(context['posthog']);
switch (call.method) {
Expand All @@ -57,9 +49,9 @@ class PosthogFlutterWeb extends PosthogFlutterPlatform {
call.arguments['alias'],
]);
break;
case 'getAnonymousId':
final anonymousId = analytics.callMethod('get_distinct_id');
return anonymousId;
case 'getDistinctId':
final distinctId = analytics.callMethod('get_distinct_id');
return distinctId;
case 'reset':
analytics.callMethod('reset');
break;
Expand Down Expand Up @@ -95,9 +87,6 @@ class PosthogFlutterWeb extends PosthogFlutterPlatform {
case 'getFeatureFlagPayload':
analytics.callMethod('getFeatureFlagPayload');
break;
case 'getFeatureFlagAndPayload':
analytics.callMethod('getFeatureFlagAndPayload');
break;
default:
throw PlatformException(
code: 'Unimplemented',
Expand Down
Loading

0 comments on commit 05c4fc6

Please sign in to comment.