# User & Attributes

## Managing User Data

The `NetmeraUser` class allows you to send structured information about your application's users to Netmera. The ideal time to update user attributes is **right after the user logs into your application.**

{% hint style="warning" %}
**User ID Management**

For optimal performance and data integrity:

* **Update user information at login**: It’s crucial to update the user information as soon as the user logs in.
* **Unique userId**: Ensure that each user has a unique `userId`. Assigning the same `userId` to multiple users leads to data inconsistencies and is not recommended.
  {% endhint %}

## Updating User Information <a href="#updating-user-information" id="updating-user-information"></a>

#### Step 1: Identify the User

Use `identifyUser` after the user logs in:

```typescript
const identifyUser = () => {
    const user = new NetmeraUser();
    user.userId = <userId>;
    user.email = <email>;
    user.msisdn = <msisdn>;
    user.wpNumber = <whatsappNumber>;

    // Identify user with callback
    Netmera.identifyUser(user, (success, error) => {
      if (success) {
        console.log("User identified successfully")
      } else {
        console.error(error?.message)
      }
    });

    // Identify user without callback
    Netmera.identifyUser(user);
}
```

#### Step 2: Update Profile Attributes

Use `updateUserProfile` to send user details:

```typescript
const sendUserProfileUpdate = () => {
  const userProfile = new NetmeraUserProfile();
  userProfile.name.set('John');
  userProfile.surname.set('Doe');
  userProfile.dateOfBirth.set(new Date().getTime());
  userProfile.gender.set(Gender.MALE);
  userProfile.externalSegments.set(['segment1', 'segment2']);

  // Update user profile with callback
  Netmera.updateUserProfile(userProfile, (success, error) => {
      if (success) {
          ...
        } else {
          ...
        }
  });

  // Update user profile without callback
  Netmera.updateUserProfile(userProfile);
};
```

{% hint style="warning" %}
You should send a **single** user update request at a time.
{% endhint %}

### Managing attributes with `set`, `add`, `remove`, and `unset` <a href="#managing-attributes-with-set-add-remove-and-unset" id="managing-attributes-with-set-add-remove-and-unset"></a>

The Netmera React Native SDK supports four core operations for each profile attribute:

<table><thead><tr><th width="125.3416748046875">Operation</th><th>Description</th><th>Data Type</th></tr></thead><tbody><tr><td><code>set()</code></td><td>Completely replaces the profile attribute</td><td>Available for all data types</td></tr><tr><td><code>unset()</code></td><td>Completely deletes the profile attribute</td><td>Available for all data types</td></tr><tr><td><code>add()</code> </td><td>Adds new element(s) to the profile attribute</td><td>Available only for Array-type attributes</td></tr><tr><td><code>remove()</code></td><td>Removes specified element(s) from the profile attribute</td><td>Available only for Array-type attributes</td></tr></tbody></table>

{% hint style="success" %}
For profile attributes defined as arrays, you do not need to manage the entire list at once. You can update them incrementally using `add()` and `remove()`.
{% endhint %}

In the following examples, we use `externalSegments`, which is a predefined array-type profile attribute in the Netmera platform. However, you are free to define and use your own custom profile attributes as needed.

### 1. `set()` – Create List from Scratch

```typescript
const profile = NetmeraUserProfile();
profile.externalSegments.set(["sports_fans","sports_fans"]);
Netmera.updateUserProfile(profile);
```

**Explanation:** This operation clears any existing values in the `externalSegments` field and assigns the new list containing `"sports_fans"` and `"sports_fans"`. Only the provided segments will be retained.

**Use case:** Reinitializing the user's profile with a new segment configuration.

### 2. `add()` – Add New Elements

```typescript
const profile = NetmeraUserProfile();
profile.externalSegments.add(["black_friday_2025"]);
Netmera.updateUserProfile(profile);
```

**Explanation:** Preserves existing values in the `externalSegments` list and appends the new segment `"black_friday_2025"`.

**Use case:** Temporarily adding a user to a campaign-specific segment.

### 3. `remove()` – Remove Elements

```typescript
const profile = NetmeraUserProfile();
profile.externalSegments.remove(["sports_fans"]);
Netmera.updateUserProfile(profile);
```

**Explanation:** Removes the `"sports_fans"` value from the user's `externalSegments` list. Other segment values remain unchanged.

**Use case:** Reflecting changes in user interests or preferences.

### 4. `unset()` – Clear All Data

```typescript
const profile = NetmeraUserProfile();
profile.externalSegments.unset();
Netmera.updateUserProfile(profile);
```

**Explanation:** Clears all data from the `externalSegments` field. The user will no longer be associated with any segment.

**Use case:** When a user account is closed or profile data needs to be reset.

### Creating Custom Profile Attributes&#x20;

#### Step 1: Navigate to the Developers section

Custom profile attributes must be created and configured in the Netmera Panel before they can be used in the application.&#x20;

* Go to **Panel** > **Developers > Profile Attributes > Create New Attribute.**

#### Step 2: Fill in Attribute Details

Define the following information:

* **Name**: The unique identifier for the attribute.
* **Label**: A user-friendly name for the attribute.
* **Description**: A brief description of the attribute.
* **Data Type**: Choose the appropriate data type for the attribute.
* **Is Array**: Define whether the attribute can hold multiple values.

#### Step 3: Save the Attribute

After clicking **Save**, the custom attribute will be available for assignment to your users.

#### Step 4: Generated Code for Custom Attributes

Once the attribute is defined in the Netmera Panel, the generated code can be found at the bottom of the **Profile Attribute** page, under the **Generate Code** section. This code must be added to your Netmera Panel under **Profile Attributes > User Class**.

{% hint style="info" %}
**Private Information Considerations**

Under the **KVKK** law, the sharing of private user data is prohibited. To ensure compliance:

* **Private Information Flag**: When defining profile attributes or events, the **Private Information** feature must be selected to ensure that these attributes are not sent to the backend by the Netmera SDK.
  {% endhint %}

<figure><img src="/files/QoRtHFanKtmtxa35m4Po" alt=""><figcaption></figcaption></figure>

### Fetch Coupon

Fetch coupons using the request/response structure and manager:

```typescript
Netmera.fetchCoupons(0, 10).then(_coupons => {
  console.log(_coupons);
}).catch(error => {
  console.log(error.code, error.message);
});
```

### Email Subscription Preferences

The Netmera SDK provides methods to manage email subscription preferences for your users.&#x20;

#### Check Email Subscription Status

Use the following method to check if the user has allowed email subscriptions:

```typescript
const emailAllowed = await Netmera.getEmailPermission();
```

* **Returns**: A `Boolean` value (`true` if the user has allowed email subscriptions, otherwise `false`).

#### Update Email Subscription Preferences

Set the user's preference for email subscriptions using one of the methods below:

1. **Allow Email Subscriptions**

```typescript
await Netmera.setEmailPermission(true);
```

2. **Disallow Email Subscriptions**

```typescript
await Netmera.setEmailPermission(false);
```

These methods enable you to respect user preferences for email communications within your application.

### SMS Subscription Preferences

The Netmera SDK provides methods to manage SMS subscription preferences for your users.&#x20;

#### Check SMS Subscription Status

Use the following method to check if the user has allowed SMS subscriptions:

```typescript
const smsAllowed = await Netmera.getSmsPermission();
```

**Returns**: A `Boolean` value (`true` if the user has allowed SMS subscriptions, otherwise `false`).

#### Update SMS Subscription Preferences

Set the user's preference for SMS subscriptions using one of the methods below:

1. **Allow SMS Subscriptions**

```typescript
await Netmera.setSmsPermission(true);
```

2. **Disallow SMS Subscriptions**

```typescript
await Netmera.setSmsPermission(false);
```

These methods enable you to respect user preferences for SMS communications within your application.

### WhatsApp Subscription Preferences

Use these APIs to check or update whether the user allows receiving WhatsApp messages. This permission controls eligibility for WhatsApp-based campaigns.

#### Check WhatsApp Subscription Status

Use the following method to check if the user has allowed WhatsApp subscriptions:

```typescript
const whatsAppAllowed = await Netmera.getWhatsAppPermission();
```

**Returns**: A `Boolean` value (`true` if the user has allowed WhatsApp subscriptions, otherwise `false`).

#### Update WhatsApp Subscription Preference

Set the user's preference for WhatsApp subscriptions using one of the methods below:

1. **Allow WhatsApp Subscriptions**

```typescript
await Netmera.setWhatsAppPermission(true);
```

2. **Disallow WhatsApp Subscriptions**

```typescript
await Netmera.setWhatsAppPermission(false);
```

These methods enable you to respect user preferences for WhatsApp communications within your application.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://user.netmera.com/netmera-developer-guide/platforms/react-native/user-and-attributes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
