setGroup method

Future<void> setGroup(
  1. String groupType,
  2. dynamic groupName, [
  3. EventOptions? options
])

Adds a user to a group or groups. You need to specify a groupType and groupName(s).

For example you can group people by their organization. In this case, groupType is 'orgId', and groupName would be the actual ID(s). groupName can be a string or an array of strings to indicate a user in multiple groups.

amplitude.setGroup('orgId', '15');

You can also call setGroup multiple times with different groupTypes to track multiple types of groups (up to 5 per app). Note: This will also set groupType: groupName as a user property.

Implementation

Future<void> setGroup(String groupType, dynamic groupName,
    [EventOptions? options]) async {
  if (groupName is! String && groupName is! List<String>) {
    // TODO(xinyi): log warn that groupName should be either a string or an array of string.
    return;
  }

  final identify = Identify().set(groupType, groupName);
  final event = IdentifyEvent()
    ..groups = {groupType: groupName}
    ..userProperties = identify.properties;

  if (options != null) {
    event.mergeEventOptions(options);
  }

  return await _channel.invokeMethod('setGroup',
      {'instanceName': configuration.instanceName, 'event': event.toMap()});
}