diff --git a/splitio_web/lib/splitio_web.dart b/splitio_web/lib/splitio_web.dart index d9e1aea..2fe20f8 100644 --- a/splitio_web/lib/splitio_web.dart +++ b/splitio_web/lib/splitio_web.dart @@ -565,4 +565,30 @@ class SplitioWeb extends SplitioPlatform { return jsTreatmentsWithConfigToMap(result); } + + Future track( + {required String matchingKey, + required String? bucketingKey, + required String eventType, + String? trafficType, + double? value, + Map properties = const {}}) async { + final client = await _getClient( + matchingKey: matchingKey, + bucketingKey: bucketingKey, + ); + + final result = client.track.callAsFunction( + null, + trafficType != null + ? trafficType.toJS + : this._trafficType != null + ? this._trafficType!.toJS + : null, + eventType.toJS, + value != null ? value.toJS : null, + _convertMap(properties, false)) as JSBoolean; + + return result.toDart; + } } diff --git a/splitio_web/lib/src/js_interop.dart b/splitio_web/lib/src/js_interop.dart index 6ec78e3..a8838b4 100644 --- a/splitio_web/lib/src/js_interop.dart +++ b/splitio_web/lib/src/js_interop.dart @@ -23,6 +23,7 @@ extension type JS_IBrowserClient._(JSObject _) implements JSObject { external JSFunction getTreatmentsByFlagSets; external JSFunction getTreatmentsWithConfigByFlagSet; external JSFunction getTreatmentsWithConfigByFlagSets; + external JSFunction track; } @JS() diff --git a/splitio_web/test/splitio_web_test.dart b/splitio_web/test/splitio_web_test.dart index 8af0a9e..20ab0e1 100644 --- a/splitio_web/test/splitio_web_test.dart +++ b/splitio_web/test/splitio_web_test.dart @@ -128,6 +128,14 @@ void main() { result.setProperty('split2'.toJS, treatmentWithConfig); return result; }.toJS; + mockClient['track'] = + (JSAny? trafficType, JSAny? eventType, JSAny? value, JSAny? properties) { + calls.add(( + methodName: 'track', + methodArguments: [trafficType, eventType, value, properties] + )); + return trafficType != null ? true.toJS : false.toJS; + }.toJS; final mockLog = JSObject(); mockLog['warn'] = (JSAny? arg1) { @@ -513,6 +521,74 @@ void main() { }); }); + group('track', () { + test('track with traffic type, value & properties', () async { + final result = await _platform.track( + matchingKey: 'matching-key', + bucketingKey: 'bucketing-key', + eventType: 'my_event', + trafficType: 'my_traffic_type', + value: 25.10, + properties: { + 'propBool': true, + 'propString': 'value', + 'propInt': 1, + 'propDouble': 1.1, + 'propList': ['value1', 100, false], // not valid property value + 'propSet': {'value3', 100, true}, // not valid property value + 'propNull': null, // not valid property value + 'propMap': {'value5': true} // not valid property value + }); + + expect(result, true); + expect(calls.last.methodName, 'track'); + expect(calls.last.methodArguments.map(jsAnyToDart), [ + 'my_traffic_type', + 'my_event', + 25.10, + { + 'propBool': true, + 'propString': 'value', + 'propInt': 1, + 'propDouble': 1.1, + } + ]); + }); + + test('track with value, and no traffic type in config', () async { + final result = await _platform.track( + matchingKey: 'matching-key', + bucketingKey: 'bucketing-key', + eventType: 'my_event', + value: 25.20); + + expect(result, false); // false because no traffic type is provided + expect(calls.last.methodName, 'track'); + expect(calls.last.methodArguments.map(jsAnyToDart), + [null, 'my_event', 25.20, {}]); + }); + + test('track without value, and traffic type in config', () async { + SplitioWeb _platform = SplitioWeb(); + await _platform.init( + apiKey: 'api-key', + matchingKey: 'matching-key', + bucketingKey: null, + sdkConfiguration: + SplitConfiguration(trafficType: 'my_traffic_type_in_config')); + + final result = await _platform.track( + matchingKey: 'matching-key', + bucketingKey: 'bucketing-key', + eventType: 'my_event'); + + expect(result, true); + expect(calls.last.methodName, 'track'); + expect(calls.last.methodArguments.map(jsAnyToDart), + ['my_traffic_type_in_config', 'my_event', null, {}]); + }); + }); + group('initialization', () { test('init with matching key only', () async { SplitioWeb _platform = SplitioWeb();