# 사용자 식별자와 속성

{% hint style="info" %}
사용자 식별자 관리

사용자 식별자는 사용자를 고유하게 식별하는 목적으로 사용합니다. 사용자 식별자의 의미와 중요성, 선택하는 기준 등에 대해서는 [사용자 식별자 관리하기](/getting-started/user-identifier.md) 문서를 참고하시기 바랍니다.
{% endhint %}

## 사용자 식별자

### 핵클에서 제공하는 기본 식별자

JavaScript SDK는 디바이스의 식별자를 관리하는 기능을 포함하고 있습니다. 따라서 사용자 식별자를 별도로 전달하지 않아도 사용자를 자동으로 식별할 수 있습니다.

SDK에서 관리하는 식별자를 조회하는 방법은 다음과 같습니다.

```javascript
// 사용자 정보 가지고 오기
const user = hackleClient.getUser();

// 디바이스 식별자 가지고 오기
const deviceId = user.deviceId;

// 내부적으로 관리되는 세션 식별자 가져오기
const sessionId = hackleClient.getSessionId();
```

#### 디바이스 ID 수정

핵클에서 제공하는 디바이스 ID를 사용하지 않고 직접 디바이스 ID를 주입할 수 있습니다.

```javascript
// 디바이스 식별자 변경
hackleClient.setDeviceId("CUSTOM_DEVICE_ID");
```

#### 사용자 식별자(User ID) 설정

로그인 한 사용자의 식별자를 설정하실 수 있습니다.

```javascript
// 로그인 한 사용자 식별자 추가
hackleClient.setUserId("LOGIN_ID");
```

### 추가 식별자

기본 식별자(deviceid, userid) 외의 식별자 타입을 추가할 경우 아래와 같이 설정할 수 있습니다.

{% hint style="info" %}
추가 식별자는 [핵클 통합 식별자](/getting-started/user-identifier/hackle-id.md)로 통합되지 않습니다.
{% endhint %}

{% hint style="danger" %}
`setUser` 를 하는 경우 현재 디바이스의 유저 정보를 덮어씁니다.

* 현재 A userId를 사용중인데 setUser 시 A userId 를 전달하지 않으면 userId가 A -> null로 변경됩니다.
* 현재 custom deviceId를 사용중인데 setUser 시 사용중인 deviceId를 전달하지 않으면 custom deviceId -> hackle deviceId로 변경됩니다.
* 추가 식별자를 사용하는 경우에도 setUser 시 전달하지 않으면 추가 식별자가 초기화가 됩니다.
* 프로퍼티의 경우 아래 케이스로 디바이스 내 캐싱된 프로퍼티가 유지 or 초기화 될 수 있습니다.
  * setUser 전 / 후 userId와 deviceId가 동일하다면 캐시된 프로퍼티 유지됩니다.
  * setUser 전 / 후 userId 혹은 deviceId가 변경된다면 캐시된 프로퍼티가 삭제됩니다.
    {% endhint %}

```javascript
const user = {
  userId: "LOGIN_ID",           // 사용자 ID (핵클 통합 식별자 사용가능)
  deviceId: "CUSTOM_DEVICE_ID", // 디바이스 ID (핵클 통합 식별자 사용가능)
  identifiers: {
    myCustomId: "CUSTOM_IDENTIFIER" // Custom ID
  }
};

hackleClient.setUser(user);
```

## 사용자 속성(Property)

핵클 SDK는 사용자 속성을 추가할 수 있도록 지원합니다.

* 속성은 속성명(key)과 속성값(value)을 한 쌍으로 보내야 합니다.
* 추가 가능한 속성 개수는 최대 128개입니다.

<table><thead><tr><th width="133.04296875">구분</th><th width="127.60546875">타입</th><th>제약사항</th></tr></thead><tbody><tr><td>속성 명(key)</td><td><code>string</code></td><td><ul><li>글자수 제한은 128자입니다. (128 characters)</li><li>대소문자를 구분하지 않습니다.</li><li>예를 들어 AGE와 age는 동일한 속성명으로 인식합니다.</li></ul></td></tr><tr><td>속성 값(value)</td><td><code>boolean</code>, <code>string</code>, <code>number</code>, <code>array</code></td><td><ul><li>string 타입인 경우 글자수 제한은 1024자입니다.<br>(1024 characters)</li><li>string 타입은 대소문자를 구분합니다.</li><li>예를 들어 APPLE과 apple은 서로 다른 속성값으로 인식합니다.</li><li>number 타입인 경우 정수 최대 15자리, 소수점 최대 6자리를<br>지원합니다.</li></ul></td></tr></tbody></table>

### 사용자 속성 추가

사용자 속성을 간단하게 추가할 수 있습니다. 아래 함수를 호출 시 PropertyOperations 객체에 `set` 을 이용하여 속성을 추가하는 것과 동일하게 동작합니다.

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

```javascript
hackleClient.setUserProperty("gender", "female");
```

{% endtab %}

{% tab title="HTML" %}

```html
<script>
  hackleClient.setUserProperty("gender", "female");
</script>
```

{% endtab %}
{% endtabs %}

### 사용자 속성 설정

사용자 속성을 추가, 제거 할 수 있습니다.

<table><thead><tr><th width="150">지원하는 함수</th><th>설명</th></tr></thead><tbody><tr><td><code>set</code></td><td>사용자 속성을 설정합니다. 속성 키에 이미 설정한 속성값이 있는 경우 덮어씁니다</td></tr><tr><td><code>setOnce</code></td><td><p>사용자 속성 값을 한번만 설정합니다. 속성키에 대한 속성이 이미 있는 경우 무시됩니다.</p><p>예를 들어 사용자에 대한 가입일, 초기 가입 위치 등을 설정할 수 있습니다.</p></td></tr><tr><td><code>unset</code></td><td>사용자 속성을 제거합니다.</td></tr><tr><td><code>clearAll</code></td><td>사용자의 모든 속성을 제거합니다.</td></tr></tbody></table>

설정하고 싶은 사용자 속성으로 `PropertyOperationsBuilder` 객체를 인스턴스화 합니다. 다음 `updateUserProperties` 를 호출하여 사용자 속성을 업데이트 합니다. 한 번에 여러개의 속성을 설정할 수도 있습니다.

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

```javascript
import { PropertyOperationsBuilder } from "@hackler/javascript-sdk"

const operations = new PropertyOperationsBuilder()
    .set("age", 42)
    .set("grade", "GOLD")
    .setOnce("sign_up_date", "2020-07-03")
    .build();

hackleClient.updateUserProperties(operations);
```

{% endtab %}

{% tab title="HTML" %}

```html
<script>
  const operations = new Hackle.PropertyOperationsBuilder()
    .set("age", 42)
    .set("grade", "GOLD")
    .setOnce("sign_up_date", "2020-07-03")
    .build();

  hackleClient.updateUserProperties(operations);
</script>
```

{% endtab %}
{% endtabs %}

## 사용자 초기화

기존에 설정한 정보를 초기화해야 합니다. 초기화를 하는 경우 기존에 설정했던 식별자, 속성이 모두 초기화됩니다.

{% hint style="danger" %}
사용자 초기화를 하는 경우 서버에 저장된 사용자 속성까지 모두 초기화가 됩니다. 로그아웃 처리를 원하는 경우 `hackleClient.setUserId(undefined);`을 사용해주세요.

userId에 undefined을 대입하면 클라이언트 상에서 로그아웃 처리가 됩니다.
{% endhint %}

```javascript
hackleClient.resetUser();
```

`resetUser()` 를 호출하는 경우 기존에 설정했던 식별자, 속성이 모두 초기화됩니다.


---

# 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.hackle.io/development-guide/javascript/user-identifier.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.
