OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Provides aggregation of feature usage by tracking impressions and clicks | 5 // Provides aggregation of feature usage by tracking impressions and clicks |
6 // over 1-week and 28-day intervals. Impressions are views of some UX and | 6 // over 1-week and 28-day intervals. Impressions are views of some UX and |
7 // clicks are any measured interaction with that UX, yielding CTR -- Click | 7 // clicks are any measured interaction with that UX, yielding CTR -- Click |
8 // Through Rate. | 8 // Through Rate. |
9 // Used by Contextual Search to record impressions of the Bar and CTR of | 9 // Used by Contextual Search to record impressions of the Bar and CTR of |
10 // panel opens to use as signals for Tap triggering. | 10 // panel opens to use as signals for Tap triggering. |
11 | 11 |
12 #ifndef COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ | 12 #ifndef COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ |
13 #define COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ | 13 #define COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ |
14 | 14 |
15 #include <string> | 15 #include <string> |
16 | 16 |
17 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "components/contextual_search/browser/weekly_activity_storage.h" | 19 #include "components/contextual_search/browser/weekly_activity_storage.h" |
20 | 20 |
21 namespace contextual_search { | 21 namespace contextual_search { |
22 | 22 |
23 // Number of weeks of data needed for 28 days. | 23 // Number of weeks of data needed for 28 days. |
24 const int kNumWeeksNeededFor28DayData = 4; | 24 const int kNumWeeksNeededFor28DayData = 4; |
25 | 25 |
26 // Usage: Create a CTRAggregator and start recording impressions or reading | 26 // Usage: Create a CtrAggregator and start recording impressions or reading |
27 // aggregated data. Get data from the previous week or previous 4-week period | 27 // aggregated data. Get data from the previous week or previous 4-week period |
28 // that ended with the previous week. | 28 // that ended with the previous week. |
29 // A new week starts at an arbitrary time based on seconds since the Epoch. | 29 // A new week starts at an arbitrary time based on seconds since the Epoch. |
30 // The data from the previous week and previous 28-day period are guaranteed to | 30 // The data from the previous week and previous 28-day period are guaranteed to |
31 // be complete only if the HasPrevious method returns true. If one of the data | 31 // be complete only if the HasPrevious method returns true. If one of the data |
32 // accessors is called when the data is not complete invalid data may be | 32 // accessors is called when the data is not complete invalid data may be |
33 // returned. | 33 // returned. |
34 class CTRAggregator { | 34 class CtrAggregator { |
35 public: | 35 public: |
36 // Constructs a CTRAggregator using the given |storage| mechanism. | 36 // Constructs a CtrAggregator using the given |storage| mechanism. |
37 // Data is stored by |storage| typically on persistent device-local storage. | 37 // Data is stored by |storage| typically on persistent device-local storage. |
38 // A callback through the storage interface may occur at construction time, | 38 // A callback through the storage interface may occur at construction time, |
39 // so the |storage| must be fully initialized when this constructor is | 39 // so the |storage| must be fully initialized when this constructor is |
40 // called. | 40 // called. |
41 CTRAggregator(WeeklyActivityStorage& storage); | 41 CtrAggregator(WeeklyActivityStorage& storage); |
42 ~CTRAggregator(); | 42 ~CtrAggregator(); |
43 | 43 |
44 // Records an impression. Records a click if |did_click| is true. | 44 // Records an impression. Records a click if |did_click| is true. |
45 void RecordImpression(bool did_click); | 45 void RecordImpression(bool did_click); |
46 | 46 |
| 47 // Returns the number for the current week. Useful for checking when the |
| 48 // current week changes. |
| 49 int GetCurrentWeekNumber(); |
| 50 |
47 // Returns whether we have the previous week's data for this user. | 51 // Returns whether we have the previous week's data for this user. |
48 bool HasPreviousWeekData(); | 52 bool HasPreviousWeekData(); |
49 | 53 |
50 // Gets the number of impressions from the previous week. | 54 // Gets the number of impressions from the previous week. |
51 // Callers must check if there is previous week's data for this user, or | 55 // Callers must check if there is previous week's data for this user, or |
52 // invalid data may be returned. | 56 // invalid data may be returned. |
53 int GetPreviousWeekImpressions(); | 57 int GetPreviousWeekImpressions(); |
54 | 58 |
55 // Gets the CTR from the previous week. | 59 // Gets the CTR from the previous week. |
56 // Callers must check if there is previous week's data for this user, or | 60 // Callers must check if there is previous week's data for this user, or |
57 // invalid data may be returned. | 61 // invalid data may be returned. |
58 float GetPreviousWeekCTR(); | 62 float GetPreviousWeekCtr(); |
59 | 63 |
60 // Returns whether we have data from a 28 day period ending in the previous | 64 // Returns whether we have data from a 28 day period ending in the previous |
61 // week. | 65 // week. |
62 bool HasPrevious28DayData(); | 66 bool HasPrevious28DayData(); |
63 | 67 |
64 // Gets the number of impressions from a 28 day period ending in the previous | 68 // Gets the number of impressions from a 28 day period ending in the previous |
65 // week. | 69 // week. |
66 // Callers must check if there is previous 28 day data for this user, or | 70 // Callers must check if there is previous 28 day data for this user, or |
67 // invalid data may be returned. | 71 // invalid data may be returned. |
68 int GetPrevious28DayImpressions(); | 72 int GetPrevious28DayImpressions(); |
69 | 73 |
70 // Gets the CTR from a 28 day period ending in the previous week. | 74 // Gets the CTR from a 28 day period ending in the previous week. |
71 // Callers must check if there is previous 28 day data for this user, or | 75 // Callers must check if there is previous 28 day data for this user, or |
72 // invalid data may be returned. | 76 // invalid data may be returned. |
73 float GetPrevious28DayCTR(); | 77 float GetPrevious28DayCtr(); |
74 | 78 |
75 private: | 79 private: |
76 // This implementation uses a fixed number of bins to store integer impression | 80 // This implementation uses a fixed number of bins to store integer impression |
77 // and click data for the most recent N weeks, where N = 5 (in order to keep 4 | 81 // and click data for the most recent N weeks, where N = 5 (in order to keep 4 |
78 // complete weeks). Another bin keeps track of the current week being | 82 // complete weeks). Another bin keeps track of the current week being |
79 // written. Yet another bin records when data was first stored or accessed so | 83 // written. Yet another bin records when data was first stored or accessed so |
80 // we can know when a time period has complete data. | 84 // we can know when a time period has complete data. |
81 friend class CTRAggregatorTest; | 85 friend class CtrAggregatorTest; |
82 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SimpleOperationTest); | 86 FRIEND_TEST_ALL_PREFIXES(CtrAggregatorTest, SimpleOperationTest); |
83 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, MultiWeekTest); | 87 FRIEND_TEST_ALL_PREFIXES(CtrAggregatorTest, MultiWeekTest); |
84 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SkipOneWeekTest); | 88 FRIEND_TEST_ALL_PREFIXES(CtrAggregatorTest, SkipOneWeekTest); |
85 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SkipThreeWeeksTest); | 89 FRIEND_TEST_ALL_PREFIXES(CtrAggregatorTest, SkipThreeWeeksTest); |
86 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SkipFourWeeksTest); | 90 FRIEND_TEST_ALL_PREFIXES(CtrAggregatorTest, SkipFourWeeksTest); |
87 | 91 |
88 // Constructs an instance for testing; sets the week. | 92 // Constructs an instance for testing; sets the week. |
89 CTRAggregator(WeeklyActivityStorage& storage, int week_number); | 93 CtrAggregator(WeeklyActivityStorage& storage, int week_number); |
90 // For testing, increments the current week number by |weeks|. | 94 // For testing, increments the current week number by |weeks|. |
91 void IncrementWeek(int weeks); | 95 void IncrementWeek(int weeks); |
92 | 96 |
93 // Gets the number of clicks from the previous week. | 97 // Gets the number of clicks from the previous week. |
94 // Callers must check if there is previous week's data for this user, or | 98 // Callers must check if there is previous week's data for this user, or |
95 // invalid data may be returned. | 99 // invalid data may be returned. |
96 int GetPreviousWeekClicks(); | 100 int GetPreviousWeekClicks(); |
97 // Gets the number of clicks from a 28 day period ending in the previous | 101 // Gets the number of clicks from a 28 day period ending in the previous |
98 // week. | 102 // week. |
99 // Callers must check if there is previous 28 day data for this user, or | 103 // Callers must check if there is previous 28 day data for this user, or |
100 // invalid data may be returned. | 104 // invalid data may be returned. |
101 int GetPrevious28DayClicks(); | 105 int GetPrevious28DayClicks(); |
102 | 106 |
103 // Stores the weekly activity data. | 107 // Stores the weekly activity data. |
104 WeeklyActivityStorage& storage_; | 108 WeeklyActivityStorage& storage_; |
105 | 109 |
106 // The current week number, expressed as the number of weeks since Epoch. | 110 // The current week number, expressed as the number of weeks since Epoch. |
107 int week_number_; | 111 int week_number_; |
108 | 112 |
109 DISALLOW_COPY_AND_ASSIGN(CTRAggregator); | 113 DISALLOW_COPY_AND_ASSIGN(CtrAggregator); |
110 }; | 114 }; |
111 | 115 |
112 } // namespace contextual_search | 116 } // namespace contextual_search |
113 | 117 |
114 #endif // COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ | 118 #endif // COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ |
OLD | NEW |