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

Side by Side 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, 3 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 unified diff | Download patch
« no previous file with comments | « milo/appengine/settings/themes.go ('k') | server/analytics/doc.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 package analytics
6
7 // analytics.go is contains public functions for getting
8 // the Google Analytics ID and javascript snippets out from the admin settings.
9
10 import (
11 "fmt"
12 "html/template"
13
14 "golang.org/x/net/context"
15
16 "github.com/luci/luci-go/common/logging"
17 )
18
19 // ID returns the Google Analytics ID if it's set, and "" otherwise.
20 func ID(c context.Context) string {
21 return fetchCachedSettings(c).AnalyticsID
22 }
23
24 // Snippet returns the html snippet for Google Analytics, including the
25 // <script> tag and ID, if ID is set.
26 func Snippet(c context.Context) template.HTML {
27 id := ID(c)
28 if id == "" {
29 return ""
30 }
31 if !rAllowed.MatchString(id) {
32 logging.Errorf(c, "Analytics ID %s does not match UA-\\d+-\\d+", id)
33 return ""
34 }
35 return template.HTML(fmt.Sprintf(`
36 <script>
37 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||functio n(){
38 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createEleme nt(o),
39 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefo re(a,m)
40 })(window,document,'script','https://www.google-analytics.com/analytics. js','ga');
41
42 ga('create', '%s', 'auto');
43 ga('send', 'pageview');
44 </script>
45 `, id))
46 }
OLDNEW
« 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