Add ExtractDomain to pkg/uri for eTLD+1 extraction

Uses golang.org/x/net/publicsuffix to extract the effective
top-level domain plus one label from a URL. Used to populate
detected_trackers.initiator_domain for domain-based tracker
attribution.

Signed-off-by: Émile Ré <emile@getprobo.com>
Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
Émile Ré
2026-05-13 16:33:02 +04:00
committed by Émile Ré
parent 60ee8da348
commit 1928aa9b72
2 changed files with 64 additions and 0 deletions

View File

@@ -18,6 +18,9 @@ import (
"database/sql/driver"
"fmt"
"net/url"
"strings"
"golang.org/x/net/publicsuffix"
)
// URI is a validated absolute URI (scheme + host required).
@@ -71,3 +74,28 @@ func (u *URI) Scan(value any) error {
func (u URI) Value() (driver.Value, error) {
return u.String(), nil
}
// ExtractDomain returns the eTLD+1 (effective top-level domain plus one
// label) from a raw URL string. For example:
//
// "https://www.googletagmanager.com/gtag/js" → "googletagmanager.com"
// "https://cdn.segment.io/v1/projects" → "segment.io"
//
// Returns an empty string when the URL cannot be parsed or has no valid
// hostname (e.g. data: URIs, bare IP addresses without a public suffix).
func ExtractDomain(rawURL string) string {
u, err := Parse(rawURL)
if err != nil {
return ""
}
parsed, _ := url.Parse(string(u))
hostname := strings.ToLower(parsed.Hostname())
domain, err := publicsuffix.EffectiveTLDPlusOne(hostname)
if err != nil {
return ""
}
return domain
}