# Tracking Users

{% hint style="warning" %}
**Must Read**

We recommend that you get yourself acquainted with all the concepts related to [*Customers*](https://docs.antsomi.com/developers-guide/overview/customers-and-visitors) and [*Events*](https://docs.antsomi.com/developers-guide/overview/events-and-business-objects-bos) before proceeding. Doing so will help you understand the workings of this section, better.
{% endhint %}

## Identifying Users

At Antsomi, we begin tracking users as soon as you integrate our SDKs into your platform. When a user opens your mobile app, our Antsomi SDK automatically assigns a unique ID known as a UID (User ID) to them. This helps us record users in our backend system and create an anonymous profile for them. All their behavioral data and session data (including System Events, Custom Events, System User Attributes, and Custom User Attributes) are stored under this anonymous profile.

You have the option to establish a connection between a user and a customer by utilizing a unique ID known as a CID (Customer ID). If there is no existing customer associated with the CID, this action will also result in the creation of a new customer, effectively linking the user to this newly created customer profile. This process ensures that both new and existing customers are seamlessly integrated into your system. We recommend assigning a CID at any of these key moments in the user's journey:

1. When a user signs up for your platform.
2. When a user logs in to their account.
3. When a user's identity becomes known, such as during specific screen views or interactions.

Assigning a CID to a user has several important implications:

* It signifies that the user is now identified and recognized as a "Customer" in your Antsomi's CDP365 dashboard.
* It initiates the creation of a new "Customer Profile" that contains all of the user's data (in case there aren't any Customer Profile associated with that CID in Antsomi yet).
* Importantly, all previous anonymous profiles associated with the user are merged into this new *Customer Profile*. This means that data from the user's first visit to your website to their most recent interactions can all be found in one user profile. This consolidation ensures a comprehensive view of the user's interactions with your platform.

{% hint style="info" %}
**How Customer Profiles are Merged When User is Identified (assigned a CID)**

Let's assume that *User A* opens your mobile app a few times before signing up.

**User A opens your mobile app on Device 1:** Antsomi assigns them a UID and automatically creates an anonymous user profile containing all their data *(Anonymous Profile 1).*

**User A re-opens your mobile app on Device 3:** Antsomi assigns them a UID and creates another anonymous profile to record all their data *(Anonymous Profile 2).*

**User A re-opens your mobile app on Device 7 and creates an account:** On account creation, you can choose to assign the user a CID. This will lead to the creation of a new user profile.

* As soon as the *Customer Profile* is created, Antsomi will run a quick check in our backend to identify all the existing anonymous user profiles of that customer that were created on their previous visits.
* In this case, *Anonymous Profile 1* and *Anonymous Profile 2* will be merged with the final profile of *Customer A* to provide a unified view of their preferences and behavioral history.
  {% endhint %}

### Guidelines

Here are a few things to keep in mind when assigning a customer ID (CID) to identify your users:

* A customer ID cannot be changed once it has been assigned.
* While the unique customer ID (CID) can be any `string` that effectively distinguishes users in your system, we strongly advise utilizing hashing methods, such as MD5 or SHA256, with user-specific information like email addresses or phone numbers as input. This approach enhances security and ensures the uniqueness of the generated IDs.

### On Login

You can allocate a customer identification (CID) by invoking the `setCustomerId()` setter method. Any attributes, events, or session data gathered before utilizing this API are initially linked to an automatically generated anonymous user.

* Upon the invocation of `setCustomerId()`, all the previously stored information becomes associated with the identified customer.
* It's essential to trigger `setCustomerId()` promptly upon the user's login to your application or whenever you are capable of recognizing the user.

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

```java
import com.antsomi.AntsomiSdk;

public class MainActivity {
    // ... other fields and methods
    private String customerEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        AntsomiSdk antsomiSdk = AntsomiSdk.getInstance();    
        
        Bundle customer = new Bundle();
        customer.putString("name", this.customerName);
        customer.putString("email", this.customerEmail);
        customer.putString("phone", this.customerPhone);
        
        AntsomiSdk antsomiSdk = AntsomiSdk.getInstance();        
        
        AntsomiTrackEvent event = new AntsomiTrackEvent("sign_in_user");
        event.setCustomerProperties(customer);
        
        antsomiSdk.setCustomerId(MD5(this.customerEmail));
        antsomiSdk.track(event);
    }
}
```

{% endtab %}
{% endtabs %}

### On Logout

Make sure you call `resetCustomer()` when the logged-in customer logs out, or you do not want to attach any future event, session, or user data with this customer until `setCustomerId()`  is called again.

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

```java
import com.antsomi.AntsomiSdk;

public class ProfileActivity {
    // ... other fields and methods

    private void handleLogout() {
        AntsomiSdk antsomiSdk = AntsomiSdk.getInstance();
        antsomiSdk.resetCustomer();
    }
}
```

{% endtab %}
{% endtabs %}

## Customer Attributes

Additional details such as a name, email address, location, and more can be linked to your customer profile. Antsomi offers a convenient, all-in-one method for setting values for these customer attributes. The `setCustomerProperties()` method accepts a `Bundle` as its parameter, where the key corresponds to the internal code of the *Customer Attribute* in Antsomi, and the value represents the data you intend to assign to it. This streamlined approach simplifies enriching your customer profiles with essential information.

For example, in the sign-in event, you can send more attributes beside `customer_id` and `name`, like the code snippet below

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

```java
import com.antsomi.AntsomiSdk;
import com.antsomi.AntsomiTrackEvent;

public class MainActivity {
    // ... other fields and methods
    private String customerName;
    private String customerEmail;
    private String customerPhone;
    private Date customerDob;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
        String dob = sdf.format(this.customerDob);
        
        Bundle customer = new Bundle();
        customer.putString("name", this.customerName);
        customer.putString("email", this.customerEmail);
        customer.putString("phone", this.customerPhone);
        customer.putString("date_of_birth", dob);
        
        AntsomiSdk antsomiSdk = AntsomiSdk.getInstance();        
        
        AntsomiTrackEvent event = new AntsomiTrackEvent("sign_in_user");
        event.setCustomerProperties(customer);
        
        antsomiSdk.setCustomerId(MD5(this.customerEmail));
        antsomiSdk.track(event);
    }
}
```

{% endtab %}
{% endtabs %}


---

# 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://docs.antsomi.com/developers-guide/android/tracking-users.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.
