diff --git a/lib/posthog_flutter.dart b/lib/posthog_flutter.dart index 5d6ab45..6250534 100644 --- a/lib/posthog_flutter.dart +++ b/lib/posthog_flutter.dart @@ -1,2 +1,4 @@ -export 'package:posthog_flutter/src/posthog.dart'; -export 'package:posthog_flutter/src/posthog_observer.dart'; +library posthog_flutter; + +export 'src/posthog.dart'; +export 'src/posthog_observer.dart'; diff --git a/lib/posthog_flutter_web.dart b/lib/posthog_flutter_web.dart index c8ef9a7..d6c78b4 100644 --- a/lib/posthog_flutter_web.dart +++ b/lib/posthog_flutter_web.dart @@ -6,10 +6,11 @@ import 'dart:js'; import 'package:flutter/services.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; -import 'posthog_flutter_platform_interface.dart'; +import 'src/posthog_flutter_platform_interface.dart'; +import 'src/posthog_flutter_web_handler.dart'; /// A web implementation of the PosthogFlutterPlatform of the PosthogFlutter plugin. -class PosthogFlutterWeb extends PosthogFlutterPlatform { +class PosthogFlutterWeb extends PosthogFlutterPlatformInterface { /// Constructs a PosthogFlutterWeb PosthogFlutterWeb(); @@ -24,93 +25,6 @@ class PosthogFlutterWeb extends PosthogFlutterPlatform { } Future handleMethodCall(MethodCall call) async { - final analytics = JsObject.fromBrowserObject(context['posthog']); - switch (call.method) { - case 'identify': - final userProperties = call.arguments['userProperties']; - final userPropertiesSetOnce = call.arguments['userPropertiesSetOnce']; - analytics.callMethod('identify', [ - call.arguments['userId'], - JsObject.jsify(userProperties), - JsObject.jsify(userPropertiesSetOnce), - ]); - break; - case 'capture': - analytics.callMethod('capture', [ - call.arguments['eventName'], - JsObject.jsify(call.arguments['properties']), - ]); - break; - case 'screen': - final properties = call.arguments['properties'] ?? {}; - final screenName = call.arguments['screenName']; - if (screenName != null) { - properties['\$screen_name'] = screenName; - } - analytics.callMethod('capture', [ - '\$screen', - JsObject.jsify(properties), - ]); - break; - case 'alias': - analytics.callMethod('alias', [ - call.arguments['alias'], - ]); - break; - case 'distinctId': - final distinctId = analytics.callMethod('get_distinct_id'); - return distinctId; - case 'reset': - analytics.callMethod('reset'); - break; - case 'debug': - analytics.callMethod('debug', [ - call.arguments['debug'], - ]); - break; - case 'isFeatureEnabled': - final isFeatureEnabled = analytics.callMethod('isFeatureEnabled', [ - 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; - case 'enable': - analytics.callMethod('opt_in_capturing'); - break; - case 'disable': - analytics.callMethod('opt_out_capturing'); - break; - case 'getFeatureFlag': - analytics.callMethod('getFeatureFlag', [ - call.arguments['key'], - ]); - break; - case 'getFeatureFlagPayload': - analytics.callMethod('getFeatureFlagPayload', [ - call.arguments['key'], - ]); - break; - case 'register': - final properties = {call.arguments['key']: call.arguments['value']}; - analytics.callMethod('register', [ - properties, - ]); - break; - default: - throw PlatformException( - code: 'Unimplemented', - details: - "The posthog plugin for web doesn't implement the method '${call.method}'", - ); - } + handleWebMethodCall(call, context); } } diff --git a/lib/src/posthog.dart b/lib/src/posthog.dart index a635ada..2cd4cad 100644 --- a/lib/src/posthog.dart +++ b/lib/src/posthog.dart @@ -1,8 +1,8 @@ -import 'package:posthog_flutter/posthog_flutter_platform_interface.dart'; -export 'package:posthog_flutter/src/posthog_observer.dart'; +import 'posthog_flutter_platform_interface.dart'; class Posthog { - static PosthogFlutterPlatform get _posthog => PosthogFlutterPlatform.instance; + static PosthogFlutterPlatformInterface get _posthog => + PosthogFlutterPlatformInterface.instance; static final Posthog _instance = Posthog._internal(); diff --git a/lib/posthog_flutter_method_channel.dart b/lib/src/posthog_flutter_io.dart similarity index 97% rename from lib/posthog_flutter_method_channel.dart rename to lib/src/posthog_flutter_io.dart index 15851cb..4acc381 100644 --- a/lib/posthog_flutter_method_channel.dart +++ b/lib/src/posthog_flutter_io.dart @@ -3,8 +3,8 @@ import 'package:flutter/services.dart'; import 'posthog_flutter_platform_interface.dart'; -/// An implementation of [PosthogFlutterPlatform] that uses method channels. -class MethodChannelPosthogFlutter extends PosthogFlutterPlatform { +/// An implementation of [PosthogFlutterPlatformInterface] that uses method channels. +class PosthogFlutterIO extends PosthogFlutterPlatformInterface { /// The method channel used to interact with the native platform. final _methodChannel = const MethodChannel('posthog_flutter'); diff --git a/lib/posthog_flutter_platform_interface.dart b/lib/src/posthog_flutter_platform_interface.dart similarity index 83% rename from lib/posthog_flutter_platform_interface.dart rename to lib/src/posthog_flutter_platform_interface.dart index be38527..7860bf9 100644 --- a/lib/posthog_flutter_platform_interface.dart +++ b/lib/src/posthog_flutter_platform_interface.dart @@ -1,24 +1,24 @@ import 'package:plugin_platform_interface/plugin_platform_interface.dart'; -import 'posthog_flutter_method_channel.dart'; +import 'posthog_flutter_io.dart'; -abstract class PosthogFlutterPlatform extends PlatformInterface { +abstract class PosthogFlutterPlatformInterface extends PlatformInterface { /// Constructs a PosthogFlutterPlatform. - PosthogFlutterPlatform() : super(token: _token); + PosthogFlutterPlatformInterface() : super(token: _token); static final Object _token = Object(); - static PosthogFlutterPlatform _instance = MethodChannelPosthogFlutter(); + static PosthogFlutterPlatformInterface _instance = PosthogFlutterIO(); - /// The default instance of [PosthogFlutterPlatform] to use. + /// The default instance of [PosthogFlutterPlatformInterface] to use. /// - /// Defaults to [MethodChannelPosthogFlutter]. - static PosthogFlutterPlatform get instance => _instance; + /// Defaults to [PosthogFlutterIO]. + static PosthogFlutterPlatformInterface get instance => _instance; /// Platform-specific implementations should set this with their own - /// platform-specific class that extends [PosthogFlutterPlatform] when + /// platform-specific class that extends [PosthogFlutterPlatformInterface] when /// they register themselves. - static set instance(PosthogFlutterPlatform instance) { + static set instance(PosthogFlutterPlatformInterface instance) { PlatformInterface.verifyToken(instance, _token); _instance = instance; } diff --git a/lib/src/posthog_flutter_web_handler.dart b/lib/src/posthog_flutter_web_handler.dart new file mode 100644 index 0000000..bed4bab --- /dev/null +++ b/lib/src/posthog_flutter_web_handler.dart @@ -0,0 +1,94 @@ +import 'dart:js'; + +import 'package:flutter/services.dart'; + +Future handleWebMethodCall(MethodCall call, JsObject context) async { + final analytics = JsObject.fromBrowserObject(context['posthog']); + switch (call.method) { + case 'identify': + final userProperties = call.arguments['userProperties']; + final userPropertiesSetOnce = call.arguments['userPropertiesSetOnce']; + analytics.callMethod('identify', [ + call.arguments['userId'], + JsObject.jsify(userProperties), + JsObject.jsify(userPropertiesSetOnce), + ]); + break; + case 'capture': + analytics.callMethod('capture', [ + call.arguments['eventName'], + JsObject.jsify(call.arguments['properties']), + ]); + break; + case 'screen': + final properties = call.arguments['properties'] ?? {}; + final screenName = call.arguments['screenName']; + if (screenName != null) { + properties['\$screen_name'] = screenName; + } + analytics.callMethod('capture', [ + '\$screen', + JsObject.jsify(properties), + ]); + break; + case 'alias': + analytics.callMethod('alias', [ + call.arguments['alias'], + ]); + break; + case 'distinctId': + final distinctId = analytics.callMethod('get_distinct_id'); + return distinctId; + case 'reset': + analytics.callMethod('reset'); + break; + case 'debug': + analytics.callMethod('debug', [ + call.arguments['debug'], + ]); + break; + case 'isFeatureEnabled': + final isFeatureEnabled = analytics.callMethod('isFeatureEnabled', [ + 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; + case 'enable': + analytics.callMethod('opt_in_capturing'); + break; + case 'disable': + analytics.callMethod('opt_out_capturing'); + break; + case 'getFeatureFlag': + final featureFlag = analytics.callMethod('getFeatureFlag', [ + call.arguments['key'], + ]); + return featureFlag; + case 'getFeatureFlagPayload': + final featureFlag = analytics.callMethod('getFeatureFlagPayload', [ + call.arguments['key'], + ]); + return featureFlag; + case 'register': + final properties = {call.arguments['key']: call.arguments['value']}; + analytics.callMethod('register', [ + properties, + ]); + break; + default: + throw PlatformException( + code: 'Unimplemented', + details: + "The posthog plugin for web doesn't implement the method '${call.method}'", + ); + } +} diff --git a/lib/src/posthog_observer.dart b/lib/src/posthog_observer.dart index cb76f88..b804a16 100644 --- a/lib/src/posthog_observer.dart +++ b/lib/src/posthog_observer.dart @@ -1,5 +1,6 @@ import 'package:flutter/widgets.dart'; -import 'package:posthog_flutter/posthog_flutter.dart'; + +import 'posthog.dart'; typedef ScreenNameExtractor = String? Function(RouteSettings settings);