Files
Kavosh/app/services/referrer.py
T
2026-08-07 21:17:17 +03:30

20 lines
680 B
Python

"""Referrer -> domain bucketing (Chapter 08 rollup, Method A)."""
from __future__ import annotations
from urllib.parse import urlparse
def referrer_domain(referrer: str | None) -> str | None:
"""Bucket a raw referrer URL to its host, stripping a leading 'www.'.
Full-URL cardinality would make the daily rollup unbounded in row
count; domain bucketing keeps it small (Ch03's bounded-resource bias).
Empty/unparsable referrers return None and are excluded from the rollup.
"""
if not referrer:
return None
host = urlparse(referrer).netloc.lower()
if not host:
return None
return host[4:] if host.startswith("www.") else host