From 7b2a4dad512bd32c4f8447dc3df71861caa8e7ac Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Sun, 13 Sep 2020 16:16:49 +0200 Subject: [PATCH 1/4] Updated dependencies. --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 027a278..4ca12c9 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/emvi/api-go v0.2.2 github.com/emvi/logbuch v1.1.1 - github.com/emvi/pirsch v1.5.1 + github.com/emvi/pirsch v1.5.2 github.com/go-sql-driver/mysql v1.5.0 // indirect github.com/golang-migrate/migrate v3.5.4+incompatible github.com/golang-migrate/migrate/v4 v4.12.2 diff --git a/go.sum b/go.sum index 1f610dc..ee17938 100644 --- a/go.sum +++ b/go.sum @@ -100,6 +100,8 @@ github.com/emvi/pirsch v1.5.1-0.20200911122906-93d727c607a9 h1:H1+nLRJsBy1ZuhBP4 github.com/emvi/pirsch v1.5.1-0.20200911122906-93d727c607a9/go.mod h1:GDijqLHM331iWtmDmc7th19RxDrZadRkKoNvd9/kDX8= github.com/emvi/pirsch v1.5.1 h1:HFlR5Ps2mPFqHGkFvEfHLme6beCn5aIpu/fCNz/B43Y= github.com/emvi/pirsch v1.5.1/go.mod h1:GDijqLHM331iWtmDmc7th19RxDrZadRkKoNvd9/kDX8= +github.com/emvi/pirsch v1.5.2 h1:fKGTOngDbVFhbzhbXy2NbaeZrVYSiIoDVy+dce0agFs= +github.com/emvi/pirsch v1.5.2/go.mod h1:GDijqLHM331iWtmDmc7th19RxDrZadRkKoNvd9/kDX8= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= From 0e96dff816984a28f61959d0607d8d71a50fb2c3 Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Sun, 13 Sep 2020 20:04:31 +0200 Subject: [PATCH 2/4] Added sessions and bounces to tracking page. --- main.go | 6 ++++- template/tracking.html | 52 ++++++++++++++++++++++++++++++++---------- tracking/statistics.go | 23 +++++++++++++++---- 3 files changed, 63 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 7a36620..c3ed0a2 100644 --- a/main.go +++ b/main.go @@ -123,7 +123,7 @@ func serveTracking() http.HandlerFunc { } activeVisitorPages, activeVisitors := tracking.GetActiveVisitors() - totalVisitorsLabels, totalVisitorsDps := tracking.GetTotalVisitors(startDate, endDate) + totalVisitorsLabels, totalVisitorsDps, sessionsDps, bouncesDps := tracking.GetTotalVisitors(startDate, endDate) hourlyVisitorsTodayLabels, hourlyVisitorsTodayDps := tracking.GetHourlyVisitorsToday() pageVisitors, pageRank := tracking.GetPageVisits(startDate, endDate) tplCache.RenderWithoutCache(w, "tracking.html", struct { @@ -132,6 +132,8 @@ func serveTracking() http.HandlerFunc { EndDate time.Time TotalVisitorsLabels template.JS TotalVisitorsDps template.JS + SessionsDps template.JS + BouncesDps template.JS PageVisitors []tracking.PageVisitors PageRank []tracking.PageVisitors Languages []pirsch.LanguageStats @@ -149,6 +151,8 @@ func serveTracking() http.HandlerFunc { endDate, totalVisitorsLabels, totalVisitorsDps, + sessionsDps, + bouncesDps, pageVisitors, pageRank, tracking.GetLanguages(startDate, endDate), diff --git a/template/tracking.html b/template/tracking.html index f296f86..eb32ce2 100644 --- a/template/tracking.html +++ b/template/tracking.html @@ -234,12 +234,26 @@ type: "line", data: { labels: [{{.TotalVisitorsLabels}}], - datasets: [{ - backgroundColor: "rgba(127, 127, 127, 0.05)", - borderColor: "#7f7f7f", - label: "Total Visitors", - data: [{{.TotalVisitorsDps}}] - }] + datasets: [ + { + backgroundColor: "rgb(43, 180, 0, 0.02)", + borderColor: "#289800", + label: "Total Visitors", + data: [{{.TotalVisitorsDps}}] + }, + { + backgroundColor: "rgb(0, 63, 197, 0.02)", + borderColor: "#00359f", + label: "Sessions", + data: [{{.SessionsDps}}] + }, + { + backgroundColor: "rgba(194, 0, 0, 0.02)", + borderColor: "#980000", + label: "Bounces", + data: [{{.BouncesDps}}] + } + ] } }); @@ -263,12 +277,26 @@ type: "line", data: { labels: [{{$data.Labels}}], - datasets: [{ - backgroundColor: "rgba(127, 127, 127, 0.05)", - borderColor: "#7f7f7f", - label: "Page Visits", - data: [{{$data.Data}}] - }] + datasets: [ + { + backgroundColor: "rgb(43, 180, 0, 0.02)", + borderColor: "#289800", + label: "Page Visits", + data: [{{$data.Data}}] + }, + { + backgroundColor: "rgb(0, 63, 197, 0.02)", + borderColor: "#00359f", + label: "Sessions", + data: [{{$data.Sessions}}] + }, + { + backgroundColor: "rgba(194, 0, 0, 0.02)", + borderColor: "#980000", + label: "Bounces", + data: [{{$data.Bounces}}] + } + ] } }); {{end}} diff --git a/tracking/statistics.go b/tracking/statistics.go index a10fb52..9ff18e1 100644 --- a/tracking/statistics.go +++ b/tracking/statistics.go @@ -19,6 +19,8 @@ type PageVisitors struct { Visitors int Labels template.JS Data template.JS + Sessions template.JS + Bounces template.JS } func GetActiveVisitors() ([]pirsch.Stats, int) { @@ -43,12 +45,12 @@ func GetHourlyVisitorsToday() (template.JS, template.JS) { return getLabelsAndDataHourly(visitors) } -func GetTotalVisitors(startDate, endDate time.Time) (template.JS, template.JS) { +func GetTotalVisitors(startDate, endDate time.Time) (template.JS, template.JS, template.JS, template.JS) { visitors, err := analyzer.Visitors(&pirsch.Filter{From: startDate, To: endDate}) if err != nil { logbuch.Error("Error reading visitor statistics", logbuch.Fields{"err": err}) - return "", "" + return "", "", "", "" } return getLabelsAndData(visitors) @@ -65,12 +67,14 @@ func GetPageVisits(startDate, endDate time.Time) ([]PageVisitors, []PageVisitors pageVisitors := make([]PageVisitors, len(visits)) for i, visit := range visits { - labels, data := getLabelsAndData(visit.Stats) + labels, data, sessions, bounces := getLabelsAndData(visit.Stats) pageVisitors[i] = PageVisitors{ Path: visit.Path, Visitors: sumVisitors(visit.Stats), Labels: labels, Data: data, + Sessions: sessions, + Bounces: bounces, } } @@ -148,18 +152,27 @@ func sumVisitors(stats []pirsch.Stats) int { return sum } -func getLabelsAndData(visitors []pirsch.Stats) (template.JS, template.JS) { +func getLabelsAndData(visitors []pirsch.Stats) (template.JS, template.JS, template.JS, template.JS) { var labels strings.Builder var dp strings.Builder + var sessions strings.Builder + var bounces strings.Builder for _, point := range visitors { labels.WriteString(fmt.Sprintf("'%s',", point.Day.Format(statisticsDateFormat))) dp.WriteString(fmt.Sprintf("%d,", point.Visitors)) + sessions.WriteString(fmt.Sprintf("%d,", point.Sessions)) + bounces.WriteString(fmt.Sprintf("%d,", point.Bounces)) } labelsStr := labels.String() dataStr := dp.String() - return template.JS(labelsStr[:len(labelsStr)-1]), template.JS(dataStr[:len(dataStr)-1]) + sessionsStr := sessions.String() + bouncesStr := sessions.String() + return template.JS(labelsStr[:len(labelsStr)-1]), + template.JS(dataStr[:len(dataStr)-1]), + template.JS(sessionsStr[:len(sessionsStr)-1]), + template.JS(bouncesStr[:len(sessionsStr)-1]) } func getLabelsAndDataHourly(visitors []pirsch.VisitorTimeStats) (template.JS, template.JS) { From 5469b3722a17c11e10001ff915fceb051bdaa432 Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Mon, 14 Sep 2020 21:29:16 +0200 Subject: [PATCH 3/4] Enable session tracking. --- tracking/tracking.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tracking/tracking.go b/tracking/tracking.go index 4b8ad0f..39e6c8e 100644 --- a/tracking/tracking.go +++ b/tracking/tracking.go @@ -30,9 +30,9 @@ func NewTracker() (*pirsch.Tracker, context.CancelFunc) { store = pirsch.NewPostgresStore(conn, nil) tracker := pirsch.NewTracker(store, os.Getenv("MB_TRACKING_SALT"), &pirsch.TrackerConfig{ - // I don't care about traffic from my own website - ReferrerDomainBlacklist: []string{"marvinblum.de"}, + ReferrerDomainBlacklist: []string{"marvinblum.de"}, // I don't care about traffic from my own website ReferrerDomainBlacklistIncludesSubdomains: true, + Sessions: true, }) analyzer = pirsch.NewAnalyzer(store) processor := pirsch.NewProcessor(store) From 61685488cdc50663e7e9ca69c66aace5d180ecf6 Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Mon, 14 Sep 2020 21:46:38 +0200 Subject: [PATCH 4/4] Fixed bounce/session statistics and updated pirsch to dev version. --- go.mod | 2 +- go.sum | 2 ++ schema/0002_pirsch_update.up.sql | 23 +++++++++++++++++++++++ template/tracking.html | 12 ++++++------ tracking/statistics.go | 4 ++-- 5 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 schema/0002_pirsch_update.up.sql diff --git a/go.mod b/go.mod index 4ca12c9..5961c0c 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/emvi/api-go v0.2.2 github.com/emvi/logbuch v1.1.1 - github.com/emvi/pirsch v1.5.2 + github.com/emvi/pirsch v1.5.3-0.20200914194223-a990d20e91a2 github.com/go-sql-driver/mysql v1.5.0 // indirect github.com/golang-migrate/migrate v3.5.4+incompatible github.com/golang-migrate/migrate/v4 v4.12.2 diff --git a/go.sum b/go.sum index ee17938..0c9709b 100644 --- a/go.sum +++ b/go.sum @@ -102,6 +102,8 @@ github.com/emvi/pirsch v1.5.1 h1:HFlR5Ps2mPFqHGkFvEfHLme6beCn5aIpu/fCNz/B43Y= github.com/emvi/pirsch v1.5.1/go.mod h1:GDijqLHM331iWtmDmc7th19RxDrZadRkKoNvd9/kDX8= github.com/emvi/pirsch v1.5.2 h1:fKGTOngDbVFhbzhbXy2NbaeZrVYSiIoDVy+dce0agFs= github.com/emvi/pirsch v1.5.2/go.mod h1:GDijqLHM331iWtmDmc7th19RxDrZadRkKoNvd9/kDX8= +github.com/emvi/pirsch v1.5.3-0.20200914194223-a990d20e91a2 h1:HoKxeUtA0E4QVuw2kaR4TioT2OZPIzXQ2rA6EJ3GRTM= +github.com/emvi/pirsch v1.5.3-0.20200914194223-a990d20e91a2/go.mod h1:GDijqLHM331iWtmDmc7th19RxDrZadRkKoNvd9/kDX8= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= diff --git a/schema/0002_pirsch_update.up.sql b/schema/0002_pirsch_update.up.sql new file mode 100644 index 0000000..e5786a2 --- /dev/null +++ b/schema/0002_pirsch_update.up.sql @@ -0,0 +1,23 @@ +ALTER TABLE "hit" ADD COLUMN "screen_width" integer DEFAULT 0; +ALTER TABLE "hit" ADD COLUMN "screen_height" integer DEFAULT 0; + +CREATE TABLE "screen_stats" ( + id bigint NOT NULL UNIQUE, + tenant_id bigint, + day date NOT NULL, + visitors integer NOT NULL, + width integer NOT NULL, + height integer NOT NULL +); + +CREATE SEQUENCE screen_stats_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + +ALTER SEQUENCE screen_stats_id_seq OWNED BY "screen_stats".id; +ALTER TABLE ONLY "screen_stats" ALTER COLUMN id SET DEFAULT nextval('screen_stats_id_seq'::regclass); +ALTER TABLE ONLY "screen_stats" ADD CONSTRAINT screen_stats_pkey PRIMARY KEY (id); +CREATE INDEX screen_stats_day_index ON screen_stats(day); diff --git a/template/tracking.html b/template/tracking.html index eb32ce2..d58fc7d 100644 --- a/template/tracking.html +++ b/template/tracking.html @@ -237,19 +237,19 @@ datasets: [ { backgroundColor: "rgb(43, 180, 0, 0.02)", - borderColor: "#289800", + borderColor: "rgb(40, 152, 0, 0.5)", label: "Total Visitors", data: [{{.TotalVisitorsDps}}] }, { backgroundColor: "rgb(0, 63, 197, 0.02)", - borderColor: "#00359f", + borderColor: "rgb(0, 53, 159, 0.5)", label: "Sessions", data: [{{.SessionsDps}}] }, { backgroundColor: "rgba(194, 0, 0, 0.02)", - borderColor: "#980000", + borderColor: "rgb(152, 0, 0, 0.5)", label: "Bounces", data: [{{.BouncesDps}}] } @@ -280,19 +280,19 @@ datasets: [ { backgroundColor: "rgb(43, 180, 0, 0.02)", - borderColor: "#289800", + borderColor: "rgb(40, 152, 0, 0.5)", label: "Page Visits", data: [{{$data.Data}}] }, { backgroundColor: "rgb(0, 63, 197, 0.02)", - borderColor: "#00359f", + borderColor: "rgb(0, 53, 159, 0.5)", label: "Sessions", data: [{{$data.Sessions}}] }, { backgroundColor: "rgba(194, 0, 0, 0.02)", - borderColor: "#980000", + borderColor: "rgb(152, 0, 0, 0.5)", label: "Bounces", data: [{{$data.Bounces}}] } diff --git a/tracking/statistics.go b/tracking/statistics.go index 9ff18e1..6887a3c 100644 --- a/tracking/statistics.go +++ b/tracking/statistics.go @@ -168,11 +168,11 @@ func getLabelsAndData(visitors []pirsch.Stats) (template.JS, template.JS, templa labelsStr := labels.String() dataStr := dp.String() sessionsStr := sessions.String() - bouncesStr := sessions.String() + bouncesStr := bounces.String() return template.JS(labelsStr[:len(labelsStr)-1]), template.JS(dataStr[:len(dataStr)-1]), template.JS(sessionsStr[:len(sessionsStr)-1]), - template.JS(bouncesStr[:len(sessionsStr)-1]) + template.JS(bouncesStr[:len(bouncesStr)-1]) } func getLabelsAndDataHourly(visitors []pirsch.VisitorTimeStats) (template.JS, template.JS) {