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

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: Review 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.
Vadim Sh. 2016/08/29 19:26:32 nit: move ID() and Snippet() into analytics/analyt
Ryan Tseng 2016/08/29 19:36:01 Done.
55 func ID(c context.Context) string {
56 return fetchCachedSettings(c).AnalyticsID
57 }
58
59 var rAllowed = regexp.MustCompile("UA-\\d+-\\d+")
60
61 // Snippet returns the html snippet for Google Analytics, including the
62 // <script> tag and ID, if ID is set.
63 func Snippet(c context.Context) template.HTML {
64 id := ID(c)
65 if id == "" {
66 return ""
67 }
68 if !rAllowed.MatchString(id) {
69 logging.Errorf(c, "Analytics ID %s does not match UA-\\d+-\\d+", id)
70 return ""
71 }
72 return template.HTML(fmt.Sprintf(`
73 <script>
74 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||functio n(){
75 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createEleme nt(o),
76 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefo re(a,m)
77 })(window,document,'script','https://www.google-analytics.com/analytics. js','ga');
78
79 ga('create', '%s', 'auto');
80 ga('send', 'pageview');
81 </script>
82 `, id))
83 }
84
85 ////////////////////////////////////////////////////////////////////////////////
86 // UI for GAE settings.
87
88 type settingsUIPage struct {
89 settings.BaseUIPage
90 }
91
92 func (settingsUIPage) Title(c context.Context) (string, error) {
93 return "Google Analytics Related Settings", nil
94 }
95
96 func (settingsUIPage) Overview(c context.Context) (template.HTML, error) {
97 return template.HTML(`<p>To generate a Google Analytics Tracking ID</p>
98 <ul>
99 <li> Sign in to <a href="https://www.google.com/analytics/web/#home/">your Analy tics account.</a></li>
100 <li>Select the Admin tab.</li>
101 <li>Select an account from the drop-down menu in the <i>ACCOUNT</i> column.</li>
102 <li>Select a property from the drop-down menu in the <i>PROPERTY</i> column.</li >
103 <li>Under <i>PROPERTY</i>, click <b>Tracking Info > Tracking Code.</b></li>
104 </ul>`), nil
105 }
106
107 func (settingsUIPage) Fields(c context.Context) ([]settings.UIField, error) {
108 return []settings.UIField{
109 {
110 ID: "AnalyticsID",
111 Title: "Google Analytics Tracking ID",
112 Type: settings.UIFieldText,
113 Help: `Tracking ID used for Google Analytics. Filling th is in enables
114 Google Analytics tracking across the app.`,
115 },
116 }, nil
117 }
118
119 func (settingsUIPage) ReadSettings(c context.Context) (map[string]string, error) {
120 s := analyticsSettings{}
121 err := settings.GetUncached(c, settingsKey, &s)
122 if err != nil && err != settings.ErrNoSettings {
123 return nil, err
124 }
125 return map[string]string{
126 "AnalyticsID": s.AnalyticsID,
127 }, nil
128 }
129
130 func (settingsUIPage) WriteSettings(c context.Context, values map[string]string, who, why string) error {
131 modified := analyticsSettings{}
132 id := values["AnalyticsID"]
133 if id != "" {
134 if !rAllowed.MatchString(id) {
135 return fmt.Errorf("Analytics ID %s does not match format UA-\\d+-\\d+", id)
136 }
137 modified.AnalyticsID = id
138 }
139
140 return settings.SetIfChanged(c, settingsKey, &modified, who, why)
141 }
142
143 func init() {
144 settings.RegisterUIPage(settingsKey, settingsUIPage{})
145 }
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