-
Notifications
You must be signed in to change notification settings - Fork 208
/
ga.go
35 lines (29 loc) · 895 Bytes
/
ga.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
package metrics
import (
v1 "github.com/mjpitz/go-ga/client/v1"
"github.com/mjpitz/go-ga/client/v1/gatypes"
)
var (
previousTraffic int64
client = v1.NewClient("UA-222030361-1", "customUserAgent")
)
// ReportStatistics sends basic usage events to google analytics
func ReportStatistics(traffic int64, clientID string) error {
delta := traffic - previousTraffic
previousTraffic = traffic
return trackEvent(delta, clientID)
}
func trackEvent(traffic int64, clientID string) error {
const kb = 1024
return client.SendPost(&gatypes.Payload{
HitType: "event",
NonInteractionHit: true,
DisableAdvertisingPersonalization: true,
Users: gatypes.Users{ClientID: clientID},
Event: gatypes.Event{
EventCategory: "statistics",
EventAction: "heartbeat",
EventValue: traffic / kb,
},
})
}