fathom/api/collect.go

79 lines
1.7 KiB
Go
Raw Normal View History

package api
2016-11-19 21:35:23 +00:00
import (
"net/http"
"log"
"time"
"github.com/mssola/user_agent"
"github.com/dannyvankooten/ana/models"
"github.com/dannyvankooten/ana/core"
2016-11-19 21:35:23 +00:00
)
func CollectHandler(w http.ResponseWriter, r *http.Request) {
2016-11-19 21:35:23 +00:00
log.Printf("[%s] %s %s (%s)\n", time.Now(), r.Method, r.RequestURI, r.UserAgent())
ua := user_agent.New(r.UserAgent())
// abort if this is a bot.
2016-11-19 21:35:23 +00:00
if ua.Bot() {
return
}
// prepare statement for inserting data
stmt, err := core.DB.Prepare(`INSERT INTO visits(
2016-11-19 21:35:23 +00:00
ip_address,
path,
2016-11-19 21:35:23 +00:00
referrer_url,
browser_language,
browser_name,
browser_version,
device_os,
screen_resolution
) VALUES( ?, ?, ?, ?, ?, ?, ?, ? )`)
if err != nil {
log.Fatal(err.Error())
}
defer stmt.Close()
// TODO: Mask IP Address
// TODO: Query DB to determine whether visitor is returning
q := r.URL.Query()
visit := models.Visit{
2016-11-19 21:35:23 +00:00
Path: q.Get("p"),
IpAddress: r.RemoteAddr,
ReferrerUrl: q.Get("r"),
BrowserLanguage: q.Get("l"),
ScreenResolution: q.Get("sr"),
}
// add browser details
visit.BrowserName, visit.BrowserVersion = ua.Browser()
// add device details
visit.DeviceOS = ua.OS()
log.Printf("%+v\n", visit)
_, err = stmt.Exec(
visit.IpAddress,
visit.Path,
visit.ReferrerUrl,
visit.BrowserLanguage,
visit.BrowserName,
visit.BrowserVersion,
visit.DeviceOS,
visit.ScreenResolution,
)
if err != nil {
log.Fatal(err)
}
// 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")
w.Header().Set("Status", "200")
}