| 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 |
| 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 |
| 8 // Through Rate. |
| 9 // Used by Contextual Search to record impressions of the Bar and CTR of |
| 10 // panel opens to use as signals for Tap triggering. |
| 11 |
| 12 #ifndef COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ |
| 13 #define COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ |
| 14 |
| 15 #include <string> |
| 16 |
| 17 #include "base/gtest_prod_util.h" |
| 18 #include "base/macros.h" |
| 19 #include "components/contextual_search/browser/weekly_activity_storage.h" |
| 20 |
| 21 namespace contextual_search { |
| 22 |
| 23 // Number of weeks of data needed for 28 days. |
| 24 const int kNumWeeksNeededFor28DayData = 4; |
| 25 |
| 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 |
| 28 // that ended with the previous week. |
| 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 |
| 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 |
| 33 // returned. |
| 34 class CTRAggregator { |
| 35 public: |
| 36 // Constructs a CTRAggregator using the given |storage| mechanism. |
| 37 // Data is stored by |storage| typically on persistent device-local storage. |
| 38 // A callback through the storage interface may occur at construction time, |
| 39 // so the |storage| must be fully initialized when this constructor is |
| 40 // called. |
| 41 CTRAggregator(WeeklyActivityStorage& storage); |
| 42 ~CTRAggregator(); |
| 43 |
| 44 // Records an impression. Records a click if |did_click| is true. |
| 45 void RecordImpression(bool did_click); |
| 46 |
| 47 // Returns whether we have the previous week's data for this user. |
| 48 bool HasPreviousWeekData(); |
| 49 |
| 50 // Gets the number of impressions from the previous week. |
| 51 // Callers must check if there is previous week's data for this user, or |
| 52 // invalid data may be returned. |
| 53 int GetPreviousWeekImpressions(); |
| 54 |
| 55 // Gets the CTR from the previous week. |
| 56 // Callers must check if there is previous week's data for this user, or |
| 57 // invalid data may be returned. |
| 58 float GetPreviousWeekCTR(); |
| 59 |
| 60 // Returns whether we have data from a 28 day period ending in the previous |
| 61 // week. |
| 62 bool HasPrevious28DayData(); |
| 63 |
| 64 // Gets the number of impressions from a 28 day period ending in the previous |
| 65 // week. |
| 66 // Callers must check if there is previous 28 day data for this user, or |
| 67 // invalid data may be returned. |
| 68 int GetPrevious28DayImpressions(); |
| 69 |
| 70 // 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 |
| 72 // invalid data may be returned. |
| 73 float GetPrevious28DayCTR(); |
| 74 |
| 75 private: |
| 76 // 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 |
| 78 // 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 |
| 80 // we can know when a time period has complete data. |
| 81 friend class CTRAggregatorTest; |
| 82 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SimpleOperationTest); |
| 83 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, MultiWeekTest); |
| 84 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SkipOneWeekTest); |
| 85 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SkipThreeWeeksTest); |
| 86 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SkipFourWeeksTest); |
| 87 |
| 88 // Constructs an instance for testing; sets the week. |
| 89 CTRAggregator(WeeklyActivityStorage& storage, int week_number); |
| 90 // For testing, increments the current week number by |weeks|. |
| 91 void IncrementWeek(int weeks); |
| 92 |
| 93 // Gets the number of clicks from the previous week. |
| 94 // Callers must check if there is previous week's data for this user, or |
| 95 // invalid data may be returned. |
| 96 int GetPreviousWeekClicks(); |
| 97 // Gets the number of clicks from a 28 day period ending in the previous |
| 98 // week. |
| 99 // Callers must check if there is previous 28 day data for this user, or |
| 100 // invalid data may be returned. |
| 101 int GetPrevious28DayClicks(); |
| 102 |
| 103 // Stores the weekly activity data. |
| 104 WeeklyActivityStorage& storage_; |
| 105 |
| 106 // The current week number, expressed as the number of weeks since Epoch. |
| 107 int week_number_; |
| 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(CTRAggregator); |
| 110 }; |
| 111 |
| 112 } // namespace contextual_search |
| 113 |
| 114 #endif // COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ |
| OLD | NEW |