, which * is what tells a click on my own post apart from someone else's link. */ final class Source { /** The platforms the site shares posts to; the values of ?via=. */ public const PLATFORMS = ['linkedin', 'x', 'facebook', 'instagram']; /** Host (or the end of one) => platform. */ private const SOCIAL = [ 'linkedin.com' => 'linkedin', 'lnkd.in' => 'linkedin', 'com.linkedin.android' => 'linkedin', 't.co' => 'x', 'x.com' => 'x', 'twitter.com' => 'x', 'com.twitter.android' => 'x', 'facebook.com' => 'facebook', 'fb.com' => 'facebook', 'fb.me' => 'facebook', 'com.facebook.katana' => 'facebook', 'instagram.com' => 'instagram', 'com.instagram.android' => 'instagram', 'threads.net' => 'threads', 'threads.com' => 'threads', 'bsky.app' => 'bluesky', 'reddit.com' => 'reddit', 'youtube.com' => 'youtube', 'youtu.be' => 'youtube', 'news.ycombinator.com' => 'hackernews', 'tweakers.net' => 'tweakers', ]; /** Host (or the end of one) => search engine. */ private const SEARCH = [ 'bing.com' => 'bing', 'duckduckgo.com' => 'duckduckgo', 'ecosia.org' => 'ecosia', 'startpage.com' => 'startpage', 'search.brave.com' => 'brave', 'yandex.com' => 'yandex', 'yandex.ru' => 'yandex', 'baidu.com' => 'baidu', 'kagi.com' => 'kagi', 'qwant.com' => 'qwant', 'search.yahoo.com' => 'yahoo', 'com.google.android.googlequicksearchbox' => 'google', ]; private function __construct( public readonly string $channel, public readonly ?string $source, public readonly ?string $referrerHost, ) {} /** * @param string $via the ?via= of the address the visitor arrived on * @param string $campaign its ?utm_source= * @param string $referrer the address of the page that linked here * @param string $ownHost this site's own host: a link from the site itself is no source */ public static function of(string $via, string $campaign, string $referrer, string $ownHost): self { $host = self::host($referrer); if ($host === self::bare($ownHost)) { $host = null; } $via = strtolower(trim($via)); if (in_array($via, self::PLATFORMS, true)) { return new self('share', $via, $host); } $campaign = strtolower(trim($campaign)); if ($campaign !== '' && preg_match('/^[a-z0-9._-]{1,80}$/', $campaign) === 1) { $platform = self::SOCIAL[$campaign.'.com'] ?? ($campaign === 'twitter' ? 'x' : null); return $platform !== null ? new self('social', $platform, $host) : new self('campaign', $campaign, $host); } if ($host === null) { return new self('direct', null, null); } if (($platform = self::match($host, self::SOCIAL)) !== null) { return new self('social', $platform, $host); } // google.com, google.nl, google.co.uk ... if (preg_match('/(^|\.)google\.[a-z.]{2,6}$/', $host) === 1) { return new self('search', 'google', $host); } if (($engine = self::match($host, self::SEARCH)) !== null) { return new self('search', $engine, $host); } return new self('referral', $host, $host); } /** The host of an address, lower case and without "www."; null when there is none. */ private static function host(string $address): ?string { $address = trim($address); if ($address === '') { return null; } $host = parse_url($address, PHP_URL_HOST); if (! is_string($host) || $host === '' || strlen($host) > 255) { return null; } return self::bare($host); } private static function bare(string $host): string { $host = strtolower($host); return str_starts_with($host, 'www.') ? substr($host, 4) : $host; } /** @param array $known */ private static function match(string $host, array $known): ?string { foreach ($known as $end => $name) { if ($host === $end || str_ends_with($host, '.'.$end)) { return $name; } } return null; } }