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.
Summary
Functions
The five metrics of one row as display strings.
Money to 2 decimals, rounded half up.
Parse the CSV text into campaign rows (file order).
(num_n / num_d) / den * mult computed exactly with integers, rounded half up to decimals
places. Returns "n/a" when den is zero.
Printable table: one line per campaign plus a total line.
Sum of all rows as a total row named Total.
Types
@type row() :: %{ campaign: String.t(), impressions: non_neg_integer(), clicks: non_neg_integer(), spend: spend(), leads: non_neg_integer() }
@type spend() :: {non_neg_integer(), pos_integer()}
Functions
@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 to 2 decimals, rounded half up.
Parse the CSV text into campaign rows (file order).
@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.
Printable table: one line per campaign plus a total line.
Sum of all rows as a total row named Total.