| OLD | NEW |
| (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 } |
| OLD | NEW |