Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Provides aggregation of feature usage by tracking impressions and clicks | |
|
marq (ping after 24h)
2016/08/30 08:30:36
Define "impression" and "click" in this context ("
Donn Denman
2016/08/31 04:35:20
Done.
| |
| 6 // over 1-week and 28-day intervals. | |
| 7 // Used by Contextual Search to record impressions of the Bar and CTR of | |
|
marq (ping after 24h)
2016/08/30 08:30:36
Define "CTR".
Donn Denman
2016/08/31 04:35:21
Done.
| |
| 8 // panel opens to use as signals for Tap triggering. | |
| 9 | |
| 10 #ifndef COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ | |
| 11 #define COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ | |
| 12 | |
| 13 #include <string> | |
| 14 | |
| 15 #include "base/gtest_prod_util.h" | |
| 16 #include "base/macros.h" | |
| 17 #include "components/contextual_search/browser/device_int_storage.h" | |
| 18 | |
| 19 namespace contextual_search { | |
| 20 | |
| 21 // Usage: Create a CTRAggregator and start recording impressions or reading | |
| 22 // aggregated data. Get data from the previous week or previous 4-week period | |
|
marq (ping after 24h)
2016/08/30 08:30:36
How is this data used? Does it leave the device? H
Donn Denman
2016/08/31 04:35:20
Done.
| |
| 23 // that ended with the previous week. | |
| 24 // A new week starts at an arbitrary time based on seconds since the Epoch. | |
| 25 // The data from the previous week and previous 28-day period are guaranteed to | |
| 26 // be complete only if the HasPrevious method returns true. If one of the data | |
| 27 // accessors is called when the data is not complete the Impressions will return | |
| 28 // data for only the partial period, and the CTR method will return NAN. | |
| 29 class CTRAggregator { | |
| 30 public: | |
| 31 CTRAggregator(DeviceIntStorage& storage); | |
| 32 ~CTRAggregator(); | |
| 33 | |
| 34 // Record an impression. Records a click if |did_click| is true. | |
|
marq (ping after 24h)
2016/08/30 08:30:36
s/Record/Records/
Donn Denman
2016/08/31 04:35:21
Done.
| |
| 35 void RecordImpression(bool did_click); | |
| 36 | |
| 37 // Returns whether we have the previous week's data for this user. | |
| 38 bool HasPreviousWeekData(); | |
| 39 | |
| 40 // Gets the number of impressions from the previous week. | |
| 41 int GetPreviousWeekImpressions(); | |
| 42 | |
| 43 // Gets the CTR from the previous week. | |
| 44 // Returns NAN if there is no previous week's data for this user. | |
| 45 float GetPreviousWeekCTR(); | |
| 46 | |
| 47 // Returns whether we have data from a 28 day period ending in the previous | |
| 48 // week. | |
| 49 bool HasPrevious28DayData(); | |
| 50 | |
| 51 // Gets the number of impressions from a 28 day period ending in the previous | |
| 52 // week. | |
| 53 int GetPrevious28DayImpressions(); | |
| 54 | |
| 55 // Gets the CTR from a 28 day period ending in the previous week. | |
| 56 // Returns NAN if there is no data for that period for this user. | |
| 57 float GetPrevious28DayCTR(); | |
| 58 | |
| 59 private: | |
| 60 // This implementation uses a fixed number of bins to store integer impression | |
| 61 // and click data for the most recent N weeks, where N = 5 (in order to keep 4 | |
| 62 // complete weeks). Another bin keeps track of the current week being | |
| 63 // written. Yet another bin records when data was first stored or accessed so | |
| 64 // we can know when a time period has complete data. | |
| 65 friend class CTRAggregatorTest; | |
| 66 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SimpleOperationTest); | |
| 67 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, MultiWeekTest); | |
| 68 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SkipOneWeekTest); | |
| 69 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SkipThreeWeeksTest); | |
| 70 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SkipFourWeeksTest); | |
| 71 | |
| 72 // Constructor for testing; sets the week. | |
| 73 CTRAggregator(DeviceIntStorage& storage, int week_number); | |
| 74 // For testing, increment the current week number by |weeks|. | |
| 75 void IncrementWeek(int weeks); | |
| 76 | |
| 77 // Ensure that the week number of the first week ever used is recorded. | |
|
marq (ping after 24h)
2016/08/30 08:30:36
Ensures.
Donn Denman
2016/08/31 04:35:21
Done.
| |
| 78 // This should be called at least once through every public API code path. | |
| 79 void EnsureFirstWeekRecorded(); | |
| 80 // Updates storage of skipped weeks when advancing to a new current week. | |
| 81 void UpdateStorageToCurrentWeek(); | |
| 82 | |
| 83 // Returns the string key of the storage bin for the given week |which_week|. | |
| 84 std::string GetWeekKey(int which_week); | |
| 85 // Returns the string key for the "clicks" storage bin for the given week | |
| 86 // |which_week|. | |
| 87 std::string GetWeekClicksKey(int which_week); | |
| 88 // Returns the string key for the "impressions" storage bin for the given week | |
| 89 // |which_week|. | |
| 90 std::string GetWeekImpressionsKey(int which_week); | |
| 91 | |
| 92 // Gets the number of clicks from the previous week. | |
| 93 int GetPreviousWeekClicks(); | |
| 94 // Gets the number of clicks from a 28 day period ending in the previous | |
| 95 // week. | |
| 96 int GetPrevious28DayClicks(); | |
| 97 | |
| 98 // Increments the value in the storage bin with the given |key|. | |
| 99 void Increment(std::string key); | |
| 100 | |
| 101 // Cached storage accessors: | |
| 102 // Reads and returns an int keyed by |storage_key|. | |
| 103 int ReadInt(std::string storage_key); | |
| 104 // Writes the given int |value| keyed by |storage_key|. | |
| 105 void WriteInt(std::string storage_key, int value); | |
| 106 | |
| 107 // Persistently stores the CTR data. | |
| 108 DeviceIntStorage& storage_; | |
| 109 int week_number_; | |
| 110 | |
| 111 DISALLOW_COPY_AND_ASSIGN(CTRAggregator); | |
| 112 }; | |
| 113 | |
| 114 } // namespace contextual_search | |
| 115 | |
| 116 #endif // COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ | |
| OLD | NEW |