Skip to main content

User Attributes

User attributes are typed profile fields your organization defines in the Allegro admin — things like a newsletter opt-in, a favorite topic, or any preference you want a member to control. This guide covers reading and writing them from the browser with the SDK, which is how you'd build a preference center on your site.

Attributes are defined by an operator first. See the User Attributes product guide for how to create them and what the types mean.

Requires a signed-in member

These methods act on the currently authenticated member. Call them after the member has signed in — an unauthenticated visitor has no profile to read or write.

Reading a member's attributes

allegro.member.getUserAttributes() returns every defined attribute merged with the current member's value, so you can render a whole preference form from one call:

window.allegro.push(async function (allegro) {
const attributes = await allegro.member.getUserAttributes();

attributes.forEach(function (attribute) {
console.log(attribute.slug, attribute.value);
});
});

Each entry has the attribute's definition plus the member's current value:

FieldTypeDescription
slugstringThe attribute key.
namestringThe human-readable label.
descriptionstring | nullThe optional description.
typestringstring, number, boolean, or enum.
optionsstring[] | nullThe allowed choices, when the type is enum.
valueunknownThe member's current value, or null when none is set.

Every defined attribute appears in the list, whether or not the member has set a value for it.

Setting a value

allegro.member.setUserAttribute(slug, value) sets the value for the current member and resolves with the stored value:

window.allegro.push(async function (allegro) {
await allegro.member.setUserAttribute('wants-newsletter', true);
});

The value must match the attribute's type — a boolean for a boolean attribute, one of the defined options for an enum attribute, and so on. A value that fails validation, or a slug that isn't a defined attribute, is rejected.

Clearing a value

allegro.member.deleteUserAttribute(slug) removes the member's value for an attribute:

window.allegro.push(async function (allegro) {
await allegro.member.deleteUserAttribute('wants-newsletter');
});

Keeping the session token current

Setting or clearing a value changes the member's session token — attribute values are carried in its user_attributes claim. The SDK stores the freshly-issued token automatically after each write, so a subsequent allegro.member.getJwt() returns a token that reflects the change.