Setting attributes with code

One way to send attributes to Formbricks is in your code. In Formbricks, there are two special attributes for user identification(user ID & email) and custom attributes. An example:

Setting during Initialization

It's recommended to set custom user attributes directly during the initialization of Formbricks for better user identification.

Set custom attributes during initialization

  environmentId: "<environment-id>",
  apiHost: "<api-host>",
  userId: "<user_id>",
  attributes: {
    plan: "free",

Setting independently

You can use the setAttribute function to set any custom attribute for the user (e.g. name, plan, etc.) anywhere in the user journey. Formbricks maintains a state of the current user inside the browser and makes sure attributes aren't sent to the backend twice.

Setting Plan to Pro

formbricks.setAttribute("Plan", "Pro");

Generally speaking, the setAttribute function works like this:

Setting Custom Attributes

formbricks.setAttribute("attribute_key", "attribute_value");

Where attributeName is the name of the attribute you want to set, and attributeValue is the value of the attribute you want to set.

Was this page helpful?