User class

View as Markdown

The User class represents a user. An instance representing the current user is returned calling the user method of Krenalis. For example:

const userId = krenalis.user().id();

id

The id method is used to get and set the identifier of the user. It always returns the user's identifier, or null if there is no identifier.

To set the user's identifier, call the id method with an argument:

  • to remove the identifier, pass a null argument.
  • to change the identifier and resets the Anonymous ID, pass a non-empty String or a Number (the number will be converted to a String). If the passed identifier is the same of the current identifier, it does nothing.

Syntax

id(id)
id(id?: string | null): string | null

Parameters

Name Type Required Description
id String or Number User identifier to set. If it is null, the user's identifier is removed.

Examples

const userId = krenalis.user().id();
krenalis.user().id(null);
krenalis.user().id('509284521');

anonymousId

The anonymousId method is used to retrieve and modify the Anonymous ID. It consistently returns the Anonymous ID.

To modify the Anonymous ID, call the anonymous method with an argument:

  • to reset the Anonymous ID with a newly generated value, pass null.
  • to set the Anonymous ID with a specified value, pass a non-empty String or a Number (the number will be converted to a String).

Syntax

anonymousId(id)
anonymousId(id?: string | null): string

Parameters

Name Type Required Description
id String or Number Anonymous ID to set.

Examples

const anonymousId = krenalis.user().anonymousId();
krenalis.user().anonymousId(null);
krenalis.user().anonymousId('e2984831-431d-44ad-b1ec-4b901392fb67');

traits

The traits method is used to access and modify a user's traits. These traits are for the anonymous user if the user is anonymous, and for the non-anonymous user if non-anonymous. It consistently returns the user's traits.

To modify the user's traits, call the traits method with an argument:

  • To remove all traits, pass a null argument.
  • To update the traits, provide a non-null Object. Since traits are serialized with JSON.stringify, they must consist only of serializable values and should not contain cyclic references. In case of serialization errors, a warning will be logged in the console. The provided traits will completely replace the current traits of the user, if any.

Syntax

traits(traits)
traits(traits?: Record<string, unknown> | null): Record<string, unknown>

Parameters

Name Type Required Description
traits Object User's traits to set. null to remove all traits.

Examples

const traits = krenalis.user().traits();
krenalis.user().traits(null);
krenalis.user().traits({ firstName: 'Emily', lastName: 'Johnson' });