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