LinkedinAdMetrics (linkedin_ad_metrics v1.0.0)

Copy Markdown View Source

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

row()

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

spend()

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

Functions

metrics(map)

@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(arg)

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

Money to 2 decimals, rounded half up.

parse(text)

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

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

ratio(num_n, num_d, den, mult, decimals)

@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(rows)

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

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

total(rows)

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

Sum of all rows as a total row named Total.