// Package utm adds UTM tags to links that point at the company's own sites, // so its analytics (Plausible) can tell which posts brought visitors. Links // elsewhere are left alone: no one else's analytics will read the tags. package utm import ( "net/url" "regexp" "strings" ) // Params are the tags for one post. type Params struct { Source string // the channel: x, linkedin, reddit Medium string // "social" Campaign string // the kind of post: partner, competitor, watch, product Content string // which post: entry and draft } // Tagger knows which hosts are the company's. type Tagger struct { hosts []string } // NewTagger takes the company's site and product URLs (or bare hosts). func NewTagger(sites []string) *Tagger { t := &Tagger{} seen := map[string]bool{} for _, s := range sites { h := host(s) if h != "" && !seen[h] { seen[h] = true t.hosts = append(t.hosts, h) } } return t } func host(s string) string { s = strings.TrimSpace(s) if s == "" { return "" } if !strings.Contains(s, "://") { s = "https://" + s } u, err := url.Parse(s) if err != nil { return "" } return strings.TrimPrefix(strings.ToLower(u.Hostname()), "www.") } // Own reports whether rawURL is on one of the company's hosts or their // subdomains. func (t *Tagger) Own(rawURL string) bool { h := host(rawURL) if h == "" || !strings.Contains(rawURL, "://") { return false } for _, own := range t.hosts { if h == own || strings.HasSuffix(h, "."+own) { return true } } return false } // URL tags rawURL if it's the company's. Tags already in the link win. func (t *Tagger) URL(rawURL string, p Params) string { if t == nil || !t.Own(rawURL) { return rawURL } u, err := url.Parse(rawURL) if err != nil { return rawURL } q := u.Query() for k, v := range map[string]string{ "utm_source": p.Source, "utm_medium": p.Medium, "utm_campaign": p.Campaign, "utm_content": p.Content, } { if v != "" && q.Get(k) == "" { q.Set(k, v) } } u.RawQuery = q.Encode() return u.String() } // links finds http(s) URLs in text. Trailing punctuation that usually ends // a sentence isn't part of the link. var links = regexp.MustCompile(`https?://[^\s<>"]+`) // Text tags the company's links inside a post. func (t *Tagger) Text(text string, p Params) string { if t == nil || len(t.hosts) == 0 { return text } return links.ReplaceAllStringFunc(text, func(m string) string { trail := "" for len(m) > 0 && strings.ContainsRune(".,;:!?)]}'", rune(m[len(m)-1])) { trail = m[len(m)-1:] + trail m = m[:len(m)-1] } return t.URL(m, p) + trail }) } // Slug turns a name into a lowercase tag value: "Jitsi Meet" → "jitsi-meet". func Slug(s string) string { var b strings.Builder dash := false for _, r := range strings.ToLower(s) { if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') { b.WriteRune(r) dash = false } else if b.Len() > 0 && !dash { b.WriteByte('-') dash = true } } return strings.TrimSuffix(b.String(), "-") } // Any reports whether the tagger knows any of the company's hosts. func (t *Tagger) Any() bool { return t != nil && len(t.hosts) > 0 } // HasOwnLink reports whether text contains a link to the company's sites. func (t *Tagger) HasOwnLink(text string) bool { if !t.Any() { return false } for _, m := range links.FindAllString(text, -1) { if t.Own(strings.TrimRight(m, ".,;:!?)]}'")) { return true } } return false }