Karta.Tracking (Karta v1.0.0)

Open and click tracking and one-click unsubscribe (RFC 8058). Each is turned on per server (track_opens, track_clicks, list_unsubscribe on Karta.Servers.Server, all off by default) and served from the tracking host (KartaWeb.TrackingEndpoint), never from the API host.

At delivery (Karta.Outbound.Deliverer), prepare/3 decides once per message what applies and stores the message's trackable links (Karta.Tracking.Link); personalise/3 then writes each recipient's copy before it is signed (Karta.Tracking.Rewriter): links become <base>/c/<token>, the pixel is <base>/o/<token>.gif, and List-Unsubscribe is <base>/u/<token> (tokens: Karta.Tracking.Token). <base> is the tracking host of the sender's domain, or of the server, when it has one (Karta.Tracking.Hosts.url_for/1), else the configured tracking URL. Nothing applies to incoming mail, to delivery status notifications (the null reverse path), or while there is no tracking URL. A message can switch open or click tracking off for itself (track_opens and track_clicks false on Karta.Messages.Message), never on.

On the tracking host, resolve/2 verifies a token before anything is looked up, then finds its recipient (and link) among accepted messages. record/5 stores an open or a click (Karta.Tracking.Event): the client's network rather than its address, the user agent shortened, at most events_per_minute per recipient (default 60; more are not recorded), and only while the server still tracks that kind. A click redirects to the URL stored for its link, whatever the request says. A recorded event is announced on the topics of its message and of its server (:tracking, Karta.Changes), and its webhook event (message.opened, message.clicked) recorded with it (Karta.Webhooks). unsubscribe/1 puts the recipient's address on the server's suppression list (reason :unsubscribe, source "list-unsubscribe"), once, with the webhook event message.unsubscribed.

Retention: links and events belong to their message and are deleted with it (Karta.Messages.Retention).

Configuration (config :karta, Karta.Tracking): base_url, the tracking host as https://host without a path (nil, the default, turns everything off), and events_per_minute.

Summary

Types

What personalisation does for a message, from plan/2.

What applies to a message's recipients, from prepare/3.

Functions

Whether a message is personalised for each recipient (plan/2 says how).

The configured tracking URL, or nil.

How many opens and clicks each recipient of a message has: %{recipient_id => %{open: n, click: n}}.

The opens and clicks of a recipient, oldest first, each with its link.

The opens and clicks of a message, oldest first, each with its link. Options: limit (default 100, at most 1000) and after (an event id: the list continues after it).

The message as sent to recipient, from what prepare/3 returned.

What personalisation would do for message (its server preloaded), or nil when nothing applies — nothing ever applies to a forward of received mail, sent from an SRS address. Option base_url replaces the message's tracking URL (the tracking host of its domain or server, else the configured one).

What applies to message, or nil when nothing does; with click tracking its links are stored. raw is the stored message.

Records an open or a click of recipient (from resolve/2), unless its server no longer tracks that kind or the recipient has reached the limit. Option limit replaces events_per_minute.

The recipient a token was made for (with its message and server), and for :click the link. :error for a token that does not verify, and for one whose recipient, message or link no longer exists: the two cannot be told apart, and a token that does not verify is never looked up.

Puts the recipient's address on its server's suppression list (reason :unsubscribe, source "list-unsubscribe"), unless it is already suppressed: repeating it changes nothing, and an entry for another reason stays as it is.

The plan with its click links stored, as personalise/3 takes it.

Types

client()

@type client() :: %{ip: :inet.ip_address() | nil, user_agent: String.t() | nil}

plan()

@type plan() :: %{
  base: String.t(),
  opens: boolean(),
  clicks: boolean(),
  unsubscribe: boolean()
}

What personalisation does for a message, from plan/2.

prepared()

@type prepared() :: %{
  base: String.t(),
  opens: boolean(),
  unsubscribe: boolean(),
  links: %{required(String.t()) => non_neg_integer()} | nil
}

What applies to a message's recipients, from prepare/3.

Functions

applies?(message, opts \\ [])

@spec applies?(Karta.Messages.Message.t(), keyword()) :: boolean()

Whether a message is personalised for each recipient (plan/2 says how).

base_url()

@spec base_url() :: String.t() | nil

The configured tracking URL, or nil.

counts(message)

@spec counts(Karta.Messages.Message.t()) :: %{
  required(Ecto.UUID.t()) => %{
    open: non_neg_integer(),
    click: non_neg_integer()
  }
}

How many opens and clicks each recipient of a message has: %{recipient_id => %{open: n, click: n}}.

events(recipient)

The opens and clicks of a recipient, oldest first, each with its link.

events(message, opts)

The opens and clicks of a message, oldest first, each with its link. Options: limit (default 100, at most 1000) and after (an event id: the list continues after it).

personalise(raw, prepared, recipient)

@spec personalise(binary(), prepared(), Karta.Outbound.Recipient.t()) :: binary()

The message as sent to recipient, from what prepare/3 returned.

plan(message, opts \\ [])

@spec plan(Karta.Messages.Message.t(), keyword()) :: plan() | nil

What personalisation would do for message (its server preloaded), or nil when nothing applies — nothing ever applies to a forward of received mail, sent from an SRS address. Option base_url replaces the message's tracking URL (the tracking host of its domain or server, else the configured one).

A delivery asks this before it reads the stored message: only a message that is personalised per recipient has to be held whole.

prepare(message, raw, opts \\ [])

@spec prepare(Karta.Messages.Message.t(), binary(), keyword()) :: prepared() | nil

What applies to message, or nil when nothing does; with click tracking its links are stored. raw is the stored message.

record(kind, recipient, link, client, opts \\ [])

@spec record(
  :open | :click,
  Karta.Outbound.Recipient.t(),
  Karta.Tracking.Link.t() | nil,
  client(),
  keyword()
) :: :ok

Records an open or a click of recipient (from resolve/2), unless its server no longer tracks that kind or the recipient has reached the limit. Option limit replaces events_per_minute.

resolve(kind, token)

@spec resolve(:open | :unsubscribe, term()) ::
  {:ok, Karta.Outbound.Recipient.t()} | :error
@spec resolve(:click, term()) ::
  {:ok, Karta.Outbound.Recipient.t(), Karta.Tracking.Link.t()} | :error

The recipient a token was made for (with its message and server), and for :click the link. :error for a token that does not verify, and for one whose recipient, message or link no longer exists: the two cannot be told apart, and a token that does not verify is never looked up.

unsubscribe(recipient)

@spec unsubscribe(Karta.Outbound.Recipient.t()) :: :ok

Puts the recipient's address on its server's suppression list (reason :unsubscribe, source "list-unsubscribe"), unless it is already suppressed: repeating it changes nothing, and an entry for another reason stays as it is.

with_links(plan, message, raw)

@spec with_links(plan(), Karta.Messages.Message.t(), binary()) :: prepared()

The plan with its click links stored, as personalise/3 takes it.