~evanj/evanjon.es

f128562aaf9ad32be8298fb624942109a332a493 — Evan M Jones 3 months ago e5a364f
Feat(segment): Adding very basic analytics to backend.
4 files changed, 59 insertions(+), 0 deletions(-)

M go.mod
M go.sum
M main.go
A track/track.go
M go.mod => go.mod +4 -0
@@ 4,10 4,14 @@ go 1.12

require (
	git.evanjon.es/i/errgroupcount v0.0.0-20200209032944-45e72d8ba8fc
	github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
	github.com/davecgh/go-spew v1.1.1 // indirect
	github.com/gorilla/feeds v1.1.1
	github.com/kr/pretty v0.2.0 // indirect
	github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3 // indirect
	github.com/stretchr/testify v1.4.0
	github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
	gitlab.com/golang-commonmark/markdown v0.0.0-20191127184510-91b5b3c99c19
	golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
	gopkg.in/segmentio/analytics-go.v3 v3.1.0
)

M go.sum => go.sum +8 -0
@@ 1,5 1,7 @@
git.evanjon.es/i/errgroupcount v0.0.0-20200209032944-45e72d8ba8fc h1:1ir5aJcs8EYyUsu8pFK9HQcFWsfqsAG4LU6uPNvOLDo=
git.evanjon.es/i/errgroupcount v0.0.0-20200209032944-45e72d8ba8fc/go.mod h1:sTqmKw5Lmqlkd5CtnSZ+G1XV7xMVt40RCIZ7ksKCldQ=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=


@@ 14,11 16,15 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v2.0.0+incompatible h1:cBXrhZNUf9C+La9/YpS+UHpUT8YD6Td9ZMSU9APFcsk=
github.com/russross/blackfriday v2.0.0+incompatible/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3 h1:ZuhckGJ10ulaKkdvJtiAqsLTiPrLaXSdnVgXJKJkTxE=
github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3/go.mod h1:9/Rh6yILuLysoQnZ2oNooD2g7aBnvM7r/fNVxRNWfBc=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c h1:3lbZUMbMiGUW/LMkfsEABsc5zNT9+b1CvsJx47JzJ8g=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c/go.mod h1:UrdRz5enIKZ63MEE3IF9l2/ebyx59GyGgPi+tICQdmM=
gitlab.com/golang-commonmark/html v0.0.0-20191124015941-a22733972181 h1:K+bMSIx9A7mLES1rtG+qKduLIXq40DAzYHtb0XuCukA=
gitlab.com/golang-commonmark/html v0.0.0-20191124015941-a22733972181/go.mod h1:dzYhVIwWCtzPAa4QP98wfB9+mzt33MSmM8wsKiMi2ow=
gitlab.com/golang-commonmark/linkify v0.0.0-20191026162114-a0c2df6c8f82 h1:oYrL81N608MLZhma3ruL8qTM4xcpYECGut8KSxRY59g=


@@ 38,5 44,7 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/segmentio/analytics-go.v3 v3.1.0 h1:UzxH1uaGZRpMKDhJyBz0pexz6yUoBU3x8bJsRk/HV6U=
gopkg.in/segmentio/analytics-go.v3 v3.1.0/go.mod h1:4QqqlTlSSpVlWA9/9nDcPw+FkM2yv1NQoYjUbL9/JAw=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

M main.go => main.go +5 -0
@@ 16,6 16,7 @@ import (
	"evanjon.es/contentful"
	"evanjon.es/markdown"
	rssencoder "evanjon.es/rss"
	"evanjon.es/track"
)

type hook string


@@ 31,11 32,14 @@ const (
)

var app api.Endpoint
var tracker track.Track

func init() {
	app = api.New()
	app.Tmpl("html/*")

	tracker = track.New(os.Getenv("SEGMENT_KEY"))

	content := contentful.New(
		markdown.New(),
		os.Getenv("SPACE_ID"),


@@ 106,4 110,5 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request) {
		return
	}
	handler.ServeHTTP(w, r)
	tracker.ServeHTTP(w, r) // This doesn't actually serve responses.
}

A track/track.go => track/track.go +42 -0
@@ 0,0 1,42 @@
package track

import (
	"net/http"
	"time"

	"gopkg.in/segmentio/analytics-go.v3"
)

type event string

const (
	pageView event = "PageView"
	// Note: segment _requires_ that we have a UserId - but we don't care about
	// it.
	userId = "anonymous user"
)

type Track struct {
	secret string
}

func New(secret string) Track {
	return Track{secret}
}

// ServeHTTP doesn't ACTUALLY serve http requests, just sends some of the client
// info to segment.
func (t Track) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	client := analytics.New(t.secret)
	defer client.Close()
	client.Enqueue(analytics.Page{
		UserId:    userId,
		Name:      string(pageView),
		Timestamp: time.Now(),
		Properties: analytics.NewProperties().
			Set("Path", r.URL.Path).
			Set("UserAgent", r.UserAgent()).
			Set("Referer", r.Referer()).
			Set("RequestURI", r.RequestURI),
	})
}