PhoenixKitWebAnalytics.Referrer (PhoenixKitWebAnalytics v0.2.0)

Copy Markdown View Source

Turns a raw Referer header into a {source, medium} pair for reporting.

source is a human-readable name ("Google", "Hacker News") when the host is recognised, otherwise the bare host ("example.com"). medium is one of "none", "organic", "social", "referral", "internal", "email".

UTM parameters, when present, take precedence over the header — that's what utm_medium in a campaign URL means — but that resolution lives in PhoenixKitWebAnalytics.Collector, not here. This module only reads the referrer.

iex> PhoenixKitWebAnalytics.Referrer.classify("https://www.google.com/search?q=x", "myapp.com")
{"Google", "organic"}

iex> PhoenixKitWebAnalytics.Referrer.classify(nil, "myapp.com")
{nil, "none"}

iex> PhoenixKitWebAnalytics.Referrer.classify("https://myapp.com/blog", "myapp.com")
{nil, "internal"}

Summary

Functions

Classifies a referrer against the current request host.

The bare host of a referrer URL, with any leading www. removed.

Lowercases a host and drops a leading www., so myapp.com and WWW.MyApp.com compare equal.

Functions

classify(referrer, site_host \\ nil)

@spec classify(String.t() | nil, String.t() | nil) :: {String.t() | nil, String.t()}

Classifies a referrer against the current request host.

Passing the request host is what makes internal navigation distinguishable from an inbound referral — without it, every in-site link click would inflate the referrers report.

host_of(url)

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

The bare host of a referrer URL, with any leading www. removed.

iex> PhoenixKitWebAnalytics.Referrer.host_of("https://www.Example.com/a/b?c=1")
"example.com"

normalize_host(host)

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

Lowercases a host and drops a leading www., so myapp.com and WWW.MyApp.com compare equal.