# list_split_assign

Elixir library and Mix task. Give it a CSV list of ids with a segment label and it splits the ids into groups (for example for an A/B test or to share a list between senders) with a seeded hash. The group of an id is `:erlang.phash2({seed, lowercased_id}, groups)`, mapped to the letters A, B, C and so on. The same id and seed always give the same group, whatever the row order or the case of the id.

A hash split is only approximately balanced: it does not count, so small lists can be far from even, and the more groups you ask for the more this shows. `:erlang.phash2/2` is the Erlang term hash. The group of each sample id is whatever the run prints; the tests pin the sample's output. The seed is set by you (default "seed"); change it to get a different split of the same list. The number of groups is set by you with `--groups` (an integer from 2 to 26, default 2).

The id is trimmed and compared case-insensitively, and a duplicate id is an error. The segment is a label (trimmed, case-insensitive, the first spelling is shown). The report gives the number of ids and groups, the seed, the ids and share (1 decimal) per group, and per segment (most ids first, then by name) the ids per group and the largest gap between the biggest and the smallest group share of that segment, in percentage points (1 decimal). With `--out FILE` it also writes a CSV with the columns id, segment, group in input order and says where. It exits 2 when the input cannot be read: a missing file, a missing column, no rows, an empty id or segment (the message names the row), a duplicate id, `--groups` outside 2 to 26 or an unknown option. It only assigns groups; it does not send anything or judge which group performs better.

Part of the [OneGTMLab](https://www.onegtmlab.dev) free GTM tools. Background on the idea: [Instantly vs Saleshandy](https://www.onegtmlab.dev/tool-directory/compare/instantly-vs-saleshandy).

## Install

```
{:list_split_assign, "~> 1.0"}   # in mix.exs deps
```

## Use

CSV columns: id, segment. Options: --groups (2 to 26, default 2), --seed (default "seed"), --out FILE.

```
$ mix list_split_assign sample.csv --groups 3 --seed spring
ids: 12
groups: 3
seed: spring
per group:
  group A: 4 ids (33.3%)
  group B: 4 ids (33.3%)
  group C: 4 ids (33.3%)
per segment (ids per group, gap between the biggest and smallest group share):
  Alpha: 6 ids | A 1, B 3, C 2 | largest gap 33.3 points
  Beta: 4 ids | A 3, B 0, C 1 | largest gap 75.0 points
  Gamma: 2 ids | A 0, B 1, C 1 | largest gap 50.0 points
```

## Test

```
mix test
```

Offline, no network calls, no dependencies beyond the language runtime. MIT licensed.
