From d3e16a4a094794fcc6ef2596dc6ee241b3e191e2 Mon Sep 17 00:00:00 2001 From: sergystepanov Date: Fri, 30 Jul 2021 12:29:16 +0300 Subject: [PATCH] Make Google Analytics injection optional (#336) This allows switching GA client statistics on the client by setting these params in the config.yaml file: analytics: inject: false/true gtag: (your tag) By default, GA analytics will be disabled. --- configs/config.yaml | 3 +++ pkg/config/coordinator/config.go | 7 +++++++ pkg/coordinator/coordinator.go | 2 +- pkg/coordinator/handlers.go | 17 +++++++++++------ web/index.html | 14 +++++--------- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/configs/config.yaml b/configs/config.yaml index 53e53dfe..dc88f6d6 100644 --- a/configs/config.yaml +++ b/configs/config.yaml @@ -41,6 +41,9 @@ coordinator: httpsPort: 443 httpsKey: httpsChain: + analytics: + inject: false + gtag: worker: network: diff --git a/pkg/config/coordinator/config.go b/pkg/config/coordinator/config.go index 47511f0c..c11b98b8 100644 --- a/pkg/config/coordinator/config.go +++ b/pkg/config/coordinator/config.go @@ -12,6 +12,7 @@ import ( type Config struct { Coordinator struct { + Analytics Analytics PublicDomain string PingServer string DebugHost string @@ -24,6 +25,12 @@ type Config struct { Webrtc webrtcConfig.Webrtc } +// Analytics is optional Google Analytics +type Analytics struct { + Inject bool + Gtag string +} + // allows custom config path var configPath string diff --git a/pkg/coordinator/coordinator.go b/pkg/coordinator/coordinator.go index db9a4e91..7741e8f4 100644 --- a/pkg/coordinator/coordinator.go +++ b/pkg/coordinator/coordinator.go @@ -73,7 +73,7 @@ func makeHTTPServer(server *Server) *http.Server { r.HandleFunc("/ws", server.WS) r.HandleFunc("/wso", server.WSO) r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./web")))) - r.PathPrefix("/").HandlerFunc(server.GetWeb) + r.PathPrefix("/").Handler(server.GetWeb(server.cfg)) svmux := &http.ServeMux{} svmux.Handle("/", r) diff --git a/pkg/coordinator/handlers.go b/pkg/coordinator/handlers.go index f535d46c..91b4b0b6 100644 --- a/pkg/coordinator/handlers.go +++ b/pkg/coordinator/handlers.go @@ -54,13 +54,18 @@ func NewServer(cfg coordinator.Config, library games.GameLibrary) *Server { } // GetWeb returns web frontend -func (o *Server) GetWeb(w http.ResponseWriter, r *http.Request) { - tmpl, err := template.ParseFiles(index) - if err != nil { - log.Fatal(err) - } +func (o *Server) GetWeb(conf coordinator.Config) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + tpl, err := template.ParseFiles(index) + if err != nil { + log.Fatal(err) + } - tmpl.Execute(w, struct{}{}) + // render index page with some tpl values + if err = tpl.Execute(w, conf.Coordinator.Analytics); err != nil { + log.Fatal(err) + } + }) } // getPingServer returns the server for latency check of a zone. diff --git a/web/index.html b/web/index.html index 273abf4b..27c309eb 100644 --- a/web/index.html +++ b/web/index.html @@ -137,19 +137,15 @@ - - +{{if .Inject}} + +{{end}}