-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics.go
64 lines (53 loc) · 1.18 KB
/
analytics.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
62
63
64
package analytics
import (
"fmt"
"strings"
"math/rand"
"net/url"
"net/http"
)
const VERSION = "0.1.0"
type UA struct {
ID string
UUID string
GeoID string
}
func CreateUA(id string) *UA {
return &UA{ID: id,}
}
func (ua *UA)SetUUID(uuid string) {
ua.UUID = uuid
}
func (ua *UA)SetGeoID(geoid string) {
ua.GeoID = geoid
}
func (ua *UA)Event(category string, action string, label string, value int) {
hc := http.Client{}
form := url.Values{}
form.Add("v", "1")
form.Add("tid", ua.ID)
if ua.UUID == "" {
ua.UUID = UUID() // cannot be null
}
form.Add("cid", ua.UUID)
form.Add("t", "event")
form.Add("ec", category)
form.Add("ea", action)
form.Add("el", label)
form.Add("ev", fmt.Sprint(value))
if ua.GeoID != "" {
form.Add("geoid", ua.GeoID)
}
req, _ := http.NewRequest("POST", "https://www.google-analytics.com/collect", strings.NewReader(form.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
hc.Do(req)
}
func UUID() string {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
return ""
}
uuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return uuid
}