2016-11-21 12:24:50 +00:00
|
|
|
package api
|
2016-11-19 21:35:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-12-10 13:16:05 +00:00
|
|
|
"strings"
|
2016-11-19 21:35:23 +00:00
|
|
|
"github.com/mssola/user_agent"
|
2016-11-21 12:24:50 +00:00
|
|
|
"github.com/dannyvankooten/ana/models"
|
2016-12-08 08:16:43 +00:00
|
|
|
"github.com/dannyvankooten/ana/db"
|
2016-12-08 11:10:13 +00:00
|
|
|
"encoding/base64"
|
2016-11-19 21:35:23 +00:00
|
|
|
)
|
|
|
|
|
2016-12-10 13:16:05 +00:00
|
|
|
func getRequestIp(r *http.Request) string {
|
|
|
|
ipAddress := r.RemoteAddr
|
|
|
|
|
|
|
|
headerForwardedFor := r.Header.Get("X-Forwarded-For")
|
|
|
|
if( headerForwardedFor != "" ) {
|
|
|
|
ipAddress = headerForwardedFor
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipAddress
|
|
|
|
}
|
|
|
|
|
2016-11-21 12:24:50 +00:00
|
|
|
func CollectHandler(w http.ResponseWriter, r *http.Request) {
|
2016-11-19 21:35:23 +00:00
|
|
|
ua := user_agent.New(r.UserAgent())
|
2016-11-21 12:24:50 +00:00
|
|
|
|
|
|
|
// abort if this is a bot.
|
2016-11-19 21:35:23 +00:00
|
|
|
if ua.Bot() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-10 13:16:05 +00:00
|
|
|
q := r.URL.Query()
|
|
|
|
|
|
|
|
// find or insert page
|
|
|
|
page := models.Page{
|
|
|
|
Path: q.Get("p"),
|
|
|
|
Title: q.Get("t"),
|
|
|
|
Hostname: q.Get("h"),
|
|
|
|
}
|
|
|
|
stmt, _ := db.Conn.Prepare("SELECT p.id FROM pages p WHERE p.hostname = ? AND p.path = ? LIMIT 1")
|
|
|
|
defer stmt.Close()
|
|
|
|
err := stmt.QueryRow(page.Hostname, page.Path).Scan(&page.ID)
|
|
|
|
if err != nil {
|
|
|
|
page.Save(db.Conn)
|
|
|
|
}
|
|
|
|
|
2016-12-10 14:58:54 +00:00
|
|
|
// find or insert visitor.
|
|
|
|
// TODO: Mask IP Address
|
2016-12-10 13:16:05 +00:00
|
|
|
visitor := models.Visitor{
|
|
|
|
IpAddress: getRequestIp(r),
|
|
|
|
BrowserLanguage: q.Get("l"),
|
|
|
|
ScreenResolution: q.Get("sr"),
|
|
|
|
DeviceOS: ua.OS(),
|
|
|
|
Country: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
// add browser details
|
|
|
|
visitor.BrowserName, visitor.BrowserVersion = ua.Browser()
|
|
|
|
versionParts := strings.SplitN(visitor.BrowserVersion, ".", 3)
|
|
|
|
if len(versionParts) > 1 {
|
|
|
|
visitor.BrowserVersion = versionParts[0] + "." + versionParts[1]
|
|
|
|
}
|
|
|
|
|
2016-12-10 14:58:54 +00:00
|
|
|
// query by unique visitor key
|
|
|
|
visitor.GenerateKey()
|
2016-12-10 13:16:05 +00:00
|
|
|
stmt, _ = db.Conn.Prepare("SELECT v.id FROM visitors v WHERE v.visitor_key = ? LIMIT 1")
|
|
|
|
defer stmt.Close()
|
|
|
|
err = stmt.QueryRow(visitor.Key).Scan(&visitor.ID)
|
|
|
|
if err != nil {
|
|
|
|
visitor.Save(db.Conn)
|
|
|
|
}
|
|
|
|
|
2016-12-10 14:58:54 +00:00
|
|
|
pageview := models.Pageview{
|
2016-12-10 13:16:05 +00:00
|
|
|
PageID: page.ID,
|
|
|
|
VisitorID: visitor.ID,
|
|
|
|
ReferrerUrl: q.Get("ru"),
|
|
|
|
ReferrerKeyword: q.Get("rk"),
|
2016-11-25 15:39:51 +00:00
|
|
|
}
|
2016-11-19 21:35:23 +00:00
|
|
|
|
2016-12-10 13:16:05 +00:00
|
|
|
// only store referrer URL if not coming from own site
|
2016-12-10 14:58:54 +00:00
|
|
|
if strings.Contains(pageview.ReferrerUrl, page.Hostname) {
|
|
|
|
pageview.ReferrerUrl = ""
|
2016-11-19 21:35:23 +00:00
|
|
|
}
|
|
|
|
|
2016-12-10 14:58:54 +00:00
|
|
|
pageview.Save(db.Conn)
|
2016-11-19 21:35:23 +00:00
|
|
|
|
|
|
|
// don't you cache this
|
|
|
|
w.Header().Set("Content-Type", "image/gif")
|
|
|
|
w.Header().Set("Expires", "Mon, 01 Jan 1990 00:00:00 GMT")
|
|
|
|
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
|
w.Header().Set("Pragma", "no-cache")
|
2016-12-08 11:10:13 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
|
|
|
// 1x1 px transparent GIF
|
|
|
|
b, _ := base64.StdEncoding.DecodeString("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")
|
|
|
|
w.Write(b)
|
2016-11-19 21:35:23 +00:00
|
|
|
}
|