Group API in Mobile SDKs

Learn about the group API call in the Android (Kotlin) and iOS (Swift) SDKs.

This guide explains how to use the group API in the Android (Kotlin) and iOS (Swift) SDKs.

Overview

The RudderStack Android (Kotlin) and iOS (Swift) SDKs provide a group API that lets you link an identified user with a group like a company, organization, or an account. You can also record any custom traits associated with that group like the company name, number of employees, etc.

You can link an identified user to multiple groups.

Android (Kotlin)

The group method definition in the Android (Kotlin) SDK is as follows:

kotlin
analytics.group(
    groupId = "<groupId>",
    traits = buildJsonObject {
        put("key", "value")
    },
    options = RudderOption(),
)

The corresponding Java snippet is shown below:

java
HashMap<String, Object> traits = new HashMap<>();
traits.put("key", "value");

analytics.group("<groupId>", traits, new RudderOption());

Method signature

The below table describes the group method signature in detail:

FieldData typeDescription
groupIdStringThe group’s unique identifier in your database.
traitsTraitsContains traits associated with the group. They are added in the root level of the event.

Note that:

  • RudderStack does not store any group traits.
  • The traits type in Java is Map<String, Object>.
optionsRudderOptionAdditional event options.

Example

A sample group event sent from the Android (Kotlin) SDK is shown below:

kotlin
analytics.group(
    groupId = "5e8a78ba9d32d3b1898a6247",
    traits = buildJsonObject {
        put("name", "RudderStack")
    },
    options = RudderOption(
        customContext = buildJsonObject {
            put("plan", "Basic")
        },
        integrations = buildJsonObject {
            put("Amplitude", true)
            put("INTERCOM", buildJsonObject {
                put("lookup", "phone")
            })
        },
        externalIds = listOf(
            ExternalId(type = "brazeExternalId", id = "value1234")
        )
    )
)

iOS (Swift)

The group method definition in the iOS (Swift) SDK is as follows:

swift
analytics.group(
    groupId: "<groupId>",
    traits: [
        "key": "value"
    ],
    options: RudderOption()
)
Make sure to include the import RudderStackAnalytics statement before making the call.

The corresponding Objective-C snippet is shown below:

objectivec
[analytics group:@"<groupId>" traits:@{@"key": @"value"} options:[[RSSOptionBuilder new] build]];
RSSOptionBuilder is an Objective-C–only helper class. It uses the builder pattern to create an RSSOption instance and lets you set custom context, configure integrations, and attach external IDs in a structured way.

Method signature

The below table describes the group method signature in detail:

FieldData typeDescription
groupIdStringThe group’s unique identifier in your database.
traitsTraitsContains traits associated with the group. They are added in the root level of the event.
optionsRudderOptionAdditional event options.

Example

A sample group event sent from the iOS (Swift) SDK is shown below:

swift
analytics.group(
    groupId: "some_group_id",
    traits: [
        "key-1": "value-1"
    ],
    options: RudderOption(
        integrations: [
            "Amplitude": true,
            "INTERCOM": [
                "lookup": "phone"
            ]
        ],
        customContext: [
            "key-1": "value-1"
        ],
        externalIds: [
            ExternalId(type: "brazeExternalId", id: "value1234")
        ]
    )
)

Questions? Let's figure it out together.

Join the RudderStack Slack community to connect with other users, customers, and the RudderStack team — or reach out for direct support.