Repository navigation
Expand file tree
/
Copy pathstate.go
More file actions
69 lines (63 loc) · 1.53 KB
/
Copy pathstate.go
File metadata and controls
69 lines (63 loc) · 1.53 KB
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
65
66
67
68
69
package main
import (
"log"
"net/http"
"net/url"
"runtime"
"strconv"
"strings"
"time"
)
type fireflyState struct {
trackingID string
clientID string
httpClient *http.Client
ch chan string
}
func newState(clientID string, trackingID string, proxyURL *url.URL) *fireflyState {
return &fireflyState{
trackingID: trackingID,
clientID: clientID,
httpClient: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
},
Timeout: time.Minute,
},
ch: make(chan string, 10),
}
}
func (s *fireflyState) reportEvent(category, action, label, value string) {
log.Printf("report google analytics event: [%s %s]", category, action)
resp, err := s.httpClient.PostForm("https://www.google-analytics.com/collect", url.Values{
"v": {"1"},
"tid": {s.trackingID},
"cid": {s.clientID},
"an": {"Firefly"},
"ua": {strings.Join([]string{runtime.GOOS, runtime.GOARCH}, "_")},
"av": {FIREFLY_VERSION},
"t": {"event"},
"ec": {category},
"ea": {action},
"el": {label},
"ev": {value},
})
if err != nil {
log.Printf("error to report google analytics event: %s", err)
} else {
resp.Body.Close()
}
}
func (s *fireflyState) event(category string, action string, label string, value int) {
s.ch <- strings.Join([]string{"event", category, action, label, strconv.Itoa(value)}, "|")
}
func (s *fireflyState) run() {
for {
cmd := <-s.ch
switch {
case strings.HasPrefix(cmd, "event"):
args := strings.Split(cmd, "|")
s.reportEvent(args[1], args[2], args[3], args[4])
}
}
}