aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2019-08-19 13:48:21 -0400
committerGitHub <noreply@github.com>2019-08-19 13:48:21 -0400
commita6c66568c8b59563b64f3ad3d2d4f4a36ec53004 (patch)
treef4cc2f5094b21ba38df370f1d598c3cfd62f6671 /cmd
parenteaca47ba372cd933d273fe277f724e64580e0cd7 (diff)
downloadxesite-a6c66568c8b59563b64f3ad3d2d4f4a36ec53004.tar.xz
xesite-a6c66568c8b59563b64f3ad3d2d4f4a36ec53004.zip
Pageview times experiment (#69)
* experiment: track pageview times * strictly respect do not track * oops * asdfasdfasdf * add blogpost * fix typos oops
Diffstat (limited to 'cmd')
-rw-r--r--cmd/site/main.go1
-rw-r--r--cmd/site/pageview.go53
2 files changed, 54 insertions, 0 deletions
diff --git a/cmd/site/main.go b/cmd/site/main.go
index b7774b1..04d8bc4 100644
--- a/cmd/site/main.go
+++ b/cmd/site/main.go
@@ -223,6 +223,7 @@ func Build() (*Site, error) {
w.Header().Set("Content-Type", "application/xml")
_, _ = smi.WriteTo(w)
})))
+ s.mux.HandleFunc("/api/pageview-timer", handlePageViewTimer)
return s, nil
}
diff --git a/cmd/site/pageview.go b/cmd/site/pageview.go
new file mode 100644
index 0000000..5154659
--- /dev/null
+++ b/cmd/site/pageview.go
@@ -0,0 +1,53 @@
+package main
+
+import (
+ "encoding/json"
+ "io/ioutil"
+ "net/http"
+ "time"
+
+ "github.com/prometheus/client_golang/prometheus"
+ "within.website/ln"
+)
+
+var (
+ readTimes = prometheus.NewHistogramVec(prometheus.HistogramOpts{
+ Name: "blogpage_read_times",
+ Help: "This tracks how much time people spend reading articles on my blog",
+ }, []string{"path"})
+)
+
+func init() {
+ _ = prometheus.Register(readTimes)
+}
+
+func handlePageViewTimer(w http.ResponseWriter, r *http.Request) {
+ if r.Header.Get("DNT") == "1" {
+ http.NotFound(w, r)
+ return
+ }
+
+ data, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ ln.Error(r.Context(), err, ln.Info("while reading data"))
+ http.Error(w, "oopsie whoopsie uwu", http.StatusInternalServerError)
+ return
+ }
+ r.Body.Close()
+
+ type metricsData struct {
+ Path string `json:"path"`
+ StartTime time.Time `json:"start_time"`
+ EndTime time.Time `json:"end_time"`
+ }
+ var md metricsData
+ err = json.Unmarshal(data, &md)
+ if err != nil {
+ http.NotFound(w, r)
+ return
+ }
+
+ diff := md.EndTime.Sub(md.StartTime).Seconds()
+
+ readTimes.WithLabelValues(md.Path).Observe(float64(diff))
+}