-
Notifications
You must be signed in to change notification settings - Fork 1
/
utm.go
61 lines (53 loc) · 1.11 KB
/
utm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"net/url"
"strings"
)
type utm struct {
UtmValid bool `json:"v"`
UtmExist bool `json:"e"`
UtmSource string `json:"s"`
UtmMedium string `json:"m"`
UtmCampaign string `json:"c"`
UtmID string `json:"i"`
UtmTerm string `json:"t"`
UtmContent string `json:"cn"`
}
func parseUTM(u *url.URL) utm {
result := utm{
UtmExist: false,
UtmValid: false,
}
if u == nil {
return result
}
queries := u.Query()
for k, v := range queries {
kci := strings.ToLower(k)
switch kci {
case "utm_source":
result.UtmExist = true
result.UtmSource = v[0]
case "utm_medium":
result.UtmExist = true
result.UtmMedium = v[0]
case "utm_campaign":
result.UtmExist = true
result.UtmCampaign = v[0]
case "utm_id":
result.UtmExist = true
result.UtmID = v[0]
case "utm_term":
result.UtmExist = true
result.UtmTerm = v[0]
case "utm_content":
result.UtmExist = true
result.UtmContent = v[0]
}
}
result.UtmValid = false
if result.UtmSource != "" && result.UtmMedium != "" && result.UtmCampaign != "" {
result.UtmValid = true
}
return result
}