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

Side by Side Diff: server/analytics/settings.go

Issue 2248893002: Settings page for analytics ID (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: Fix tests 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 | « server/analytics/doc.go ('k') | no next file » | 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 import (
8 "fmt"
9 "html/template"
10 "regexp"
11
12 "golang.org/x/net/context"
13
14 "github.com/luci/luci-go/common/logging"
15 "github.com/luci/luci-go/server/settings"
16 )
17
18 // settingsKey is key for global GAE settings (described by analyticsSettings st ruct)
19 // in the settings store. See github.com/luci/luci-go/server/settings.
20 const settingsKey = "analytics"
21
22 // analyticsSettings contain settings to enable Google Analytics.
23 type analyticsSettings struct {
24 // AnalyticsID is a Google Analytics ID an admin can set to enable Analy tics.
25 // The app must support analytics for this to work.
26 AnalyticsID string `json:"analytics_id"`
27 }
28
29 // fetchCachedSettings fetches analyticsSettings from the settings store or pani cs.
30 //
31 // Uses in-process global cache to avoid hitting datastore often. The cache
32 // expiration time is 1 min (see analyticsSettings.expirationTime), meaning
33 // the instance will refetch settings once a minute (blocking only one unlucky
34 // request to do so).
35 //
36 // Panics only if there's no cached value (i.e. it is the first call to this
37 // function in this process ever) and datastore operation fails. It is a good
38 // idea to implement /_ah/warmup to warm this up.
39 func fetchCachedSettings(c context.Context) analyticsSettings {
40 s := analyticsSettings{}
41 switch err := settings.Get(c, settingsKey, &s); {
42 case err == nil:
43 return s
44 case err == settings.ErrNoSettings:
45 // Defaults.
46 return analyticsSettings{
47 AnalyticsID: "",
48 }
49 default:
50 panic(fmt.Errorf("could not fetch GAE settings - %s", err))
51 }
52 }
53
54 // ID returns the Google Analytics ID if it's set, and "" otherwise.
55 func ID(c context.Context) string {
56 return fetchCachedSettings(c).AnalyticsID
57 }
58
59 var rAllowed = regexp.MustCompile("UA-\\d+-\\d+")
60
61 func Snippet(c context.Context) template.HTML {
Vadim Sh. 2016/08/26 23:12:00 add documenting comment
Ryan Tseng 2016/08/29 19:21:40 Done.
62 id := ID(c)
63 if id == "" {
64 return ""
65 }
66 if !rAllowed.MatchString(id) {
67 logging.Errorf(c, "Analytics ID %s does not match UA-\\d+-\\d+", id)
68 return ""
69 }
70 return template.HTML(fmt.Sprintf(`
71 <script>
72 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||functio n(){
73 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createEleme nt(o),
74 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefo re(a,m)
75 })(window,document,'script','https://www.google-analytics.com/analytics. js','ga');
76
77 ga('create', '%s', 'auto');
78 ga('send', 'pageview');
79 </script>
80 `, id))
81 }
82
83 ////////////////////////////////////////////////////////////////////////////////
84 // UI for GAE settings.
85
86 type settingsUIPage struct {
87 settings.BaseUIPage
88 }
89
90 func (settingsUIPage) Title(c context.Context) (string, error) {
91 return "Google Analytics Related Settings", nil
92 }
93
94 func (settingsUIPage) Overview(c context.Context) (template.HTML, error) {
95 return template.HTML(`<p>To generate a Google Analytics Tracking ID</p>
96 <ul>
97 <li> Sign in to <a href="https://www.google.com/analytics/web/#home/">your Analy tics account.</a></li>
98 <li>Select the Admin tab.</li>
99 <li>Select an account from the drop-down menu in the <i>ACCOUNT</i> column.</li>
100 <li>Select a property from the drop-down menu in the <i>PROPERTY</i> column.</li >
101 <li>Under <i>PROPERTY</i>, click <b>Tracking Info > Tracking Code.</b></li>
102 </ul>`), nil
103 }
104
105 func (settingsUIPage) Fields(c context.Context) ([]settings.UIField, error) {
106 return []settings.UIField{
107 {
108 ID: "AnalyticsID",
109 Title: "Google Analytics Tracking ID",
110 Type: settings.UIFieldText,
111 Help: `Tracking ID used for Google Analytics. Filling th is in enables
112 Google Analytics tracking across the app.`,
113 },
114 }, nil
115 }
116
117 func (settingsUIPage) ReadSettings(c context.Context) (map[string]string, error) {
118 s := analyticsSettings{}
119 err := settings.GetUncached(c, settingsKey, &s)
120 if err != nil && err != settings.ErrNoSettings {
121 return nil, err
122 }
123 return map[string]string{
124 "AnalyticsID": s.AnalyticsID,
125 }, nil
126 }
127
128 func (settingsUIPage) WriteSettings(c context.Context, values map[string]string, who, why string) error {
129 modified := analyticsSettings{}
130 modified.AnalyticsID = values["AnalyticsID"]
Vadim Sh. 2016/08/26 23:12:00 might as well do validation against regexp here to
Ryan Tseng 2016/08/29 19:21:40 Done.
131
132 return settings.SetIfChanged(c, settingsKey, &modified, who, why)
133 }
134
135 func init() {
136 settings.RegisterUIPage(settingsKey, settingsUIPage{})
137 }
OLDNEW
« no previous file with comments | « server/analytics/doc.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698