Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add flush method #92

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- add flush method ([#92](https://github.com/PostHog/posthog-flutter/pull/92))

## 4.1.0

- add unregister method ([#86](https://github.com/PostHog/posthog-flutter/pull/86))
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ To use this plugin, add `posthog_flutter` as a [dependency in your pubspec.yaml
| `enable` | X | X | X |
| `debug` | X | X | X |
| `register` | X | X | X |
| `register` | X | X | X |
| `unregister` | X | X | X |
| `flush` | X | X | X |
| `isFeatureEnabled` | X | X | X |
| `reloadFeatureFlags` | X | X | X |
| `getFeatureFlag` | X | X | X |
| `group` | X | X | X |
| `getFeatureFlagPayload` | X | X | X |
| `group` | X | X | X |

### Example

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ class PosthogFlutterPlugin : FlutterPlugin, MethodCallHandler {
"debug" -> {
debug(call, result)
}

"flush" -> {
flush(result)
}
else -> {
result.notImplemented()
}
Expand Down Expand Up @@ -299,4 +301,13 @@ class PosthogFlutterPlugin : FlutterPlugin, MethodCallHandler {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}

private fun flush(result: Result) {
try {
PostHog.flush()
result.success(null)
} catch (e: Throwable) {
result.error("PosthogFlutterException", e.localizedMessage, null)
}
}
}
6 changes: 6 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ class _MyAppState extends State<MyApp> {
},
child: const Text("Reset"),
),
ElevatedButton(
onPressed: () async {
await _posthogFlutterPlugin.flush();
},
child: const Text("Flush"),
),
ElevatedButton(
onPressed: () async {
final result =
Expand Down
65 changes: 29 additions & 36 deletions ios/Classes/PosthogFlutterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,25 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin {
case "alias":
alias(call, result: result)
case "distinctId":
distinctId(call, result: result)
distinctId(result)
case "reset":
reset(call, result: result)
reset(result)
case "enable":
enable(call, result: result)
enable(result)
case "disable":
disable(call, result: result)
disable(result)
case "debug":
debug(call, result: result)
case "reloadFeatureFlags":
reloadFeatureFlags(call, result: result)
reloadFeatureFlags(result)
case "group":
group(call, result: result)
case "register":
register(call, result: result)
case "unregister":
unregister(call, result: result)
case "flush":
flush(result)
default:
result(FlutterMethodNotImplemented)
}
Expand All @@ -97,7 +99,7 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin {
let value = PostHogSDK.shared.getFeatureFlag(featureFlagKey)
result(value)
} else {
_badArgumentError(result: result)
_badArgumentError(result)
}
}

Expand All @@ -111,7 +113,7 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin {
let value = PostHogSDK.shared.isFeatureEnabled(featureFlagKey)
result(value)
} else {
_badArgumentError(result: result)
_badArgumentError(result)
}
}

Expand All @@ -125,7 +127,7 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin {
let value = PostHogSDK.shared.getFeatureFlagPayload(featureFlagKey)
result(value)
} else {
_badArgumentError(result: result)
_badArgumentError(result)
}
}

Expand All @@ -146,7 +148,7 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin {
)
result(nil)
} else {
_badArgumentError(result: result)
_badArgumentError(result)
}
}

Expand All @@ -164,7 +166,7 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin {
)
result(nil)
} else {
_badArgumentError(result: result)
_badArgumentError(result)
}
}

Expand All @@ -182,7 +184,7 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin {
)
result(nil)
} else {
_badArgumentError(result: result)
_badArgumentError(result)
}
}

Expand All @@ -196,38 +198,26 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin {
PostHogSDK.shared.alias(alias)
result(nil)
} else {
_badArgumentError(result: result)
_badArgumentError(result)
}
}

private func distinctId(
_: FlutterMethodCall,
result: @escaping FlutterResult
) {
private func distinctId(_ result: @escaping FlutterResult) {
let val = PostHogSDK.shared.getDistinctId()
result(val)
}

private func reset(
_: FlutterMethodCall,
result: @escaping FlutterResult
) {
private func reset(_ result: @escaping FlutterResult) {
PostHogSDK.shared.reset()
result(nil)
}

private func enable(
_: FlutterMethodCall,
result: @escaping FlutterResult
) {
private func enable(_ result: @escaping FlutterResult) {
PostHogSDK.shared.optIn()
result(nil)
}

private func disable(
_: FlutterMethodCall,
result: @escaping FlutterResult
) {
private func disable(_ result: @escaping FlutterResult) {
PostHogSDK.shared.optOut()
result(nil)
}
Expand All @@ -242,13 +232,11 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin {
PostHogSDK.shared.debug(debug)
result(nil)
} else {
_badArgumentError(result: result)
_badArgumentError(result)
}
}

private func reloadFeatureFlags(
_: FlutterMethodCall,
result: @escaping FlutterResult
private func reloadFeatureFlags(_ result: @escaping FlutterResult
) {
PostHogSDK.shared.reloadFeatureFlags()
result(nil)
Expand All @@ -266,7 +254,7 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin {
PostHogSDK.shared.group(type: groupType, key: groupKey, groupProperties: groupProperties)
result(nil)
} else {
_badArgumentError(result: result)
_badArgumentError(result)
}
}

Expand All @@ -281,7 +269,7 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin {
PostHogSDK.shared.register([key: value])
result(nil)
} else {
_badArgumentError(result: result)
_badArgumentError(result)
}
}

Expand All @@ -295,12 +283,17 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin {
PostHogSDK.shared.unregister(key)
result(nil)
} else {
_badArgumentError(result: result)
_badArgumentError(result)
}
}

private func flush(_ result: @escaping FlutterResult) {
PostHogSDK.shared.flush()
result(nil)
}

// Return bad Arguments error
private func _badArgumentError(result: @escaping FlutterResult) {
private func _badArgumentError(_ result: @escaping FlutterResult) {
result(FlutterError(
code: "PosthogFlutterException", message: "Missing arguments!", details: nil
))
Expand Down
4 changes: 4 additions & 0 deletions lib/src/posthog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,9 @@ class Posthog {
return _posthog.getFeatureFlagPayload(key: key);
}

Future<void> flush() async {
return _posthog.flush();
}

Posthog._internal();
}
9 changes: 9 additions & 0 deletions lib/src/posthog_flutter_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ class PosthogFlutterIO extends PosthogFlutterPlatformInterface {
}
}

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

void _printIfDebug(String message) {
if (kDebugMode) {
print(message);
Expand Down
6 changes: 5 additions & 1 deletion lib/src/posthog_flutter_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,9 @@ abstract class PosthogFlutterPlatformInterface extends PlatformInterface {
'getFeatureFlagPayload() has not been implemented.');
}

// TODO: missing unregister, flush, capture with more parameters, close
Future<void> flush() {
throw UnimplementedError('flush() has not been implemented.');
}

// TODO: missing capture with more parameters, close
}
4 changes: 4 additions & 0 deletions lib/src/posthog_flutter_web_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ Future<dynamic> handleWebMethodCall(MethodCall call, JsObject context) async {
call.arguments['key'],
]);
break;
case 'flush':
// not supported on Web
// analytics.callMethod('flush');
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
break;
default:
throw PlatformException(
code: 'Unimplemented',
Expand Down