forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
analytics.go
90 lines (74 loc) · 2.03 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2015 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Sample analytics demonstrates Google Analytics calls from App Engine flexible environment.
package main
import (
"errors"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
"github.com/satori/go.uuid"
"google.golang.org/appengine"
)
var gaPropertyID = mustGetenv("GA_TRACKING_ID")
func mustGetenv(k string) string {
v := os.Getenv(k)
if v == "" {
log.Fatalf("%s environment variable not set.", k)
}
return v
}
func main() {
http.HandleFunc("/", handle)
appengine.Main()
}
func handle(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
if err := trackEvent(r, "Example", "Test action", "label", nil); err != nil {
fmt.Fprintf(w, "Event did not track: %v", err)
return
}
fmt.Fprint(w, "Event tracked.")
}
func trackEvent(r *http.Request, category, action, label string, value *uint) error {
if gaPropertyID == "" {
return errors.New("analytics: GA_TRACKING_ID environment variable is missing")
}
if category == "" || action == "" {
return errors.New("analytics: category and action are required")
}
v := url.Values{
"v": {"1"},
"tid": {gaPropertyID},
// Anonymously identifies a particular user. See the parameter guide for
// details:
// https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cid
//
// Depending on your application, this might want to be associated with the
// user in a cookie.
"cid": {uuid.NewV4().String()},
"t": {"event"},
"ec": {category},
"ea": {action},
"ua": {r.UserAgent()},
}
if label != "" {
v.Set("el", label)
}
if value != nil {
v.Set("ev", fmt.Sprintf("%d", *value))
}
if remoteIP, _, err := net.SplitHostPort(r.RemoteAddr); err != nil {
v.Set("uip", remoteIP)
}
// NOTE: Google Analytics returns a 200, even if the request is malformed.
_, err := http.PostForm("https://www.google-analytics.com/collect", v)
return err
}