Skip to content

Commit

Permalink
fix: Groups support and identify type fix (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite authored Jun 9, 2023
1 parent 9fc7433 commit ede5e09
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 62 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 3.1.0

- Adds support for `groups`
- Fixes a type issue with identify so that the userId is now always a String

## 3.0.5

- Fixes a bug with the iOS implementation for feature flags that stopped the SDK from building

## 3.0.4

- Adds CI/CD for deploying to pub.dev
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,98 +142,68 @@ public void onMethodCall(MethodCall call, Result result) {
this.isFeatureEnabled(call, result);
} else if (call.method.equals("reloadFeatureFlags")) {
this.reloadFeatureFlags(call, result);
} else if (call.method.equals("group")) {
this.group(call, result);
} else {
result.notImplemented();
}
}

private Properties hashMapToProperties(HashMap<String, Object> propertiesData) {
Properties properties = new Properties();

for(Map.Entry<String, Object> property : propertiesData.entrySet()) {
properties.putValue(property.getKey(), property.getValue());
}

return properties;
}

private void identify(MethodCall call, Result result) {
try {
String userId = call.argument("userId");
HashMap<String, Object> propertiesData = call.argument("properties");
HashMap<String, Object> options = call.argument("options");
this.callIdentify(userId, propertiesData, options);
HashMap<String, Object> optionsData = call.argument("options");
Properties properties = this.hashMapToProperties(propertiesData);
Options options = this.buildOptions(optionsData);
PostHog.with(this.applicationContext).identify(userId, properties, options);

result.success(true);
} catch (Exception e) {
result.error("PosthogFlutterException", e.getLocalizedMessage(), null);
}
}

private void callIdentify(
String userId,
HashMap<String, Object> propertiesData,
HashMap<String, Object> optionsData
) {
Properties properties = new Properties();
Options options = this.buildOptions(optionsData);

for(Map.Entry<String, Object> property : propertiesData.entrySet()) {
String key = property.getKey();
Object value = property.getValue();
properties.putValue(key, value);
}

PostHog.with(this.applicationContext).identify(userId, properties, options);
}

private void capture(MethodCall call, Result result) {
try {
String eventName = call.argument("eventName");
HashMap<String, Object> propertiesData = call.argument("properties");
HashMap<String, Object> options = call.argument("options");
this.callCapture(eventName, propertiesData, options);
HashMap<String, Object> optionsData = call.argument("options");
Properties properties = this.hashMapToProperties(propertiesData);
Options options = this.buildOptions(optionsData);

PostHog.with(this.applicationContext).capture(eventName, properties, options);
result.success(true);
} catch (Exception e) {
result.error("PosthogFlutterException", e.getLocalizedMessage(), null);
}
}

private void callCapture(
String eventName,
HashMap<String, Object> propertiesData,
HashMap<String, Object> optionsData
) {
Properties properties = new Properties();
Options options = this.buildOptions(optionsData);

for(Map.Entry<String, Object> property : propertiesData.entrySet()) {
String key = property.getKey();
Object value = property.getValue();
properties.putValue(key, value);
}

PostHog.with(this.applicationContext).capture(eventName, properties, options);
}

private void screen(MethodCall call, Result result) {
try {
String screenName = call.argument("screenName");
HashMap<String, Object> propertiesData = call.argument("properties");
HashMap<String, Object> options = call.argument("options");
this.callScreen(screenName, propertiesData, options);
HashMap<String, Object> optionsData = call.argument("options");
Properties properties = this.hashMapToProperties(propertiesData);
Options options = this.buildOptions(optionsData);

PostHog.with(this.applicationContext).screen(screenName, properties, options);
result.success(true);
} catch (Exception e) {
result.error("PosthogFlutterException", e.getLocalizedMessage(), null);
}
}

private void callScreen(
String screenName,
HashMap<String, Object> propertiesData,
HashMap<String, Object> optionsData
) {
Properties properties = new Properties();
Options options = this.buildOptions(optionsData);

for(Map.Entry<String, Object> property : propertiesData.entrySet()) {
String key = property.getKey();
Object value = property.getValue();
properties.putValue(key, value);
}

PostHog.with(this.applicationContext).screen(screenName, properties, options);
}

private void alias(MethodCall call, Result result) {
try {
String alias = call.argument("alias");
Expand Down Expand Up @@ -313,6 +283,20 @@ private void reloadFeatureFlags(MethodCall call, Result result) {
result.error("PosthogFlutterException", e.getLocalizedMessage(), null);
}
}

private void group(MethodCall call, Result result) {
try {
String groupType = call.argument("groupType");
String groupKey = call.argument("groupKey");
HashMap<String, Object> propertiesData = call.argument("groupProperties");
Properties properties = this.hashMapToProperties(propertiesData);

PostHog.with(this.applicationContext).group(groupType, groupKey, properties, null);
result.success(true);
} catch (Exception e) {
result.error("PosthogFlutterException", e.getLocalizedMessage(), null);
}
}
/**
* Enables / disables / sets custom integration properties so Posthog can properly
* interact with 3rd parties, such as Amplitude.
Expand Down
22 changes: 21 additions & 1 deletion ios/Classes/PosthogFlutterPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
[self isFeatureEnabled:call result:result];
} else if ([@"reloadFeatureFlags" isEqualToString:call.method]) {
[self reloadFeatureFlags:call result:result];
} else {
} else if ([@"group" isEqualToString:call.method]) {
[self group:call result:result];
} else {
result(FlutterMethodNotImplemented);
}
}
Expand Down Expand Up @@ -160,7 +162,25 @@ - (void)setContext:(FlutterMethodCall*)call result:(FlutterResult)result {
message:[exception reason]
details: nil]);
}
}


- (void)group:(FlutterMethodCall*)call result:(FlutterResult)result {
@try {
NSString *groupType = call.arguments[@"groupType"];
NSString *groupKey = call.arguments[@"groupKey"];
NSDictionary *groupProperties = call.arguments[@"groupProperties"];
[[PHGPostHog sharedPostHog] group: groupType
groupKey: groupKey
properties: groupProperties];
result([NSNumber numberWithBool:YES]);
}
@catch (NSException *exception) {
result([FlutterError
errorWithCode:@"PosthogFlutterException"
message:[exception reason]
details: nil]);
}
}

- (void)identify:(FlutterMethodCall*)call result:(FlutterResult)result {
Expand Down
14 changes: 13 additions & 1 deletion lib/src/posthog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Posthog {
String? currentScreen;

Future<void> identify({
required userId,
required String userId,
Map<String, dynamic>? properties,
Map<String, dynamic>? options,
}) {
Expand Down Expand Up @@ -106,6 +106,18 @@ class Posthog {
Future<void> reloadFeatureFlags() {
return _posthog.reloadFeatureFlags();
}

Future<void> group({
required String groupType,
required String groupKey,
required Map<String, dynamic> groupProperties,
}) {
return _posthog.group(
groupType: groupType,
groupKey: groupKey,
groupProperties: groupProperties
);
}

Posthog._internal();
}
18 changes: 17 additions & 1 deletion lib/src/posthog_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const MethodChannel _channel = MethodChannel('posthogflutter');

class PosthogMethodChannel extends PosthogPlatform {
Future<void> identify({
required userId,
required String userId,
Map<String, dynamic>? properties,
Map<String, dynamic>? options,
}) async {
Expand Down Expand Up @@ -134,4 +134,20 @@ class PosthogMethodChannel extends PosthogPlatform {
print(exception);
}
}

Future<void> group({
required String groupType,
required String groupKey,
Map<String, dynamic>? groupProperties,
}) async {
try {
await _channel.invokeMethod('group', {
'groupType': groupType,
'groupKey': groupKey,
'groupProperties': groupProperties ?? {},
});
} on PlatformException catch (exception) {
print(exception);
}
}
}
10 changes: 9 additions & 1 deletion lib/src/posthog_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class PosthogPlatform {
static PosthogPlatform instance = PosthogMethodChannel();

Future<void> identify({
required userId,
required String userId,
Map<String, dynamic>? properties,
Map<String, dynamic>? options,
}) {
Expand Down Expand Up @@ -72,4 +72,12 @@ abstract class PosthogPlatform {
Future<void> reloadFeatureFlags() {
throw UnimplementedError('reloadFeatureFlags() has not been implemented.');
}

Future<void> group({
required String groupType,
required String groupKey,
Map<String, dynamic>? groupProperties
}) {
throw UnimplementedError('group() has not been implemented.');
}
}
7 changes: 7 additions & 0 deletions lib/src/posthog_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ class PosthogWeb {
call.arguments['key'],
]);
return isFeatureEnabled;
case 'group':
analytics.callMethod('group', [
call.arguments['groupType'],
call.arguments['groupKey'],
JsObject.jsify(call.arguments['groupProperties']),
]);
break;
case 'reloadFeatureFlags':
analytics.callMethod('reloadFeatureFlags');
break;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: posthog_flutter
description: Flutter implementation of Posthog client for iOS, Android and Web
version: 3.0.5
version: 3.1.0
homepage: https://www.posthog.com
repository: https://github.com/posthog/posthog-flutter
issue_tracker: https://github.com/posthog/posthog-flutter/issues
Expand Down

0 comments on commit ede5e09

Please sign in to comment.