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

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: 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 | « 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/server/settings"
15 )
16
17 // settingsKey is key for global GAE settings (described by analyticsSettings st ruct)
18 // in the settings store. See github.com/luci/luci-go/server/settings.
19 const settingsKey = "analytics"
20
21 // analyticsSettings contain settings to enable Google Analytics.
22 type analyticsSettings struct {
23 // AnalyticsID is a Google Analytics ID an admin can set to enable Analy tics.
24 // The app must support analytics for this to work.
25 AnalyticsID string `json:"analytics_id"`
26 }
27
28 // fetchCachedSettings fetches analyticsSettings from the settings store or pani cs.
29 //
30 // Uses in-process global cache to avoid hitting datastore often. The cache
31 // expiration time is 1 min (see analyticsSettings.expirationTime), meaning
32 // the instance will refetch settings once a minute (blocking only one unlucky
33 // request to do so).
34 //
35 // Panics only if there's no cached value (i.e. it is the first call to this
36 // function in this process ever) and datastore operation fails. It is a good
37 // idea to implement /_ah/warmup to warm this up.
38 func fetchCachedSettings(c context.Context) analyticsSettings {
39 s := analyticsSettings{}
40 switch err := settings.Get(c, settingsKey, &s); {
41 case err == nil:
42 return s
43 case err == settings.ErrNoSettings:
44 // Defaults.
45 return analyticsSettings{
46 AnalyticsID: "",
47 }
48 default:
49 panic(fmt.Errorf("could not fetch GAE settings - %s", err))
50 }
51 }
52
53 var rAllowed = regexp.MustCompile("UA-\\d+-\\d+")
54
55 ////////////////////////////////////////////////////////////////////////////////
56 // UI for GAE settings.
57
58 type settingsUIPage struct {
59 settings.BaseUIPage
60 }
61
62 func (settingsUIPage) Title(c context.Context) (string, error) {
63 return "Google Analytics Related Settings", nil
64 }
65
66 func (settingsUIPage) Overview(c context.Context) (template.HTML, error) {
67 return template.HTML(`<p>To generate a Google Analytics Tracking ID</p>
68 <ul>
69 <li> Sign in to <a href="https://www.google.com/analytics/web/#home/">your Analy tics account.</a></li>
70 <li>Select the Admin tab.</li>
71 <li>Select an account from the drop-down menu in the <i>ACCOUNT</i> column.</li>
72 <li>Select a property from the drop-down menu in the <i>PROPERTY</i> column.</li >
73 <li>Under <i>PROPERTY</i>, click <b>Tracking Info > Tracking Code.</b></li>
74 </ul>`), nil
75 }
76
77 func (settingsUIPage) Fields(c context.Context) ([]settings.UIField, error) {
78 return []settings.UIField{
79 {
80 ID: "AnalyticsID",
81 Title: "Google Analytics Tracking ID",
82 Type: settings.UIFieldText,
83 Help: `Tracking ID used for Google Analytics. Filling th is in enables
84 Google Analytics tracking across the app.`,
85 },
86 }, nil
87 }
88
89 func (settingsUIPage) ReadSettings(c context.Context) (map[string]string, error) {
90 s := analyticsSettings{}
91 err := settings.GetUncached(c, settingsKey, &s)
92 if err != nil && err != settings.ErrNoSettings {
93 return nil, err
94 }
95 return map[string]string{
96 "AnalyticsID": s.AnalyticsID,
97 }, nil
98 }
99
100 func (settingsUIPage) WriteSettings(c context.Context, values map[string]string, who, why string) error {
101 modified := analyticsSettings{}
102 id := values["AnalyticsID"]
103 if id != "" {
104 if !rAllowed.MatchString(id) {
105 return fmt.Errorf("Analytics ID %s does not match format UA-\\d+-\\d+", id)
106 }
107 modified.AnalyticsID = id
108 }
109
110 return settings.SetIfChanged(c, settingsKey, &modified, who, why)
111 }
112
113 func init() {
114 settings.RegisterUIPage(settingsKey, settingsUIPage{})
115 }
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