aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/site/html.go9
-rw-r--r--cmd/site/main.go18
2 files changed, 27 insertions, 0 deletions
diff --git a/cmd/site/html.go b/cmd/site/html.go
index ba304c5..20be7de 100644
--- a/cmd/site/html.go
+++ b/cmd/site/html.go
@@ -8,6 +8,7 @@ import (
"time"
"github.com/Xe/ln"
+ analytics "gopkg.in/segmentio/analytics-go.v3"
)
func logTemplateTime(name string, from time.Time) {
@@ -70,4 +71,12 @@ func (s *Site) showPost(w http.ResponseWriter, r *http.Request) {
}
s.renderTemplatePage("blogpost.html", p).ServeHTTP(w, r)
+
+ if s.segment != nil {
+ s.segment.Enqueue(&analytics.Track{
+ UserId: Hash(r.RemoteAddr, r.Header.Get("X-Forwarded-For")),
+ Event: "Post Viewed",
+ Properties: analytics.NewProperties().SetURL(r.RequestURI).SetTitle(p.Title),
+ })
+ }
}
diff --git a/cmd/site/main.go b/cmd/site/main.go
index bad297e..7dbe9bc 100644
--- a/cmd/site/main.go
+++ b/cmd/site/main.go
@@ -17,6 +17,7 @@ import (
"github.com/gorilla/feeds"
blackfriday "github.com/russross/blackfriday"
"github.com/tj/front"
+ "gopkg.in/segmentio/analytics-go.v3"
)
var port = os.Getenv("PORT")
@@ -47,11 +48,24 @@ type Site struct {
templates map[string]*template.Template
tlock sync.RWMutex
+
+ segment analytics.Client
}
func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ln.Log(r.Context(), ln.F{"action": "Site.ServeHTTP", "user_ip_address": r.RemoteAddr, "path": r.RequestURI})
+
s.mux.ServeHTTP(w, r)
+
+ if s.segment != nil {
+ if !strings.HasPrefix(r.RequestURI, "/blog/") {
+ s.segment.Enqueue(&analytics.Track{
+ UserId: Hash(r.RemoteAddr, r.Header.Get("X-Forwarded-For")),
+ Event: "Page Viewed",
+ Properties: analytics.NewProperties().SetURL(r.RequestURI),
+ })
+ }
+ }
}
// Build creates a new Site instance or fails.
@@ -88,6 +102,10 @@ func Build() (*Site, error) {
templates: map[string]*template.Template{},
}
+ if wk := os.Getenv("SEGMENT_WRITE_KEY"); wk != "" {
+ s.segment = analytics.New(wk)
+ }
+
err := filepath.Walk("./blog/", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err