summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--main.go5
-rw-r--r--mixpanel.go43
3 files changed, 49 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 2d31fbe..8332d30 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
include $(GOROOT)/src/Make.inc
TARG=aa
-GOFILES=main.go db.go updates.go
+GOFILES=main.go db.go updates.go mixpanel.go
pkgdir=$(QUOTED_GOROOT)/pkg/$(GOOS)_$(GOARCH)
PREREQ=$(pkgdir)/github.com/Philio/GoMySQL.a
diff --git a/main.go b/main.go
index 099767c..7185672 100644
--- a/main.go
+++ b/main.go
@@ -62,6 +62,7 @@ func renderPage(w http.ResponseWriter, page string, data interface{}) {
func home(w http.ResponseWriter, r *http.Request) {
renderPage(w, "home", nil)
+ go track("home", r.Header.Get("X-Forwarded-For"), "", "", "")
}
func playlist(w http.ResponseWriter, r *http.Request) {
@@ -81,6 +82,7 @@ func playlist(w http.ResponseWriter, r *http.Request) {
p := Playlist{Id: id}
renderPage(w, "p", p)
+ go track("p", r.Header.Get("X-Forwarded-For"), id, "", "")
}
func add(w http.ResponseWriter, r *http.Request) {
@@ -124,6 +126,7 @@ func add(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("1"))
addUpdate(pid, addAction,
&Song{Yid: q.Get("yid"), Title: q.Get("title"), User: q.Get("user")})
+ go track("add", r.Header.Get("X-Forwarded-For"), q.Get("pid"), q.Get("title"), q.Get("user"))
}
func remove(w http.ResponseWriter, r *http.Request) {
@@ -176,6 +179,7 @@ func remove(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("1"))
addUpdate(pid, removeAction, &Song{Yid: q.Get("yid")})
+ go track("remove", r.Header.Get("X-Forwarded-For"), q.Get("pid"), "", "")
}
func move(w http.ResponseWriter, r *http.Request) {
@@ -335,4 +339,5 @@ func create(w http.ResponseWriter, r *http.Request) {
return
}
http.Redirect(w, r, "/p/" + idStr, http.StatusSeeOther)
+ go track("create",r.Header.Get("X-Forwarded-For"), idStr, "", "")
}
diff --git a/mixpanel.go b/mixpanel.go
new file mode 100644
index 0000000..6c370bd
--- /dev/null
+++ b/mixpanel.go
@@ -0,0 +1,43 @@
+package main
+
+import (
+ "http"
+ "encoding/base64"
+ "json"
+ "log"
+)
+
+func init() {
+ log.SetFlags(log.Ltime | log.Lshortfile)
+}
+
+func track(event, ip, id, song, user string) {
+ const token = "643bb718adc5d66de18617b4f0bf3489"
+
+ data := make(map[string]interface{})
+ data["event"] = event
+ properties := make(map[string]string)
+ properties["token"] = token
+ properties["ip"] = ip
+ properties["id"] = id
+ if song != "" {
+ properties["song"] = song
+ }
+ if user != "" {
+ properties["user"] = user
+ }
+ data["properties"] = properties
+
+ dataJSON, err := json.Marshal(data)
+ if err != nil {
+ log.Println(err)
+ return
+ }
+
+ values := make(http.Values)
+ values.Add("data", base64.StdEncoding.EncodeToString(dataJSON))
+ r, err := http.PostForm("https://api.mixpanel.com/track/", values)
+ if err == nil {
+ r.Body.Close()
+ }
+}