Skip to content

Commit 444639a

Browse files
kkuehlzbsclifton
authored andcommitted
stats: Add dtoi param to usage ping
Resolves brave/brave-browser#10061
1 parent ffd1b85 commit 444639a

7 files changed

+141
-4
lines changed

browser/brave_stats_updater.cc

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ GURL GetUpdateURL(const GURL& base_update_url,
5656
update_url, "first", stats_updater_params.GetFirstCheckMadeParam());
5757
update_url = net::AppendQueryParameter(
5858
update_url, "woi", stats_updater_params.GetWeekOfInstallationParam());
59+
update_url = net::AppendQueryParameter(
60+
update_url, "dtoi", stats_updater_params.GetDateOfInstallationParam());
5961
update_url = net::AppendQueryParameter(
6062
update_url, "ref", stats_updater_params.GetReferralCodeParam());
6163
return update_url;

browser/brave_stats_updater_browsertest.cc

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
#include "base/environment.h"
77
#include "base/path_service.h"
8+
#include "base/time/time.h"
89
#include "brave/browser/brave_stats_updater.h"
10+
#include "brave/browser/brave_stats_updater_params.h"
911
#include "brave/common/pref_names.h"
1012
#include "brave/components/brave_referrals/browser/brave_referrals_service.h"
1113
#include "chrome/browser/ui/browser.h"

browser/brave_stats_updater_params.cc

+21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace brave {
1818

1919
base::Time BraveStatsUpdaterParams::g_current_time;
20+
bool BraveStatsUpdaterParams::g_force_first_run = false;
2021

2122
BraveStatsUpdaterParams::BraveStatsUpdaterParams(PrefService* pref_service)
2223
: BraveStatsUpdaterParams(pref_service,
@@ -58,6 +59,12 @@ std::string BraveStatsUpdaterParams::GetWeekOfInstallationParam() const {
5859
return week_of_installation_;
5960
}
6061

62+
std::string BraveStatsUpdaterParams::GetDateOfInstallationParam() const {
63+
return (GetCurrentTimeNow() - date_of_installation_ >= g_dtoi_delete_delta)
64+
? "null"
65+
: brave::GetDateAsYMD(date_of_installation_);
66+
}
67+
6168
std::string BraveStatsUpdaterParams::GetReferralCodeParam() const {
6269
return referral_promo_code_.empty() ? "none" : referral_promo_code_;
6370
}
@@ -70,6 +77,10 @@ void BraveStatsUpdaterParams::LoadPrefs() {
7077
week_of_installation_ = pref_service_->GetString(kWeekOfInstallation);
7178
if (week_of_installation_.empty())
7279
week_of_installation_ = GetLastMondayAsYMD();
80+
date_of_installation_ = brave::GetFirstRunTime(pref_service_);
81+
DCHECK(!date_of_installation_.is_null());
82+
if (ShouldForceFirstRun())
83+
date_of_installation_ = GetCurrentTimeNow();
7384
#if BUILDFLAG(ENABLE_BRAVE_REFERRALS)
7485
referral_promo_code_ = pref_service_->GetString(kReferralPromoCode);
7586
#endif
@@ -119,10 +130,20 @@ base::Time BraveStatsUpdaterParams::GetCurrentTimeNow() const {
119130
return g_current_time.is_null() ? base::Time::Now() : g_current_time;
120131
}
121132

133+
// static
134+
bool BraveStatsUpdaterParams::ShouldForceFirstRun() const {
135+
return g_force_first_run;
136+
}
137+
122138
// static
123139
void BraveStatsUpdaterParams::SetCurrentTimeForTest(
124140
const base::Time& current_time) {
125141
g_current_time = current_time;
126142
}
127143

144+
// static
145+
void BraveStatsUpdaterParams::SetFirstRunForTest(bool first_run) {
146+
g_force_first_run = first_run;
147+
}
148+
128149
} // namespace brave

browser/brave_stats_updater_params.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
99
#include <string>
1010

1111
#include "base/macros.h"
12+
#include "base/time/time.h"
1213

1314
class BraveStatsUpdaterTest;
1415
class PrefService;
1516

16-
namespace base {
17-
class Time;
18-
}
19-
2017
namespace brave {
2118

2219
class BraveStatsUpdaterParams {
@@ -33,6 +30,7 @@ class BraveStatsUpdaterParams {
3330
std::string GetMonthlyParam() const;
3431
std::string GetFirstCheckMadeParam() const;
3532
std::string GetWeekOfInstallationParam() const;
33+
std::string GetDateOfInstallationParam() const;
3634
std::string GetReferralCodeParam() const;
3735

3836
void SavePrefs();
@@ -48,8 +46,12 @@ class BraveStatsUpdaterParams {
4846
int last_check_month_;
4947
bool first_check_made_;
5048
std::string week_of_installation_;
49+
base::Time date_of_installation_;
5150
std::string referral_promo_code_;
5251
static base::Time g_current_time;
52+
static bool g_force_first_run;
53+
static constexpr base::TimeDelta g_dtoi_delete_delta =
54+
base::TimeDelta::FromSeconds(14 * 24 * 60 * 60);
5355

5456
void LoadPrefs();
5557

@@ -61,8 +63,10 @@ class BraveStatsUpdaterParams {
6163
int GetCurrentMonth() const;
6264
int GetCurrentISOWeekNumber() const;
6365
base::Time GetCurrentTimeNow() const;
66+
bool ShouldForceFirstRun() const;
6467

6568
static void SetCurrentTimeForTest(const base::Time& current_time);
69+
static void SetFirstRunForTest(bool first_run);
6670

6771
DISALLOW_COPY_AND_ASSIGN(BraveStatsUpdaterParams);
6872
};

browser/brave_stats_updater_unittest.cc

+81
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class BraveStatsUpdaterTest: public testing::Test {
4444
brave::BraveStatsUpdaterParams::SetCurrentTimeForTest(current_time);
4545
}
4646

47+
void SetFirstRunForTest(bool first_run) {
48+
brave::BraveStatsUpdaterParams::SetFirstRunForTest(first_run);
49+
}
50+
4751
private:
4852
TestingPrefServiceSimple testing_local_state_;
4953
};
@@ -148,6 +152,83 @@ TEST_F(BraveStatsUpdaterTest, IsMonthlyUpdateNeededLastCheckedNextMonth) {
148152
ASSERT_EQ(GetLocalState()->GetInteger(kLastCheckMonth), kThisMonth);
149153
}
150154

155+
TEST_F(BraveStatsUpdaterTest, HasDateOfInstallationFirstRun) {
156+
base::Time::Exploded exploded;
157+
base::Time current_time;
158+
159+
// Set date to 2018-11-04 (ISO week #44)
160+
exploded.hour = 0;
161+
exploded.minute = 0;
162+
exploded.second = 0;
163+
exploded.millisecond = 0;
164+
exploded.day_of_week = 0;
165+
exploded.year = 2018;
166+
exploded.month = 11;
167+
exploded.day_of_month = 4;
168+
169+
ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &current_time));
170+
SetCurrentTimeForTest(current_time);
171+
SetFirstRunForTest(true);
172+
173+
brave::BraveStatsUpdaterParams brave_stats_updater_params(
174+
GetLocalState(), kToday, kThisWeek, kThisMonth);
175+
ASSERT_EQ(brave_stats_updater_params.GetDateOfInstallationParam(),
176+
"2018-11-04");
177+
}
178+
179+
TEST_F(BraveStatsUpdaterTest, HasDailyRetention) {
180+
base::Time::Exploded exploded;
181+
base::Time current_time, dtoi_time;
182+
183+
// Set date to 2018-11-04
184+
exploded.hour = 0;
185+
exploded.minute = 0;
186+
exploded.second = 0;
187+
exploded.millisecond = 0;
188+
exploded.day_of_week = 0;
189+
exploded.year = 2018;
190+
exploded.month = 11;
191+
exploded.day_of_month = 4;
192+
193+
ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &dtoi_time));
194+
// Make first run date 6 days earlier (still within 14 day window)
195+
exploded.day_of_month = 10;
196+
ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &current_time));
197+
198+
SetCurrentTimeForTest(dtoi_time);
199+
brave::BraveStatsUpdaterParams brave_stats_updater_params(
200+
GetLocalState(), kToday, kThisWeek, kThisMonth);
201+
SetCurrentTimeForTest(current_time);
202+
ASSERT_EQ(brave_stats_updater_params.GetDateOfInstallationParam(),
203+
"2018-11-04");
204+
}
205+
206+
TEST_F(BraveStatsUpdaterTest, HasDailyRetentionExpiration) {
207+
base::Time::Exploded exploded;
208+
base::Time current_time, dtoi_time;
209+
210+
// Set date to 2018-11-04
211+
exploded.hour = 0;
212+
exploded.minute = 0;
213+
exploded.second = 0;
214+
exploded.millisecond = 0;
215+
exploded.day_of_week = 0;
216+
exploded.year = 2018;
217+
exploded.month = 11;
218+
exploded.day_of_month = 4;
219+
220+
ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &dtoi_time));
221+
// Make first run date 14 days earlier (outside 14 day window)
222+
exploded.day_of_month = 18;
223+
ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &current_time));
224+
225+
SetCurrentTimeForTest(dtoi_time);
226+
brave::BraveStatsUpdaterParams brave_stats_updater_params(
227+
GetLocalState(), kToday, kThisWeek, kThisMonth);
228+
SetCurrentTimeForTest(current_time);
229+
ASSERT_EQ(brave_stats_updater_params.GetDateOfInstallationParam(), "null");
230+
}
231+
151232
// This test ensures that our weekly stats cut over on Monday
152233
TEST_F(BraveStatsUpdaterTest, IsWeeklyUpdateNeededOnMondayLastCheckedOnSunday) {
153234
base::Time::Exploded exploded;

browser/brave_stats_updater_util.cc

+24
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "base/strings/string_piece.h"
1212
#include "base/strings/string_split.h"
1313
#include "base/strings/stringprintf.h"
14+
#include "brave/common/pref_names.h"
15+
#include "chrome/browser/first_run/first_run.h"
1416

1517
namespace brave {
1618

@@ -21,6 +23,28 @@ std::string GetDateAsYMD(const base::Time& time) {
2123
exploded.day_of_month);
2224
}
2325

26+
base::Time GetFirstRunTime(PrefService *pref_service) {
27+
#if defined(OS_ANDROID)
28+
// Android doesn't use a sentinel to track first run, so we use a
29+
// preference instead. kReferralAndroidFirstRunTimestamp is used because
30+
// previously only referrals needed to know the first run value.
31+
base::Time first_run_timestamp =
32+
pref_service->GetTime(kReferralAndroidFirstRunTimestamp);
33+
if (first_run_timestamp.is_null()) {
34+
first_run_timestamp = base::Time::Now();
35+
pref_service->SetTime(kReferralAndroidFirstRunTimestamp,
36+
first_run_timestamp);
37+
}
38+
return first_run_timestamp;
39+
#else
40+
(void)pref_service; // suppress unused warning
41+
42+
// Note that CreateSentinelIfNeeded() is called in chrome_browser_main.cc,
43+
// so this will be a non-blocking read of the cached sentinel value.
44+
return first_run::GetFirstRunSentinelCreationTime();
45+
#endif // #defined(OS_ANDROID)
46+
}
47+
2448
std::string GetPlatformIdentifier() {
2549
#if defined(OS_WIN)
2650
if (base::SysInfo::OperatingSystemArchitecture() == "x86")

browser/brave_stats_updater_util.h

+3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010

1111
#include "base/time/time.h"
1212
#include "base/system/sys_info.h"
13+
#include "components/prefs/pref_service.h"
1314

1415
namespace brave {
1516

1617
std::string GetDateAsYMD(const base::Time& time);
1718

19+
base::Time GetFirstRunTime(PrefService *pref_service);
20+
1821
std::string GetPlatformIdentifier();
1922

2023
int GetIsoWeekNumber(const base::Time& time);

0 commit comments

Comments
 (0)