# 테스트 그룹 분배

A/B 테스트를 진행할 때, 테스트 그룹을 대상으로 사용자를 분배하고 각 테스트 그룹에 해당하는 로직을 작성해야 합니다. 이 때 사용자 분배를 핵클 SDK를 통해 진행할 수 있습니다.

## variation

{% hint style="warning" %}
서버 SDK에서는 코호트 타겟팅을 지원하지 않습니다. 타겟팅에 코호트가 포함된 경우 클라이언트 SDK에서 테스트를 수행해주세요.
{% endhint %}

`variation()` 메소드에 **실험 키**와 **사용자 식별자**를 전달하면 사용자를 분배하고 결과를 전달받을 수 있습니다. 이후 테스트 그룹 별 로직을 구현합니다.

아래 예제 코드에서는 실험 키 42를 전달하고 있으며, 사용자 식별자는 "ae2182e0" 입니다. 테스트 그룹은 A와 B 두 개가 존재합니다.

```php
<?php
  
require 'vendor/autoload.php';

$client = \Hackle\HackleClients::create("YOUR_SDK_KEY");

$user = \Hackle\Common\HackleUser::builder()
    ->deviceId("ae2182e0")
    ->build();

$variation = $client->variation(42, $user);

if ($variation === "A") {
    //그룹 A 로직
} elseif ($variation === "B") {
    //그룹 B 로직;
}

?>
```

## variationDetail

`variationDetail()` 메소드는 `variation()` 메소드와 동일하게 동작하고 추가로 특정 그룹으로 분배된 사유를 같이 제공합니다. 이 메소드는 분배가 잘 되고 있는지 살펴볼 때 유용하게 사용할 수 있습니다.\
파라미터로 실험 키를 전달해야 합니다. 아래 예제 코드의 경우 실험 키 42를 전달하고 있습니다.

```php
<?php
  
require 'vendor/autoload.php';

$client = \Hackle\HackleClients::create("YOUR_SDK_KEY");

$user = \Hackle\Common\HackleUser::builder()
    ->deviceId("ae2182e0")
    ->build();

//분배 결정 상세
$decision = $client->variationDetail(42, $user);

//분배 그룹
$variation = $decision->getVariation();

//분배 결정 사유
$reason = $decision->getReason();

?>
```

분배 결정 사유는 **`SDK_NOT_READY`** 와 같은 형태로 받게 됩니다. 자세한 내용은 아래 표를 참고해주세요.

<table><thead><tr><th width="319.12109375">분배 사유</th><th width="305.35546875">설명</th><th>분배 결과</th></tr></thead><tbody><tr><td><code>SDK_NOT_READY</code></td><td><p>SDK 사용 준비가 되지 않았습니다.</p><p>(예: 잘못된 SDK 키로 초기화 시도)</p></td><td>A (기본 그룹)</td></tr><tr><td><code>EXPERIMENT_NOT_FOUND</code></td><td>전달한 실험 키에 대한 A/B 테스트를 찾을 수 없습니다. 실험 키가 잘못되었거나 해당 실험이 보관 상태일 수 있습니다.</td><td>A (기본 그룹)</td></tr><tr><td><code>NOT_IN_MUTUAL_EXCLUSION_EXPERIMENT</code></td><td>실험이 상호 배타적 설정에 포함되어 있지만<br>해당 상호 배타적 그룹에 할당되지 않은 경우</td><td>A (기본 그룹)</td></tr><tr><td><code>EXPERIMENT_DRAFT</code></td><td>A/B 테스트가 준비 상태입니다.</td><td>A (기본 그룹)</td></tr><tr><td><code>EXPERIMENT_PAUSED</code></td><td>A/B 테스트가 일시 정지 상태입니다.</td><td>A (기본 그룹)</td></tr><tr><td><code>EXPERIMENT_COMPLETED</code></td><td>A/B 테스트가 종료되었습니다.</td><td>종료 시 선택한승리 그룹</td></tr><tr><td><code>OVERRIDDEN</code></td><td>사용자가 수동할당에 의해<br>특정 그룹으로 결정되었습니다.</td><td>수동 할당한<br>그룹</td></tr><tr><td><code>NOT_IN_EXPERIMENT_TARGET</code></td><td>사용자가 A/B 테스트 타겟이 아닙니다.</td><td>A (기본 그룹)</td></tr><tr><td><code>TRAFFIC_NOT_ALLOCATED</code></td><td>A/B 테스트가 실행 중이지만<br>사용자가 테스트에 할당되지 않았습니다.</td><td>A (기본 그룹)</td></tr><tr><td><code>TRAFFIC_ALLOCATED</code></td><td>사용자가 A/B 테스트에 할당되었습니다.</td><td>할당된 그룹</td></tr><tr><td><code>VARIATION_DROPPED</code></td><td>원래 할당된 그룹이 테스트에서 제외되었습니다.</td><td>A (기본 그룹)</td></tr><tr><td><code>INVALID_INPUT</code></td><td>입력값이 유효하지 않습니다.</td><td>A (기본 그룹)</td></tr><tr><td><code>EXCEPTION</code></td><td>알 수 없는 오류가 발생했습니다.</td><td>A (기본 그룹)</td></tr></tbody></table>

## 파라미터

* `variationDetail()` 메소드를 통해 분배된 그룹의 파라미터 값도 같이 제공받을 수 있습니다.
* 핵클의 A/B 테스트 화면에서 설정한 파라미터 값이 key, value 형태로 존재하기 때문에, 설정한 파라미터 유형에 따라 아래 메소드를 사용하여 설정한 파라미터 값을 받아 활용할 수 있습니다.

### getString

* STRING, JSON 유형으로 설정된 파라미터 값을 반환합니다.
* 상태 결정에 따라 기본 값 혹은 규칙에 설정된 값을 반환합니다.

```php
<?php
  
require 'vendor/autoload.php';

$client = \Hackle\HackleClients::create("YOUR_SERVER_SDK_KEY");

$user = \Hackle\Common\HackleUser::builder()
    ->deviceId("ae2182e0")
    ->build();

$decision = $client->variationDetail(42, $user);

$strValue = $decision->getString("parameter_key_string_type", "defaultValue");
$jsonValue = $decision->getString("parameter_key_json_type", "defaultValue");
  
?>
```

### getInt

* Number 유형으로 설정된 parameter 값을 Int 타입으로 반환합니다.
* 상태 결정에 따라 기본 값 혹은 규칙에 설정된 값을 반환합니다.

```php
<?php
  
require 'vendor/autoload.php';

$client = \Hackle\HackleClients::create("YOUR_SERVER_SDK_KEY");

$user = \Hackle\Common\HackleUser::builder()
    ->deviceId("ae2182e0")
    ->build();

$decision = $client->variationDetail(42, $user);

$intValue = $decision->getInt("parameter_key_number_type", 0);

?>
```

### getFloat

* Number 유형으로 설정된 parameter 값을 float 타입으로 반환합니다.
* 상태 결정에 따라 기본 값 혹은 규칙에 설정된 값을 반환합니다.

```php
<?php
  
require 'vendor/autoload.php';

$client = \Hackle\HackleClients::create("YOUR_SERVER_SDK_KEY");

$user = \Hackle\Common\HackleUser::builder()
    ->deviceId("ae2182e0")
    ->build();

$decision = $client->variationDetail(42, $user);

$floatValue = $decision->getFloat("parameter_key_number_type", 0.0);

?>
```

### getBool

* Boolean 유형으로 설정된 parameter값을 반환합니다.
* 상태 결정에 따라 기본 값 혹은 규칙에 설정된 값을 반환합니다.

```php
<?php
  
require 'vendor/autoload.php';

$client = \Hackle\HackleClients::create("YOUR_SERVER_SDK_KEY");

$user = \Hackle\Common\HackleUser::builder()
    ->deviceId("ae2182e0")
    ->build();

$decision = $client->variationDetail(42, $user);

$boolValue = $decision->getBool("parameter_key_bool_type", true);

?>
```


---

# 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/php/php-variation.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.
