Skip to content

Commit

Permalink
Merge pull request #352 from kubeshop/feat/inject-ga-credentials
Browse files Browse the repository at this point in the history
feat: inject analytics credentials into html
  • Loading branch information
mathnogueira authored Apr 26, 2022
2 parents 167e904 + ccd486f commit 0ad9a72
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
26 changes: 25 additions & 1 deletion server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package main
import (
"context"
"flag"
"html/template"
"io"
"log"
"net/http"
Expand Down Expand Up @@ -99,7 +100,7 @@ func main() {
fileMatcher := regexp.MustCompile(`\.[a-zA-Z]*$`)
router.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !fileMatcher.MatchString(r.URL.Path) {
http.ServeFile(w, r, dir+"/index.html")
serveIndex(w, dir+"/index.html")
} else {
fileServer.ServeHTTP(w, r)
}
Expand All @@ -109,6 +110,29 @@ func main() {
log.Fatal(http.ListenAndServe(":8080", router))
}

type gaParams struct {
MeasurementId string
AnalyticsEnabled bool
}

func serveIndex(w http.ResponseWriter, path string) {
templateData := gaParams{
MeasurementId: os.Getenv("GOOGLE_ANALYTICS_MEASUREMENT_ID"),
AnalyticsEnabled: true,
}

tpl, err := template.ParseFiles(path)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

if err = tpl.Execute(w, templateData); err != nil {
http.Error(w, err.Error(), 500)
return
}
}

func initOtelTracing(ctx context.Context) *sdktrace.TracerProvider {
endpoint := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")
var (
Expand Down
7 changes: 7 additions & 0 deletions web/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>Tracetest</title>
<script>
window.ENV = {
measurementId: "{{ .MeasurementId }}",
analyticsEnabled: "{{ .AnalyticsEnabled }}",
secretKey: {{ .SecretKey }}
};
</script>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down

0 comments on commit 0ad9a72

Please sign in to comment.