putExternalId method

RudderOption putExternalId(
  1. String type,
  2. String id
)

Adds or updates an external ID for linking this event to external systems.

External IDs are used to connect events to users in external systems like Braze. If an external ID with the same type already exists, it will be updated.

type - The type/name of the external system (e.g., 'braze'). id - The user ID in the external system. Returns this RudderOption instance for method chaining.

Implementation

RudderOption putExternalId(String type, String id) {
  externalIds ??= [];

  Map<String, String>? externalIdMap;
  int mapIndex = -1;
  for (int index = 0; index < externalIds!.length; index++) {
    Map<String, String> map = externalIds!.elementAt(index);
    String mapType = map["type"].toString();
    if (Utils.equalsIgnoreCase(mapType, type)) {
      externalIdMap = map;
      mapIndex = index;
      break;
    }
  }

  // if not present from previous runs: create new and assign the type
  if (externalIdMap == null) {
    externalIdMap = {};
    externalIdMap["type"] = type;
  }

  // assign new id or update existing id
  externalIdMap["id"] = id;

  // finally update existing position or add new id
  if (mapIndex == -1) {
    // not found in existing storage
    externalIds!.add(externalIdMap);
  } else {
    externalIds!.elementAt(mapIndex)["id"] = id;
  }

  // return for builder pattern
  return this;
}