Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions splitio_web/lib/splitio_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,30 @@ class SplitioWeb extends SplitioPlatform {

return jsTreatmentsWithConfigToMap(result);
}

Future<bool> track(
{required String matchingKey,
required String? bucketingKey,
required String eventType,
String? trafficType,
double? value,
Map<String, dynamic> 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;
}
}
1 change: 1 addition & 0 deletions splitio_web/lib/src/js_interop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extension type JS_IBrowserClient._(JSObject _) implements JSObject {
external JSFunction getTreatmentsByFlagSets;
external JSFunction getTreatmentsWithConfigByFlagSet;
external JSFunction getTreatmentsWithConfigByFlagSets;
external JSFunction track;
}

@JS()
Expand Down
76 changes: 76 additions & 0 deletions splitio_web/test/splitio_web_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down