# 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 manage user data is immediately after the user logs in.

#### User ID Management

For optimal performance and data integrity:

* **Update at login:** Always update the user identifiers immediately after the user logs in.
* **Unique `userId`:** Ensure every user is assigned a unique `userId`. Reusing the same ID across different users will result in data inconsistencies and is not supported.

### SDK v4.4.0 Update: User Management Changes

As of version **v4.4.0**, the `updateUser` method has been **deprecated**. Use the following methods instead:

* `identifyUser`: Sets or updates identifiers such as `userId`, `email`, and `msisdn`.
* `updateUserProfile`: Updates user profile attributes like name, gender, birthday, occupation, etc.

### Updating User Information

#### Step 1: Identify the User

Use `identifyUser` after the user logs in:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val user = NMUser()
user.setUserId("the_greatest")
user.setMsisdn("001XXXXXXXXX")
user.setEmail("clay@champion.com")
Netmera.identifyUser(user)
```

{% endtab %}

{% tab title="Java" %}

```java
NMUser user = new NMUser();
user.setUserId("the_greatest");
user.setMsisdn("001XXXXXXXXX");
user.setEmail("clay@champion.com");
Netmera.identifyUser(user);
```

{% endtab %}
{% endtabs %}

#### Step 2: Update Profile Attributes

Use `updateUserProfile` to send user details:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val profile = NMUserProfile()
profile.name.set("Muhammad Ali")
profile.surname.set("Clay")
profile.externalSegments.set(listOf("sports", "box"))
profile.gender.set(Gender.MALE)
profile.maritalStatus.set(MaritalStatus.MARRIED)
profile.childCount.set(9)
profile.country.set("USA")
profile.state.set("Arizona")
profile.city.set("Scottsdale")
profile.district.set("Old Town Scottsdale")
profile.occupation.set("Professional Boxer")
profile.industry.set("Sports")
profile.favoriteTeam.set("My Team")
profile.language.set("English")
Netmera.updateUserProfile(profile)
```

{% endtab %}

{% tab title="Java" %}

```java
NMUserProfile profile = new NMUserProfile();
profile.getName().set("Muhammad Ali");
profile.getExternalSegments().set(Arrays.asList("sports", "box"));
profile.getGender().set(Gender.MALE);
profile.getMaritalStatus().set(MaritalStatus.MARRIED);
profile.getChildCount().set(9);
profile.getCountry().set("USA");
profile.getState().set("Arizona");
profile.getCity().set("Scottsdale");
profile.getDistrict().set("Old Town Scottsdale");
profile.getOccupation().set("Professional Boxer");
profile.getIndustry().set("Sports");
profile.getFavoriteTeam().set("My Team");
profile.getLanguage().set("English");
Netmera.updateUserProfile(profile);
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
**Important Notes**

* You should send a **single** user update request at a time.
* Do not use `updateUser`—it is deprecated and replaced by the two methods above.
  {% endhint %}

### Managing attributes with `set`, `add`, `remove`, and `unset`

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

| Operation  | Description                                             | Data Type                                |
| ---------- | ------------------------------------------------------- | ---------------------------------------- |
| `set()`    | Completely replaces the profile attribute               | Available for all data types             |
| `unset()`  | Completely deletes the profile attribute                | Available for all data types             |
| `add()`    | Adds new element(s) to the profile attribute            | Available only for Array-type attributes |
| `remove()` | Removes specified element(s) from the profile attribute | Available only for Array-type attributes |

{% 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

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val profile = NMUserProfile()
val segments = listOf("sports_fans", "premium_subscriber")
profile.externalSegments.set(segments)
Netmera.updateUserProfile(profile)
```

{% endtab %}

{% tab title="Java" %}

```java
NMUserProfile profile = new NMUserProfile();
List<String> segments = Arrays.asList("sports_fans", "premium_subscriber");
profile.getExternalSegments().set(segments);
Netmera.updateUserProfile(profile);
```

{% endtab %}
{% endtabs %}

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

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

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

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val profile = NMUserProfile()
val segments = listOf("black_friday_2025")
profile.externalSegments.add(segments)
Netmera.updateUserProfile(profile)
```

{% endtab %}

{% tab title="Java" %}

```java
NMUserProfile profile = new NMUserProfile();
List<String> segments = Arrays.asList("black_friday_2025");
profile.getExternalSegments().add(segments);
Netmera.updateUserProfile(profile);
```

{% endtab %}
{% endtabs %}

**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

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val profile = NMUserProfile()
val segments = listOf("sports_fans")
profile.externalSegments.remove(segments)
Netmera.updateUserProfile(profile)
```

{% endtab %}

{% tab title="Java" %}

```java
NMUserProfile profile = new NMUserProfile();
List<String> segments = Arrays.asList("sports_fans");
profile.getExternalSegments().remove(segments);
Netmera.updateUserProfile(profile);
```

{% endtab %}
{% endtabs %}

**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

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val profile = NMUserProfile()
profile.externalSegments.unset()
Netmera.updateUserProfile(profile)
```

{% endtab %}

{% tab title="Java" %}

```java
NMUserProfile profile = new NMUserProfile();
profile.getExternalSegments().unset();
Netmera.updateUserProfile(profile);
```

{% endtab %}
{% endtabs %}

**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.

{% hint style="info" %}

#### Important Note

To avoid update conflicts, **ensure that only one profile update request is sent at a time**.
{% endhint %}

To remove an attribute, set it to `null`:

{% tabs %}
{% tab title="Kotlin" %}

```tsx
val user = NMUser()
user.email = null
Netmera.identifyUser(user)
```

{% endtab %}

{% tab title="Java" %}

```java
NetmeraUser profile = new NMUser();
profile.setEmail(null);
Netmera.identifyUser(profile);
```

{% endtab %}
{% endtabs %}

### Removing Attributes With Unset

{% tabs %}
{% tab title="Kotlin" %}

```tsx
val profile = NMUserProfile()
profile.favoriteTeam.unset()
Netmera.updateUserProfile(profile)
```

{% endtab %}

{% tab title="Java" %}

```java
NMUserProfile profile = new NMUserProfile();
profile.getFavoriteTeam().unset();
Netmera.updateUserProfile(profile);
```

{% endtab %}
{% endtabs %}

### 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 KVKK compliance:

* Mark profile attributes as **Private Information** in the panel if they are sensitive.
* If flagged, the SDK will **not** send these fields to the backend during `identifyUser` or `updateUserProfile` calls.
  {% endhint %}

<figure><img src="https://2578508252-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0bOAscrXzPSujyzq8DEz%2Fuploads%2FhdG2lVSfEWWdSab1o1MO%2FScreenshot%202025-02-06%20at%2016.36.38.png?alt=media&#x26;token=cffd0f0a-f89b-49e7-8911-d50640093b3b" alt=""><figcaption></figcaption></figure>

### Fetching Coupons

Retrieve and display user-earned coupons:

```tsx
Netmera.fetchCoupons(0, 10, object : NMFetchCouponsResultListener {
    override fun onSuccess(coupons: List<NMCouponDetail>?) {
        // Handle coupons
    }
    override fun onFailure(error: String?) {
        Toast.makeText(context, error ?: "No error message", Toast.LENGTH_SHORT).show()
    }
})
```

### User Update with Listener

To track update status:

```java
Netmera.updateUserProfile(profile, new NMUpdateUserProfileListener() {
    @Override
    public void onSuccess() {
        Log.i("NetmeraApp", "User updated successfully");
    }

    @Override
    public void onFailure(@Nullable String error) {
        Log.e("NetmeraApp", "Update failed: " + error);
    }
});

```

### 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:

```swift
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**

```swift
Netmera.setEmailPermission(true)
```

2. **Disallow Email Subscriptions**

```swift
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:

```swift
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**

```swift
Netmera.setSmsPermission(true)
```

2. **Disallow SMS Subscriptions**

```swift
Netmera.setSmsPermission(false)
```

These methods enable you to respect user preferences for email 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.

#### Update WhatsApp Subscription Preference

```swift
// Set WhatsApp permission
Netmera.setWhatsAppPermission(true)
Netmera.setWhatsAppPermission(false)
```

#### Get WhatsApp Subscription Status

```swift
// Get WhatsApp permission
Netmera.getWhatsAppPermission(object : NMPermissionResultListener {

    override fun onSuccess(isAllowed: Boolean) {
        // Handle current WhatsApp permission state
    }

    override fun onFailure(error: String?) {
        // Handle error
    }
})
```
