Browse Source

add mixpanel tracking

raylu 14 năm trước cách đây
mục cha
commit
d1bfe384e7
3 tập tin đã thay đổi với 49 bổ sung1 xóa
  1. 1 1
      Makefile
  2. 5 0
      main.go
  3. 43 0
      mixpanel.go

+ 1 - 1
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
 

+ 5 - 0
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, "", "")
 }

+ 43 - 0
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()
+	}
+}