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.
This commit is contained in:
sergystepanov 2021-07-30 12:29:16 +03:00 committed by GitHub
parent ff562503f1
commit d3e16a4a09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 16 deletions

3
configs/config.yaml vendored
View file

@ -41,6 +41,9 @@ coordinator:
httpsPort: 443
httpsKey:
httpsChain:
analytics:
inject: false
gtag:
worker:
network:

View file

@ -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

View file

@ -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)

View file

@ -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.

14
web/index.html vendored
View file

@ -137,19 +137,15 @@
<script src="/static/js/init.js?v=5"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-145078282-1"></script>
{{if .Inject}}
<script async src="https://www.googletagmanager.com/gtag/js?id={{.Gtag}}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'UA-145078282-1');
gtag('config', '{{.Gtag}}');
</script>
{{end}}
</body>