From 2a8ebf6f5fadc11a34c8386630e1d0c979146dbb Mon Sep 17 00:00:00 2001 From: Felix Weuthen Date: Thu, 23 May 2024 16:20:11 +0200 Subject: [PATCH 1/5] initial commit --- lib/posthog_flutter_web.dart | 5 +- lib/src/posthog_flutter_web_handler.dart | 60 +++++++++++++----------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/lib/posthog_flutter_web.dart b/lib/posthog_flutter_web.dart index 93c75dc..d892830 100644 --- a/lib/posthog_flutter_web.dart +++ b/lib/posthog_flutter_web.dart @@ -2,7 +2,8 @@ // 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:js'; +import 'dart:js_interop'; + import 'package:flutter/services.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; @@ -25,6 +26,6 @@ class PosthogFlutterWeb extends PosthogFlutterPlatformInterface { } Future handleMethodCall(MethodCall call) async { - return handleWebMethodCall(call, context); + return handleWebMethodCall(call, globalContext); } } diff --git a/lib/src/posthog_flutter_web_handler.dart b/lib/src/posthog_flutter_web_handler.dart index 70843bd..3dacffc 100644 --- a/lib/src/posthog_flutter_web_handler.dart +++ b/lib/src/posthog_flutter_web_handler.dart @@ -1,25 +1,25 @@ -import 'dart:js'; - +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; import 'package:flutter/services.dart'; -Future handleWebMethodCall(MethodCall call, JsObject context) async { - final analytics = JsObject.fromBrowserObject(context['posthog']); +Future handleWebMethodCall(MethodCall call, JSObject context) async { + final JSObject analytics = context.getProperty('posthog'.toJS)!; switch (call.method) { case 'identify': final userProperties = call.arguments['userProperties'] ?? {}; final userPropertiesSetOnce = call.arguments['userPropertiesSetOnce'] ?? {}; - analytics.callMethod('identify', [ + analytics.callMethodVarArgs('identify'.toJS, [ call.arguments['userId'], - JsObject.jsify(userProperties), - JsObject.jsify(userPropertiesSetOnce), + userProperties, + jsify(userPropertiesSetOnce), ]); break; case 'capture': final properties = call.arguments['properties'] ?? {}; - analytics.callMethod('capture', [ + analytics.callMethodVarArgs('capture'.toJS, [ call.arguments['eventName'], - JsObject.jsify(properties), + jsify(properties), ]); break; case 'screen': @@ -27,66 +27,68 @@ Future handleWebMethodCall(MethodCall call, JsObject context) async { final screenName = call.arguments['screenName']; properties['\$screen_name'] = screenName; - analytics.callMethod('capture', [ - '\$screen', - JsObject.jsify(properties), + analytics.callMethodVarArgs('capture'.toJS, [ + '\$screen'.toJS, + jsify(properties), ]); break; case 'alias': - analytics.callMethod('alias', [ + analytics.callMethodVarArgs('alias'.toJS, [ call.arguments['alias'], ]); break; case 'distinctId': - final distinctId = analytics.callMethod('get_distinct_id'); + final distinctId = analytics.callMethod('get_distinct_id'.toJS); return distinctId; case 'reset': - analytics.callMethod('reset'); + analytics.callMethod('reset'.toJS); break; case 'debug': - analytics.callMethod('debug', [ + analytics.callMethodVarArgs('debug'.toJS, [ call.arguments['debug'], ]); break; case 'isFeatureEnabled': - final isFeatureEnabled = analytics.callMethod('isFeatureEnabled', [ + final isFeatureEnabled = + analytics.callMethodVarArgs('isFeatureEnabled'.toJS, [ call.arguments['key'], ]); return isFeatureEnabled; case 'group': - analytics.callMethod('group', [ + analytics.callMethodVarArgs('group'.toJS, [ call.arguments['groupType'], call.arguments['groupKey'], - JsObject.jsify(call.arguments['groupProperties'] ?? {}), + jsify(call.arguments['groupProperties'] ?? {}), ]); break; case 'reloadFeatureFlags': - analytics.callMethod('reloadFeatureFlags'); + analytics.callMethod('reloadFeatureFlags'.toJS); break; case 'enable': - analytics.callMethod('opt_in_capturing'); + analytics.callMethod('opt_in_capturing'.toJS); break; case 'disable': - analytics.callMethod('opt_out_capturing'); + analytics.callMethod('opt_out_capturing'.toJS); break; case 'getFeatureFlag': - final featureFlag = analytics.callMethod('getFeatureFlag', [ + final featureFlag = analytics.callMethodVarArgs('getFeatureFlag'.toJS, [ call.arguments['key'], ]); return featureFlag; case 'getFeatureFlagPayload': - final featureFlag = analytics.callMethod('getFeatureFlagPayload', [ + final featureFlag = + analytics.callMethodVarArgs('getFeatureFlagPayload'.toJS, [ call.arguments['key'], ]); return featureFlag; case 'register': final properties = {call.arguments['key']: call.arguments['value']}; - analytics.callMethod('register', [ - JsObject.jsify(properties), + analytics.callMethodVarArgs('register'.toJS, [ + jsify(properties), ]); break; case 'unregister': - analytics.callMethod('unregister', [ + analytics.callMethodVarArgs('unregister'.toJS, [ call.arguments['key'], ]); break; @@ -102,3 +104,7 @@ Future handleWebMethodCall(MethodCall call, JsObject context) async { ); } } + +JSAny jsify(dynamic any) { + return any as JSAny; +} From b59468a978b76a1fcb6fd3e8ba31882c35889c97 Mon Sep 17 00:00:00 2001 From: Felix Weuthen Date: Thu, 23 May 2024 17:08:59 +0200 Subject: [PATCH 2/5] some potential fixes --- lib/src/posthog_flutter_web_handler.dart | 38 +++++++++++------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/lib/src/posthog_flutter_web_handler.dart b/lib/src/posthog_flutter_web_handler.dart index 3dacffc..93fd6ec 100644 --- a/lib/src/posthog_flutter_web_handler.dart +++ b/lib/src/posthog_flutter_web_handler.dart @@ -6,30 +6,30 @@ Future handleWebMethodCall(MethodCall call, JSObject context) async { final JSObject analytics = context.getProperty('posthog'.toJS)!; switch (call.method) { case 'identify': - final userProperties = call.arguments['userProperties'] ?? {}; - final userPropertiesSetOnce = + final Map userProperties = call.arguments['userProperties'] ?? {}; + final Map userPropertiesSetOnce = call.arguments['userPropertiesSetOnce'] ?? {}; analytics.callMethodVarArgs('identify'.toJS, [ call.arguments['userId'], - userProperties, - jsify(userPropertiesSetOnce), + userProperties.toJSBox, + userPropertiesSetOnce.toJSBox, ]); break; case 'capture': final properties = call.arguments['properties'] ?? {}; analytics.callMethodVarArgs('capture'.toJS, [ call.arguments['eventName'], - jsify(properties), + properties.jsify(), ]); break; case 'screen': - final properties = call.arguments['properties'] ?? {}; + final Map properties = call.arguments['properties'] ?? {}; final screenName = call.arguments['screenName']; properties['\$screen_name'] = screenName; analytics.callMethodVarArgs('capture'.toJS, [ '\$screen'.toJS, - jsify(properties), + properties.toJSBox, ]); break; case 'alias': @@ -39,13 +39,13 @@ Future handleWebMethodCall(MethodCall call, JSObject context) async { break; case 'distinctId': final distinctId = analytics.callMethod('get_distinct_id'.toJS); - return distinctId; + return distinctId?.dartify(); case 'reset': analytics.callMethod('reset'.toJS); break; case 'debug': analytics.callMethodVarArgs('debug'.toJS, [ - call.arguments['debug'], + call.arguments['debug'].toJS, ]); break; case 'isFeatureEnabled': @@ -56,9 +56,9 @@ Future handleWebMethodCall(MethodCall call, JSObject context) async { return isFeatureEnabled; case 'group': analytics.callMethodVarArgs('group'.toJS, [ - call.arguments['groupType'], - call.arguments['groupKey'], - jsify(call.arguments['groupProperties'] ?? {}), + call.arguments['groupType'].toJS, + call.arguments['groupKey'].toJS, + (call.arguments['groupProperties'] ?? {}).jsify(), ]); break; case 'reloadFeatureFlags': @@ -72,24 +72,24 @@ Future handleWebMethodCall(MethodCall call, JSObject context) async { break; case 'getFeatureFlag': final featureFlag = analytics.callMethodVarArgs('getFeatureFlag'.toJS, [ - call.arguments['key'], + call.arguments['key'].toJS, ]); return featureFlag; case 'getFeatureFlagPayload': final featureFlag = analytics.callMethodVarArgs('getFeatureFlagPayload'.toJS, [ - call.arguments['key'], + call.arguments['key'].toJS, ]); - return featureFlag; + return featureFlag?.dartify(); case 'register': final properties = {call.arguments['key']: call.arguments['value']}; analytics.callMethodVarArgs('register'.toJS, [ - jsify(properties), + properties.toJSBox, ]); break; case 'unregister': analytics.callMethodVarArgs('unregister'.toJS, [ - call.arguments['key'], + call.arguments['key'].toJS, ]); break; case 'flush': @@ -104,7 +104,3 @@ Future handleWebMethodCall(MethodCall call, JSObject context) async { ); } } - -JSAny jsify(dynamic any) { - return any as JSAny; -} From 62dee002656a9a66c57da9d8ccc68f65c263bc8e Mon Sep 17 00:00:00 2001 From: Felix Weuthen Date: Fri, 9 Aug 2024 09:08:39 +0200 Subject: [PATCH 3/5] fixed jsify methods --- lib/src/posthog_flutter_web_handler.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/src/posthog_flutter_web_handler.dart b/lib/src/posthog_flutter_web_handler.dart index 93fd6ec..b3bd7fe 100644 --- a/lib/src/posthog_flutter_web_handler.dart +++ b/lib/src/posthog_flutter_web_handler.dart @@ -11,8 +11,8 @@ Future handleWebMethodCall(MethodCall call, JSObject context) async { call.arguments['userPropertiesSetOnce'] ?? {}; analytics.callMethodVarArgs('identify'.toJS, [ call.arguments['userId'], - userProperties.toJSBox, - userPropertiesSetOnce.toJSBox, + userProperties.jsify(), + userPropertiesSetOnce.jsify(), ]); break; case 'capture': @@ -29,7 +29,7 @@ Future handleWebMethodCall(MethodCall call, JSObject context) async { analytics.callMethodVarArgs('capture'.toJS, [ '\$screen'.toJS, - properties.toJSBox, + properties.jsify(), ]); break; case 'alias': @@ -84,7 +84,7 @@ Future handleWebMethodCall(MethodCall call, JSObject context) async { case 'register': final properties = {call.arguments['key']: call.arguments['value']}; analytics.callMethodVarArgs('register'.toJS, [ - properties.toJSBox, + properties.jsify(), ]); break; case 'unregister': From 33ace0ee44d0dfce85beb6bb97d505177dba791f Mon Sep 17 00:00:00 2001 From: Felix Weuthen Date: Fri, 9 Aug 2024 09:12:40 +0200 Subject: [PATCH 4/5] removed unnessecary type fields --- lib/src/posthog_flutter_web_handler.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/src/posthog_flutter_web_handler.dart b/lib/src/posthog_flutter_web_handler.dart index b3bd7fe..cab085c 100644 --- a/lib/src/posthog_flutter_web_handler.dart +++ b/lib/src/posthog_flutter_web_handler.dart @@ -6,8 +6,8 @@ Future handleWebMethodCall(MethodCall call, JSObject context) async { final JSObject analytics = context.getProperty('posthog'.toJS)!; switch (call.method) { case 'identify': - final Map userProperties = call.arguments['userProperties'] ?? {}; - final Map userPropertiesSetOnce = + final userProperties = call.arguments['userProperties'] ?? {}; + final userPropertiesSetOnce = call.arguments['userPropertiesSetOnce'] ?? {}; analytics.callMethodVarArgs('identify'.toJS, [ call.arguments['userId'], @@ -23,7 +23,7 @@ Future handleWebMethodCall(MethodCall call, JSObject context) async { ]); break; case 'screen': - final Map properties = call.arguments['properties'] ?? {}; + final properties = call.arguments['properties'] ?? {}; final screenName = call.arguments['screenName']; properties['\$screen_name'] = screenName; From c965357bdb2205ddd2133c26916921cd4e55a97d Mon Sep 17 00:00:00 2001 From: Felix Weuthen Date: Fri, 9 Aug 2024 09:24:34 +0200 Subject: [PATCH 5/5] added more (as String).toJS wrappers to ensure conversion --- lib/src/posthog_flutter_web_handler.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/src/posthog_flutter_web_handler.dart b/lib/src/posthog_flutter_web_handler.dart index cab085c..de7b362 100644 --- a/lib/src/posthog_flutter_web_handler.dart +++ b/lib/src/posthog_flutter_web_handler.dart @@ -10,7 +10,7 @@ Future handleWebMethodCall(MethodCall call, JSObject context) async { final userPropertiesSetOnce = call.arguments['userPropertiesSetOnce'] ?? {}; analytics.callMethodVarArgs('identify'.toJS, [ - call.arguments['userId'], + (call.arguments['userId'] as String).toJS, userProperties.jsify(), userPropertiesSetOnce.jsify(), ]); @@ -18,7 +18,7 @@ Future handleWebMethodCall(MethodCall call, JSObject context) async { case 'capture': final properties = call.arguments['properties'] ?? {}; analytics.callMethodVarArgs('capture'.toJS, [ - call.arguments['eventName'], + (call.arguments['eventName'] as String).toJS, properties.jsify(), ]); break; @@ -34,7 +34,7 @@ Future handleWebMethodCall(MethodCall call, JSObject context) async { break; case 'alias': analytics.callMethodVarArgs('alias'.toJS, [ - call.arguments['alias'], + (call.arguments['alias'] as String).toJS, ]); break; case 'distinctId': @@ -51,7 +51,7 @@ Future handleWebMethodCall(MethodCall call, JSObject context) async { case 'isFeatureEnabled': final isFeatureEnabled = analytics.callMethodVarArgs('isFeatureEnabled'.toJS, [ - call.arguments['key'], + (call.arguments['key'] as String).toJS, ]); return isFeatureEnabled; case 'group':