# `LinkedinAdMetrics`
[🔗](https://github.com/jaizendra-netizen/linkedin_ad_metrics/blob/main/lib/linkedin_ad_metrics.ex#L1)

Ad performance metrics from a CSV with the columns `campaign,impressions,clicks,spend,leads`.

`impressions`, `clicks` and `leads` are non-negative whole numbers; `spend` is a non-negative
number with an optional decimal part (no currency symbol, no thousands separator). Clicks may
not exceed impressions.

Per campaign and in total:

  * CTR % = clicks / impressions * 100
  * CPC = spend / clicks
  * CPM = spend / impressions * 1000
  * CPL = spend / leads
  * click-to-lead % = leads / clicks * 100

Any ratio with a zero denominator prints `n/a`. Money and percentages are shown to 2 decimals,
rounded half up. The arithmetic is exact (integers and exact decimal spend), so there is no
floating point drift.

# `row`

```elixir
@type row() :: %{
  campaign: String.t(),
  impressions: non_neg_integer(),
  clicks: non_neg_integer(),
  spend: spend(),
  leads: non_neg_integer()
}
```

# `spend`

```elixir
@type spend() :: {non_neg_integer(), pos_integer()}
```

# `metrics`

```elixir
@spec metrics(row()) :: %{
  ctr: String.t(),
  cpc: String.t(),
  cpm: String.t(),
  cpl: String.t(),
  click_to_lead: String.t()
}
```

The five metrics of one row as display strings.

# `money_string`

```elixir
@spec money_string(spend()) :: String.t()
```

Money to 2 decimals, rounded half up.

# `parse`

```elixir
@spec parse(String.t()) :: {:ok, [row()]} | {:error, String.t()}
```

Parse the CSV text into campaign rows (file order).

# `ratio`

```elixir
@spec ratio(integer(), pos_integer(), integer(), integer(), non_neg_integer()) ::
  String.t()
```

`(num_n / num_d) / den * mult` computed exactly with integers, rounded half up to `decimals`
places. Returns `"n/a"` when `den` is zero.

# `render`

```elixir
@spec render([row()]) :: String.t()
```

Printable table: one line per campaign plus a total line.

# `total`

```elixir
@spec total([row()]) :: row()
```

Sum of all rows as a total row named `Total`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
