@@ -5,6 +5,24 @@ import 'package:flutter/services.dart';
5
5
import 'package:flutter_web_plugins/flutter_web_plugins.dart' ;
6
6
import 'package:mixpanel_flutter/web/mixpanel_js_bindings.dart' ;
7
7
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
+
8
26
/// A web implementation of the MixpanelFlutter plugin.
9
27
class MixpanelFlutterPlugin {
10
28
static Map <String , String > _mixpanelProperties = {
@@ -138,13 +156,13 @@ class MixpanelFlutterPlugin {
138
156
Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
139
157
String token = args['token' ] as String ;
140
158
dynamic config = args['config' ];
141
- init (token, config ? . jsify ( ) ?? {} );
159
+ init (token, safeJsify (config ) ?? < String , dynamic > {}. jsify () );
142
160
}
143
161
144
162
void handleSetServerURL (MethodCall call) {
145
163
Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
146
164
String serverURL = args['serverURL' ] as String ;
147
- set_config ({'api_host' : serverURL}. jsify ( ));
165
+ set_config (safeJsify ( {'api_host' : serverURL}));
148
166
}
149
167
150
168
void handleTrack (MethodCall call) {
@@ -155,7 +173,7 @@ class MixpanelFlutterPlugin {
155
173
..._mixpanelProperties,
156
174
...(properties ?? {})
157
175
};
158
- track (eventName, props. jsify ( ));
176
+ track (eventName, safeJsify (props ));
159
177
}
160
178
161
179
void handleAlias (MethodCall call) {
@@ -180,16 +198,15 @@ class MixpanelFlutterPlugin {
180
198
...(properties ?? {})
181
199
};
182
200
dynamic groups = args["groups" ];
183
- track_with_groups (eventName, props. jsify ( ), groups. jsify ( ));
201
+ track_with_groups (eventName, safeJsify (props ), safeJsify (groups ));
184
202
}
185
203
186
204
void handleSetGroup (MethodCall call) {
187
205
Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
188
206
String groupKey = args['groupKey' ] as String ;
189
207
dynamic groupID = args["groupID" ];
190
208
if (groupID != null ) {
191
- set_group (groupKey,
192
- (groupID is Map || groupID is List ) ? groupID.jsify () : groupID);
209
+ set_group (groupKey, safeJsify (groupID));
193
210
}
194
211
}
195
212
@@ -199,8 +216,7 @@ class MixpanelFlutterPlugin {
199
216
dynamic groupID = args["groupID" ];
200
217
201
218
if (groupID != null ) {
202
- add_group (groupKey,
203
- (groupID is Map || groupID is List ) ? groupID.jsify () : groupID);
219
+ add_group (groupKey, safeJsify (groupID));
204
220
}
205
221
}
206
222
@@ -209,21 +225,20 @@ class MixpanelFlutterPlugin {
209
225
String groupKey = args['groupKey' ] as String ;
210
226
dynamic groupID = args["groupID" ];
211
227
if (groupID != null ) {
212
- remove_group (groupKey,
213
- (groupID is Map || groupID is List ) ? groupID.jsify () : groupID);
228
+ remove_group (groupKey, safeJsify (groupID));
214
229
}
215
230
}
216
231
217
232
void handleRegisterSuperProperties (MethodCall call) {
218
233
Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
219
234
dynamic properties = args['properties' ];
220
- register (properties. jsify ( ));
235
+ register (safeJsify (properties ));
221
236
}
222
237
223
238
void handleRegisterSuperPropertiesOnce (MethodCall call) {
224
239
Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
225
240
dynamic properties = args['properties' ];
226
- register_once (properties. jsify ( ));
241
+ register_once (safeJsify (properties ));
227
242
}
228
243
229
244
void handleUnregisterSuperProperty (MethodCall call) {
@@ -250,51 +265,51 @@ class MixpanelFlutterPlugin {
250
265
Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
251
266
dynamic properties = args['properties' ];
252
267
Map <String , dynamic > props = {..._mixpanelProperties, ...properties};
253
- people_set (props. jsify ( ));
268
+ people_set (safeJsify (props ));
254
269
}
255
270
256
271
void handleSetOnce (MethodCall call) {
257
272
Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
258
273
dynamic properties = args['properties' ];
259
274
Map <String , dynamic > props = {..._mixpanelProperties, ...properties};
260
- people_set_once (props. jsify ( ));
275
+ people_set_once (safeJsify (props ));
261
276
}
262
277
263
278
void handlePeopleIncrement (MethodCall call) {
264
279
Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
265
280
dynamic properties = args['properties' ];
266
- people_increment (properties. jsify ( ));
281
+ people_increment (safeJsify (properties ));
267
282
}
268
283
269
284
void handlePeopleAppend (MethodCall call) {
270
285
Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
271
286
dynamic properties = args['properties' ];
272
- people_append (properties. jsify ( ));
287
+ people_append (safeJsify (properties ));
273
288
}
274
289
275
290
void handlePeopleUnion (MethodCall call) {
276
291
Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
277
292
dynamic properties = args['properties' ];
278
- people_union (properties. jsify ( ));
293
+ people_union (safeJsify (properties ));
279
294
}
280
295
281
296
void handlePeopleRemove (MethodCall call) {
282
297
Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
283
298
dynamic properties = args['properties' ];
284
- people_remove (properties. jsify ( ));
299
+ people_remove (safeJsify (properties ));
285
300
}
286
301
287
302
void handlePeopleUnset (MethodCall call) {
288
303
Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
289
304
dynamic properties = args['properties' ];
290
- people_unset (properties. jsify ( ));
305
+ people_unset (safeJsify (properties ));
291
306
}
292
307
293
308
void handleTrackCharge (MethodCall call) {
294
309
Map <Object ?, Object ?> args = call.arguments as Map <Object ?, Object ?>;
295
310
dynamic properties = args['properties' ];
296
311
double amount = args['amount' ] as double ;
297
- people_track_charge (amount, properties ? . jsify ( ) ?? {} );
312
+ people_track_charge (amount, safeJsify (properties ) ?? < String , dynamic > {}. jsify () );
298
313
}
299
314
300
315
void handleClearCharge () {
@@ -311,9 +326,8 @@ class MixpanelFlutterPlugin {
311
326
dynamic groupID = args['groupID' ];
312
327
313
328
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));
317
331
}
318
332
319
333
void handleGroupSetPropertyOnce (MethodCall call) {
@@ -323,8 +337,7 @@ class MixpanelFlutterPlugin {
323
337
324
338
dynamic properties = args['properties' ];
325
339
326
- get_group (groupKey,
327
- (groupID is Map || groupID is List ) ? groupID.jsify () : groupID)
340
+ get_group (groupKey, safeJsify (groupID))
328
341
.set_once (properties.keys.first, properties[properties.keys.first]);
329
342
}
330
343
@@ -334,8 +347,7 @@ class MixpanelFlutterPlugin {
334
347
dynamic groupID = args['groupID' ];
335
348
336
349
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))
339
351
.unset (propertyName);
340
352
}
341
353
@@ -346,9 +358,8 @@ class MixpanelFlutterPlugin {
346
358
347
359
String name = args['name' ] as String ;
348
360
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));
352
363
}
353
364
354
365
void handleGroupUnion (MethodCall call) {
@@ -357,10 +368,9 @@ class MixpanelFlutterPlugin {
357
368
dynamic groupID = args['groupID' ];
358
369
359
370
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);
364
374
}
365
375
366
376
bool handleHasOptedOutTracking () {
0 commit comments