| 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 #ifndef COMPONENTS_NTP_SNIPPETS_USER_CLASSIFIER_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_USER_CLASSIFIER_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_USER_CLASSIFIER_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_USER_CLASSIFIER_H_ |
| 7 | 7 |
| 8 #include <string> |
| 9 |
| 8 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/time/time.h" |
| 9 | 12 |
| 10 class PrefRegistrySimple; | 13 class PrefRegistrySimple; |
| 11 class PrefService; | 14 class PrefService; |
| 12 | 15 |
| 13 namespace ntp_snippets { | 16 namespace ntp_snippets { |
| 14 | 17 |
| 15 // Collects data about user usage patterns of content suggestions, computes | 18 // Collects data about user usage patterns of content suggestions, computes |
| 16 // long-term user metrics locally using pref, and reports the metrics to UMA. | 19 // long-term user metrics locally using pref, and reports the metrics to UMA. |
| 17 // TODO(jkrcal): Add classification of users based on the metrics and getters | 20 // Based on these lon-term user metrics, it classifies the user in a UserClass. |
| 18 // for the classification as well as for the metrics. | |
| 19 class UserClassifier { | 21 class UserClassifier { |
| 20 public: | 22 public: |
| 23 // Enumeration listing user classes |
| 24 enum class UserClass { |
| 25 RARE_NTP_USER, |
| 26 ACTIVE_NTP_USER, |
| 27 ACTIVE_SUGGESTIONS_CONSUMER, |
| 28 }; |
| 29 |
| 30 // For estimating the average length of the intervals between two successive |
| 31 // events, we keep a simple frequency model, a single value that we call |
| 32 // "metric" below. |
| 33 // We track exponentially-discounted rate of the given event per hour where |
| 34 // the continuous utility function between two successive events (e.g. opening |
| 35 // a NTP) at times t1 < t2 is 1 / (t2-t1), i.e. intuitively the rate of this |
| 36 // event in this time interval. |
| 37 // See https://en.wikipedia.org/wiki/Exponential_discounting for more details. |
| 38 // We keep track of the following events. |
| 39 // NOTE: if you add any element, add it also in the static arrays in .cc and |
| 40 // create another histogram. |
| 41 enum class Metric { |
| 42 NTP_OPENED, // When the user opens a new NTP - this indicates potential |
| 43 // use of content suggestions. |
| 44 SUGGESTIONS_SHOWN, // When the content suggestions are shown to the user - |
| 45 // in the current implementation when the user scrolls |
| 46 // below the fold. |
| 47 SUGGESTIONS_USED, // When the user clicks on some suggestions or on some |
| 48 // "More" button. |
| 49 COUNT // Keep this as the last element. |
| 50 }; |
| 51 |
| 21 // The provided |pref_service| may be nullptr in unit-tests. | 52 // The provided |pref_service| may be nullptr in unit-tests. |
| 22 explicit UserClassifier(PrefService* pref_service); | 53 explicit UserClassifier(PrefService* pref_service); |
| 23 ~UserClassifier(); | 54 ~UserClassifier(); |
| 24 | 55 |
| 25 // Registers profile prefs for all metrics. Called from browser_prefs.cc. | 56 // Registers profile prefs for all metrics. Called from browser_prefs.cc. |
| 26 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 57 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 27 | 58 |
| 28 // When the user opens a new NTP - this indicates potential use of content | 59 // Informs the UserClassifier about a new event for |metric|. The |
| 29 // suggestions. | 60 // classification is based on these calls. |
| 30 void OnNTPOpened(); | 61 void OnEvent(Metric metric); |
| 31 | 62 |
| 32 // When the content suggestions are shown to the user - in the current | 63 // Get the estimate average length of the interval between two successive |
| 33 // implementation when the user scrolls below the fold. | 64 // events of the given type. |
| 34 void OnSuggestionsShown(); | 65 double GetEstimatedAvgTime(Metric metric) const; |
| 35 | 66 |
| 36 // When the user clicks on some suggestions or on some "More" button. | 67 // Return the classification of the current user. |
| 37 void OnSuggestionsUsed(); | 68 UserClass GetUserClass() const; |
| 69 std::string GetUserClassDescriptionForDebugging() const; |
| 70 |
| 71 // Resets the classification (emulates a fresh upgrade / install). |
| 72 void ClearClassificationForDebugging(); |
| 38 | 73 |
| 39 private: | 74 private: |
| 40 // The event has happened, recompute and store the metric accordingly. | 75 // The event has happened, recompute the metric accordingly. Then store and |
| 41 void UpdateMetricOnEvent(const char* metric_pref_name, | 76 // return the new value. |
| 42 const char* last_time_pref_name); | 77 double UpdateMetricOnEvent(Metric metric); |
| 78 // No event has happened but we need to get up-to-date metric, recompute and |
| 79 // return the new value. This function does not store the recomputed metric. |
| 80 double GetUpToDateMetricValue(Metric metric) const; |
| 43 | 81 |
| 44 // Compute the number of hours between two events for the given metric value | 82 // Returns the number of hours since the last event of the same type. |
| 45 // assuming the events were equally distributed. | 83 // If there is no last event of that type, assume it happened just now and |
| 46 double GetEstimateHoursBetweenEvents(const char* metric_pref_name); | 84 // return 0. |
| 85 double GetHoursSinceLastTime(Metric metric) const; |
| 86 bool HasLastTime(Metric metric) const; |
| 87 void SetLastTimeToNow(Metric metric); |
| 47 | 88 |
| 48 // Returns the number of hours since the last event of the same type or | 89 double GetMetricValue(Metric metric) const; |
| 49 // DBL_MAX if there is no last event of that type. | 90 void SetMetricValue(Metric metric, double metric_value); |
| 50 double GetHoursSinceLastTime(const char* last_time_pref_name); | 91 void ClearMetricValue(Metric metric); |
| 51 void SetLastTimeToNow(const char* last_time_pref_name); | |
| 52 | 92 |
| 53 PrefService* pref_service_; | 93 PrefService* pref_service_; |
| 54 double const discount_rate_per_hour_; | 94 const double discount_rate_per_hour_; |
| 55 | 95 |
| 56 DISALLOW_COPY_AND_ASSIGN(UserClassifier); | 96 DISALLOW_COPY_AND_ASSIGN(UserClassifier); |
| 57 }; | 97 }; |
| 58 | 98 |
| 59 } // namespace ntp_snippets | 99 } // namespace ntp_snippets |
| 60 | 100 |
| 61 #endif // COMPONENTS_NTP_SNIPPETS_USER_CLASSIFIER_H_ | 101 #endif // COMPONENTS_NTP_SNIPPETS_USER_CLASSIFIER_H_ |
| OLD | NEW |