Skip to main content

Using Custom Fields in Your Code

Custom fields let you add your own metadata to experiments. You define them in the Platform Settings on the Web Console, and they become available to the SDKs at runtime.

This is useful when you need per-experiment configuration that goes beyond what ABsmartly provides out of the box. For example, you might want each experiment to specify a "developer variant" so your engineering team can easily test specific variants in production without affecting experiment data.

info

This feature is currently supported by the following SDKs:

  • Javascript (and React, Vue3 which inherit from it)
  • Java
  • Go
  • .NET
  • Ruby
  • Swift

If your SDK is not listed here (Python, PHP, Flutter, Vue2), custom fields are not yet available. You can achieve similar per-experiment configuration by checking the experiment name directly in your code and maintaining a lookup table of per-experiment settings.

An Example

Let's say you wish to override an experiment's allocation to a specific variant if the user is a member of your development team.

Creating a Custom Field

Check out the docs on creating a custom field for more information on adding a field to your experiment creation form, but for this example, we'll create a field with the following properties:

  • Name: Developers' variant
  • Type: number
  • Help Text: The variant to allocate to developers when they are testing the app in production.
  • Default Value: 1
  • Field Required: true
  • Available?: true
  • Field Key: developer_variant
  • Section: Description
note

You may wish to create a custom section for this field.

Using the Custom Field

Now, when creating an experiment, you will see the custom field in the metadata section with a default value of 1. If your experiment has more than one variant, you could change it per experiment to be 2 or 3, or you could change it to 0, if you wish for developers to only see the control variant.

Now, in your app, you can pull the custom field value from the SDK context and use it to override the context.treatment() call. Here, we are assuming that the app's user object has an isDeveloper property.

const context = sdk.createContext({
units: { user_id: "user-12345" },
});
await context.ready();

const experimentNames = context.experiments();
for (const experimentName of experimentNames) {
const developerVariant = context.customFieldValue(experimentName, "developer_variant");

if (developerVariant != null && user.isDeveloper) {
context.override(experimentName, developerVariant);
}
}

This code gets the list of experiment names, gets the developer_variant custom field value for each experiment, and if the current user is a developer, it overrides the experiment allocation to the variant that was set in the Web Console. If the user is not a developer, the experiment will run as normal. The developer_variant can also be edited on the Web Console to change the variant that developers see.

caution

Overriding an experiment allocation will not count the user towards the experiment data. To learn more about overrides, have a look at the overrides documentation.

Next steps