@@ -5,6 +5,24 @@ import 'package:flutter/services.dart';
55import 'package:flutter_web_plugins/flutter_web_plugins.dart' ;
66import 'package:mixpanel_flutter/web/mixpanel_js_bindings.dart' ;
77
8+ JSAny ? safeJsify (dynamic value) {
9+ if (value is Map ) {
10+ return value.jsify ();
11+ } else if (value is List ) {
12+ return value.jsify ();
13+ } else if (value is DateTime ) {
14+ return value.jsify ();
15+ } else if (value is bool ) {
16+ return value.toJS;
17+ } else if (value is num ) {
18+ return value.toJS;
19+ } else if (value is String ) {
20+ return value.toJS;
21+ } else {
22+ return value;
23+ }
24+ }
25+
826/// A web implementation of the MixpanelFlutter plugin.
927class MixpanelFlutterPlugin {
1028 static Map <String , String > _mixpanelProperties = {
@@ -138,13 +156,13 @@ class MixpanelFlutterPlugin {
138156 Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
139157 String token = args['token' ] as String ;
140158 dynamic config = args['config' ];
141- init (token, config ? . jsify ( ) ?? {} );
159+ init (token, safeJsify (config ) ?? < String , dynamic > {}. jsify () );
142160 }
143161
144162 void handleSetServerURL (MethodCall call) {
145163 Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
146164 String serverURL = args['serverURL' ] as String ;
147- set_config ({'api_host' : serverURL}. jsify ( ));
165+ set_config (safeJsify ( {'api_host' : serverURL}));
148166 }
149167
150168 void handleTrack (MethodCall call) {
@@ -155,7 +173,7 @@ class MixpanelFlutterPlugin {
155173 ..._mixpanelProperties,
156174 ...(properties ?? {})
157175 };
158- track (eventName, props. jsify ( ));
176+ track (eventName, safeJsify (props ));
159177 }
160178
161179 void handleAlias (MethodCall call) {
@@ -180,16 +198,15 @@ class MixpanelFlutterPlugin {
180198 ...(properties ?? {})
181199 };
182200 dynamic groups = args["groups" ];
183- track_with_groups (eventName, props. jsify ( ), groups. jsify ( ));
201+ track_with_groups (eventName, safeJsify (props ), safeJsify (groups ));
184202 }
185203
186204 void handleSetGroup (MethodCall call) {
187205 Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
188206 String groupKey = args['groupKey' ] as String ;
189207 dynamic groupID = args["groupID" ];
190208 if (groupID != null ) {
191- set_group (groupKey,
192- (groupID is Map || groupID is List ) ? groupID.jsify () : groupID);
209+ set_group (groupKey, safeJsify (groupID));
193210 }
194211 }
195212
@@ -199,8 +216,7 @@ class MixpanelFlutterPlugin {
199216 dynamic groupID = args["groupID" ];
200217
201218 if (groupID != null ) {
202- add_group (groupKey,
203- (groupID is Map || groupID is List ) ? groupID.jsify () : groupID);
219+ add_group (groupKey, safeJsify (groupID));
204220 }
205221 }
206222
@@ -209,21 +225,20 @@ class MixpanelFlutterPlugin {
209225 String groupKey = args['groupKey' ] as String ;
210226 dynamic groupID = args["groupID" ];
211227 if (groupID != null ) {
212- remove_group (groupKey,
213- (groupID is Map || groupID is List ) ? groupID.jsify () : groupID);
228+ remove_group (groupKey, safeJsify (groupID));
214229 }
215230 }
216231
217232 void handleRegisterSuperProperties (MethodCall call) {
218233 Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
219234 dynamic properties = args['properties' ];
220- register (properties. jsify ( ));
235+ register (safeJsify (properties ));
221236 }
222237
223238 void handleRegisterSuperPropertiesOnce (MethodCall call) {
224239 Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
225240 dynamic properties = args['properties' ];
226- register_once (properties. jsify ( ));
241+ register_once (safeJsify (properties ));
227242 }
228243
229244 void handleUnregisterSuperProperty (MethodCall call) {
@@ -250,51 +265,51 @@ class MixpanelFlutterPlugin {
250265 Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
251266 dynamic properties = args['properties' ];
252267 Map <String , dynamic > props = {..._mixpanelProperties, ...properties};
253- people_set (props. jsify ( ));
268+ people_set (safeJsify (props ));
254269 }
255270
256271 void handleSetOnce (MethodCall call) {
257272 Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
258273 dynamic properties = args['properties' ];
259274 Map <String , dynamic > props = {..._mixpanelProperties, ...properties};
260- people_set_once (props. jsify ( ));
275+ people_set_once (safeJsify (props ));
261276 }
262277
263278 void handlePeopleIncrement (MethodCall call) {
264279 Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
265280 dynamic properties = args['properties' ];
266- people_increment (properties. jsify ( ));
281+ people_increment (safeJsify (properties ));
267282 }
268283
269284 void handlePeopleAppend (MethodCall call) {
270285 Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
271286 dynamic properties = args['properties' ];
272- people_append (properties. jsify ( ));
287+ people_append (safeJsify (properties ));
273288 }
274289
275290 void handlePeopleUnion (MethodCall call) {
276291 Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
277292 dynamic properties = args['properties' ];
278- people_union (properties. jsify ( ));
293+ people_union (safeJsify (properties ));
279294 }
280295
281296 void handlePeopleRemove (MethodCall call) {
282297 Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
283298 dynamic properties = args['properties' ];
284- people_remove (properties. jsify ( ));
299+ people_remove (safeJsify (properties ));
285300 }
286301
287302 void handlePeopleUnset (MethodCall call) {
288303 Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
289304 dynamic properties = args['properties' ];
290- people_unset (properties. jsify ( ));
305+ people_unset (safeJsify (properties ));
291306 }
292307
293308 void handleTrackCharge (MethodCall call) {
294309 Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
295310 dynamic properties = args['properties' ];
296311 double amount = args['amount' ] as double ;
297- people_track_charge (amount, properties ? . jsify ( ) ?? {} );
312+ people_track_charge (amount, safeJsify (properties ) ?? < String , dynamic > {}. jsify () );
298313 }
299314
300315 void handleClearCharge () {
@@ -311,9 +326,8 @@ class MixpanelFlutterPlugin {
311326 dynamic groupID = args['groupID' ];
312327
313328 dynamic properties = args['properties' ];
314- get_group (groupKey,
315- (groupID is Map || groupID is List ) ? groupID.jsify () : groupID)
316- .set (properties.jsify ());
329+ get_group (groupKey, safeJsify (groupID))
330+ .set (safeJsify (properties));
317331 }
318332
319333 void handleGroupSetPropertyOnce (MethodCall call) {
@@ -323,8 +337,7 @@ class MixpanelFlutterPlugin {
323337
324338 dynamic properties = args['properties' ];
325339
326- get_group (groupKey,
327- (groupID is Map || groupID is List ) ? groupID.jsify () : groupID)
340+ get_group (groupKey, safeJsify (groupID))
328341 .set_once (properties.keys.first, properties[properties.keys.first]);
329342 }
330343
@@ -334,8 +347,7 @@ class MixpanelFlutterPlugin {
334347 dynamic groupID = args['groupID' ];
335348
336349 String propertyName = args['propertyName' ] as String ;
337- get_group (groupKey,
338- (groupID is Map || groupID is List ) ? groupID.jsify () : groupID)
350+ get_group (groupKey, safeJsify (groupID))
339351 .unset (propertyName);
340352 }
341353
@@ -346,9 +358,8 @@ class MixpanelFlutterPlugin {
346358
347359 String name = args['name' ] as String ;
348360 dynamic value = args['value' ];
349- get_group (groupKey,
350- (groupID is Map || groupID is List ) ? groupID.jsify () : groupID)
351- .remove (name, (value is Map || value is List ) ? value.jsify () : value);
361+ get_group (groupKey, safeJsify (groupID))
362+ .remove (name, safeJsify (value));
352363 }
353364
354365 void handleGroupUnion (MethodCall call) {
@@ -357,10 +368,9 @@ class MixpanelFlutterPlugin {
357368 dynamic groupID = args['groupID' ];
358369
359370 String name = args['name' ] as String ;
360- dynamic value = args['value' ] as dynamic ;
361- get_group (groupKey,
362- (groupID is Map || groupID is List ) ? groupID.jsify () : groupID)
363- .union (name, value.jsify ());
371+ JSAny ? value = safeJsify (args['value' ] as dynamic );
372+ get_group (groupKey, safeJsify (groupID))
373+ .union (name, value is JSArray ? value : < JSAny > [].toJS);
364374 }
365375
366376 bool handleHasOptedOutTracking () {
0 commit comments