Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(651)

Unified Diff: server/analytics/analytics.go

Issue 2248893002: Settings page for analytics ID (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: y Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « milo/appengine/settings/themes.go ('k') | server/analytics/doc.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: server/analytics/analytics.go
diff --git a/server/analytics/analytics.go b/server/analytics/analytics.go
new file mode 100644
index 0000000000000000000000000000000000000000..77f92c08b8afbf2cf6056621cd44afb3cf92d794
--- /dev/null
+++ b/server/analytics/analytics.go
@@ -0,0 +1,46 @@
+// Copyright 2016 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package analytics
+
+// analytics.go is contains public functions for getting
+// the Google Analytics ID and javascript snippets out from the admin settings.
+
+import (
+ "fmt"
+ "html/template"
+
+ "golang.org/x/net/context"
+
+ "github.com/luci/luci-go/common/logging"
+)
+
+// ID returns the Google Analytics ID if it's set, and "" otherwise.
+func ID(c context.Context) string {
+ return fetchCachedSettings(c).AnalyticsID
+}
+
+// Snippet returns the html snippet for Google Analytics, including the
+// <script> tag and ID, if ID is set.
+func Snippet(c context.Context) template.HTML {
+ id := ID(c)
+ if id == "" {
+ return ""
+ }
+ if !rAllowed.MatchString(id) {
+ logging.Errorf(c, "Analytics ID %s does not match UA-\\d+-\\d+", id)
+ return ""
+ }
+ return template.HTML(fmt.Sprintf(`
+<script>
+ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+ })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
+
+ ga('create', '%s', 'auto');
+ ga('send', 'pageview');
+</script>
+`, id))
+}
« no previous file with comments | « milo/appengine/settings/themes.go ('k') | server/analytics/doc.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698