# Variation Distribution

When running an A/B Test, you need to distribute users across Variations and implement the logic for each Variation.\
You can perform this user distribution through the Hackle SDK.

## variation

{% hint style="info" %}
When using the Python SDK, duplicate exposure events for the same A/B Test Variation Distribution result generated by the same user within 1 minute are deduplicated.

Events within 1 minute are counted as one.
{% endhint %}

{% hint style="warning" %}
Server SDKs do not support cohort targeting. If targeting includes a cohort, run the experiment using a Client SDK.
{% endhint %}

By passing an **Experiment Key** and **User Identifier** to the `variation()` method, you can distribute the user and receive the result.

| Parameter            | Type  | Required | Constraints |
| -------------------- | ----- | -------- | ----------- |
| Experiment Key (key) | `int` | Required | `-`         |

#### Example

In the example code below, Experiment Key 42 is passed and two Variations, A and B, exist.

```python
from hackle.model import HackleUser

# 실험 키가 42인 A/B 테스트에서
# "ae2182e0"라는 식별자를 가진 사용자에게 노출할 테스트 그룹을 결정합니다.
# 결정하지 못하는 상황인 경우 테스트 그룹 A를 반환합니다.

user = HackleUser.builder() \
                 .device_id('ae2182e0') \
                 .build()

variation = hackle_client.variation(experiment_key=42, user=user)

# 할당받은 그룹에 대한 로직
if variation == 'A':
    # 그룹 A 로직
elif variation == 'B':
    # 그룹 B 로직
```

## variation\_detail

The `variation_detail()` method works the same as the `variation()` method and additionally provides the reason for distribution.\
This method is useful for checking whether distribution is working correctly.

| Parameter            | Type  | Required | Constraints |
| -------------------- | ----- | -------- | ----------- |
| Experiment Key (key) | `int` | Required | `-`         |

#### Example

You must pass the Experiment Key as a parameter. In the example code below, Experiment Key 42 is passed.

```python
# 분배 결정 상세
decision = hackle_client.variation_detail(experiment_key=42, user=user)

# 분배 그룹
variation = decision.variation

# 분배 결정 사유
reason = decision.reason
```

### Distribution Reason

The distribution decision reason is returned in the form of **`SDK_NOT_READY`**. Refer to the table below for details.

<table><thead><tr><th width="319.12109375">Distribution Reason</th><th width="305.35546875">Description</th><th>Distribution Result</th></tr></thead><tbody><tr><td><code>SDK_NOT_READY</code></td><td><p>The SDK is not ready to use.</p><p>(e.g., initialization attempted with an invalid SDK Key)</p></td><td>A (Control Group)</td></tr><tr><td><code>EXPERIMENT_NOT_FOUND</code></td><td>No A/B Test was found for the provided Experiment Key. The Experiment Key may be incorrect, or the experiment may be archived.</td><td>A (Control Group)</td></tr><tr><td><code>NOT_IN_MUTUAL_EXCLUSION_EXPERIMENT</code></td><td>The experiment is included in Mutually Exclusive Settings, but<br>the user was not assigned to that Mutually Exclusive Group.</td><td>A (Control Group)</td></tr><tr><td><code>EXPERIMENT_DRAFT</code></td><td>The A/B Test is in draft state.</td><td>A (Control Group)</td></tr><tr><td><code>EXPERIMENT_PAUSED</code></td><td>The A/B Test is paused.</td><td>A (Control Group)</td></tr><tr><td><code>EXPERIMENT_COMPLETED</code></td><td>The A/B Test has ended.</td><td>Winning Variation selected at completion</td></tr><tr><td><code>OVERRIDDEN</code></td><td>The user was assigned to a specific group<br>by Override.</td><td>Override-assigned<br>group</td></tr><tr><td><code>NOT_IN_EXPERIMENT_TARGET</code></td><td>The user is not an A/B Test target.</td><td>A (Control Group)</td></tr><tr><td><code>TRAFFIC_NOT_ALLOCATED</code></td><td>The A/B Test is running, but<br>the user was not allocated to the test.</td><td>A (Control Group)</td></tr><tr><td><code>TRAFFIC_ALLOCATED</code></td><td>The user has been allocated to the A/B Test.</td><td>Allocated group</td></tr><tr><td><code>VARIATION_DROPPED</code></td><td>The originally allocated group has been removed from the test.</td><td>A (Control Group)</td></tr><tr><td><code>INVALID_INPUT</code></td><td>The input value is invalid.</td><td>A (Control Group)</td></tr><tr><td><code>EXCEPTION</code></td><td>An unknown error occurred.</td><td>A (Control Group)</td></tr></tbody></table>

### Parameter Settings

* You can also receive the parameter values of the distributed Variation through the `variation_detail()` method.
* You can use the config object and `get()` method to retrieve and use the Parameter Settings values configured on the A/B Test screen. If you change a value on the A/B Test parameter settings screen, the updated value is applied to your code.
* The `parameterKey` of the `get()` method is the key configured in the A/B Test parameter settings, and `defaultValue` is the value returned when the distribution decision fails or an invalid parameter type value is provided.
* To receive the configured information correctly, you must enter a value of the type that matches the parameter type you configured in `defaultValue`.

| Method | Value Type                    | Description                                                                                                                                               |
| ------ | ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `get`  | `string`, `number`, `boolean` | \* Returns the configured parameter value. \* JSON type can be received as a string. \* The default value for JSON type must be entered as a string type. |

#### Example

```python
from hackle.model import HackleUser

user = HackleUser.builder() \
                 .device_id('ae2182e0') \
                 .build()

decision = hackleClient.variation_detail(experiment_key=42, user=user)

# 분배 결정 상세에서 get() 메소드를 통해 parameter 값 가져오기
parameterValue = decision.get('parameterKey', 'defaultValue')

# string 유형의 parameter값 예제
strValue = decision.get('parmeterKey', 'defaultValue')
```


---

# 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/en/development-guide/python/python-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.
