Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add date of install P3A attribute, change format of "day zero" metric #27257

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "brave/browser/brave_referrals/referrals_service_delegate.h"
#include "brave/browser/brave_shields/ad_block_subscription_download_manager_getter.h"
#include "brave/browser/brave_stats/brave_stats_updater.h"
#include "brave/browser/brave_stats/first_run_util.h"
#include "brave/browser/brave_wallet/wallet_data_files_installer_delegate_impl.h"
#include "brave/browser/component_updater/brave_component_updater_configurator.h"
#include "brave/browser/misc_metrics/process_misc_metrics.h"
Expand Down Expand Up @@ -406,7 +407,7 @@ p3a::P3AService* BraveBrowserProcessImpl::p3a_service() {
}
p3a_service_ = base::MakeRefCounted<p3a::P3AService>(
*local_state(), brave::GetChannelName(),
local_state()->GetString(kWeekOfInstallation),
brave_stats::GetFirstRunTime(local_state()),
p3a::P3AConfig::LoadFromCommandLine());
p3a_service()->InitCallbacks();
return p3a_service_.get();
Expand Down
4 changes: 3 additions & 1 deletion browser/ntp_background/ntp_p3a_helper_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ class NTPP3AHelperImplTest : public testing::Test {
config.p3a_json_upload_url = GURL(kTestP3AJsonHost);
config.p2a_json_upload_url = GURL(kTestP2AJsonHost);
config.p3a_creative_upload_url = GURL(kTestP3ACreativeHost);
base::Time install_time;
ASSERT_TRUE(base::Time::FromString("2049-01-01", &install_time));
p3a_service_ = scoped_refptr(new p3a::P3AService(
local_state_, "release", "2049-01-01", std::move(config)));
local_state_, "release", install_time, std::move(config)));

ntp_p3a_helper_ = std::make_unique<NTPP3AHelperImpl>(
&local_state_, p3a_service_.get(),
Expand Down
21 changes: 15 additions & 6 deletions components/misc_metrics/general_browser_usage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "brave/components/misc_metrics/general_browser_usage.h"

#include "base/metrics/histogram_macros.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "brave/components/misc_metrics/pref_names.h"
Expand Down Expand Up @@ -54,6 +53,7 @@ GeneralBrowserUsage::~GeneralBrowserUsage() = default;
void GeneralBrowserUsage::RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterListPref(kMiscMetricsBrowserUsageList);
registry->RegisterStringPref(kMiscMetricsDayZeroVariantAtInstall, {});
registry->RegisterTimePref(kMiscMetricsLastDayZeroReport, {});
}

void GeneralBrowserUsage::ReportWeeklyUse() {
Expand All @@ -68,19 +68,28 @@ void GeneralBrowserUsage::ReportWeeklyUse() {

#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)
void GeneralBrowserUsage::ReportInstallTime() {
int days_since_install = (base::Time::Now() - first_run_time_).InDays();
int days_since_install =
(base::Time::Now() - first_run_time_).InDaysFloored();
if (days_since_install < 0 || days_since_install > 30) {
return;
}
auto start_of_report_day = first_run_time_ + base::Days(days_since_install);
auto last_report = local_state_->GetTime(kMiscMetricsLastDayZeroReport);
if (last_report >= start_of_report_day) {
return;
}
std::string day_zero_variant =
local_state_->GetString(kMiscMetricsDayZeroVariantAtInstall);
if (day_zero_variant.empty()) {
return;
}
std::string histogram_name = base::StrCat(
{kDayZeroInstallTimePrefix, base::ToUpperASCII(day_zero_variant),
kDayZeroInstallTimeSuffix});
base::UmaHistogramExactLinear(histogram_name, days_since_install, 31);
// Get index of first char (0-25 for a-z)
int variant_index = base::ToLowerASCII(day_zero_variant[0]) - 'a';
if (variant_index < 0) {
return;
}
local_state_->SetTime(kMiscMetricsLastDayZeroReport, base::Time::Now());
UMA_HISTOGRAM_EXACT_LINEAR(kDayZeroVariantHistogramName, variant_index, 31);
}
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)

Expand Down
3 changes: 1 addition & 2 deletions components/misc_metrics/general_browser_usage.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ inline constexpr char kWeeklyUseHistogramName[] = "Brave.Core.WeeklyUsage";
inline constexpr char kProfileCountHistogramName[] = "Brave.Core.ProfileCount";

#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)
inline constexpr char kDayZeroInstallTimePrefix[] = "Brave.DayZero.";
inline constexpr char kDayZeroInstallTimeSuffix[] = ".InstallTime";
inline constexpr char kDayZeroVariantHistogramName[] = "Brave.DayZero.Variant";
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)

// TODO(djandries): remove this metric when Nebula experiment is over
Expand Down
42 changes: 15 additions & 27 deletions components/misc_metrics/general_browser_usage_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@

namespace misc_metrics {

#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)
constexpr char kDayZeroAInstallTime[] = "Brave.DayZero.A.InstallTime";
constexpr char kDayZeroBInstallTime[] = "Brave.DayZero.B.InstallTime";
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)

class GeneralBrowserUsageUnitTest : public testing::Test {
public:
GeneralBrowserUsageUnitTest()
Expand Down Expand Up @@ -98,55 +93,48 @@ TEST_F(GeneralBrowserUsageUnitTest, ProfileCount) {
#endif // !BUILDFLAG(IS_ANDROID)

#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)
TEST_F(GeneralBrowserUsageUnitTest, InstallTimeB) {
TEST_F(GeneralBrowserUsageUnitTest, InstallTimeVariantSwitch) {
base::Time install_time = base::Time::Now();
SetUpUsage("B", true, install_time);

histogram_tester_->ExpectUniqueSample(kDayZeroBInstallTime, 0, 1);
histogram_tester_->ExpectUniqueSample(kDayZeroVariantHistogramName, 1, 1);

task_environment_.FastForwardBy(base::Days(15));

int last_bucket_count =
histogram_tester_->GetBucketCount(kDayZeroBInstallTime, 15);
EXPECT_GE(last_bucket_count, 1);
histogram_tester_->ExpectTotalCount(kDayZeroAInstallTime, 0);
histogram_tester_->ExpectUniqueSample(kDayZeroVariantHistogramName, 1, 16);

SetUpUsage("A", false, install_time);
// Ensure histogram name does not change if "day zero" is enabled
// after install; we only want to report the "day zero on" metric
// if it was enabled at install time.
EXPECT_GT(histogram_tester_->GetBucketCount(kDayZeroBInstallTime, 15),
last_bucket_count);
histogram_tester_->ExpectTotalCount(kDayZeroAInstallTime, 0);
histogram_tester_->ExpectUniqueSample(kDayZeroVariantHistogramName, 1, 16);

task_environment_.FastForwardBy(base::Days(16));
EXPECT_GE(histogram_tester_->GetBucketCount(kDayZeroBInstallTime, 30), 1);
histogram_tester_->ExpectUniqueSample(kDayZeroVariantHistogramName, 1, 31);

ResetHistogramTester();
// Ensure there are no more reports past 30 days
task_environment_.FastForwardBy(base::Days(5));

histogram_tester_->ExpectTotalCount(kDayZeroBInstallTime, 0);
histogram_tester_->ExpectTotalCount(kDayZeroAInstallTime, 0);
histogram_tester_->ExpectTotalCount(kDayZeroVariantHistogramName, 0);
histogram_tester_->ExpectTotalCount(kDayZeroVariantHistogramName, 0);
}

TEST_F(GeneralBrowserUsageUnitTest, InstallTimeA) {
TEST_F(GeneralBrowserUsageUnitTest, InstallTimeBasic) {
base::Time install_time = base::Time::Now();
SetUpUsage("A", true, install_time);

histogram_tester_->ExpectUniqueSample(kDayZeroAInstallTime, 0, 1);
histogram_tester_->ExpectUniqueSample(kDayZeroVariantHistogramName, 0, 1);

task_environment_.FastForwardBy(base::Days(15));

int last_bucket_count =
histogram_tester_->GetBucketCount(kDayZeroAInstallTime, 15);
EXPECT_GE(last_bucket_count, 1);
histogram_tester_->ExpectTotalCount(kDayZeroBInstallTime, 0);
histogram_tester_->ExpectUniqueSample(kDayZeroVariantHistogramName, 0, 16);

SetUpUsage("B", false, install_time);
EXPECT_GT(histogram_tester_->GetBucketCount(kDayZeroAInstallTime, 15),
last_bucket_count);
histogram_tester_->ExpectTotalCount(kDayZeroBInstallTime, 0);
task_environment_.FastForwardBy(base::Days(15));
histogram_tester_->ExpectUniqueSample(kDayZeroVariantHistogramName, 0, 31);

task_environment_.FastForwardBy(base::Days(15));
histogram_tester_->ExpectUniqueSample(kDayZeroVariantHistogramName, 0, 31);
}
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)

Expand Down
2 changes: 2 additions & 0 deletions components/misc_metrics/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ inline constexpr char kMiscMetricsTotalLocationBarEntriesStorage[] =

inline constexpr char kMiscMetricsDayZeroVariantAtInstall[] =
"brave.misc_metrics.day_zero_variant_at_install";
inline constexpr char kMiscMetricsLastDayZeroReport[] =
"brave.misc_metrics.last_day_zero_report";

inline constexpr char kMiscMetricsNTPWidgetUsageStorage[] =
"brave.misc_metrics.ntp_widget_usage";
Expand Down
16 changes: 9 additions & 7 deletions components/p3a/constellation_helper_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class P3AConstellationHelperTest : public testing::Test {

protected:
void SetUp() override {
ASSERT_TRUE(base::Time::FromString("2022-01-01", &install_time_));
p3a_config_.disable_star_attestation = true;
p3a_config_.star_randomness_host = kTestHost;

Expand Down Expand Up @@ -175,6 +176,7 @@ class P3AConstellationHelperTest : public testing::Test {

std::string histogram_name_from_callback_;
uint8_t epoch_from_callback_;
base::Time install_time_;

base::flat_map<MetricLogType, bool> info_request_made_;
base::flat_map<MetricLogType, bool> points_request_made_;
Expand Down Expand Up @@ -251,7 +253,7 @@ TEST_F(P3AConstellationHelperTest, GenerateBasicMessage) {
task_environment_.RunUntilIdle();

MessageMetainfo meta_info;
meta_info.Init(&local_state_, "release", "2022-01-01");
meta_info.Init(&local_state_, "release", install_time_);

helper_->StartMessagePreparation(
kTestHistogramName, log_type,
Expand All @@ -274,7 +276,7 @@ TEST_F(P3AConstellationHelperTest, GenerateBasicMessage) {

TEST_F(P3AConstellationHelperTest, IncludeRefcode) {
MessageMetainfo meta_info;
meta_info.Init(&local_state_, "release", "2022-01-01");
meta_info.Init(&local_state_, "release", install_time_);

std::string message_with_no_refcode = GenerateP3AConstellationMessage(
kTestHistogramName, 0, meta_info, kP3AUploadType, std::nullopt);
Expand All @@ -299,7 +301,7 @@ TEST_F(P3AConstellationHelperTest, IncludeRefcode) {

#if !BUILDFLAG(IS_IOS)
local_state_.SetString(kReferralPromoCode, "BRV003");
meta_info.Init(&local_state_, "release", "2022-01-01");
meta_info.Init(&local_state_, "release", install_time_);

message_with_refcode = GenerateP3AConstellationMessage(
kTestHistogramName, 0, meta_info, kP3AUploadType,
Expand All @@ -313,7 +315,7 @@ TEST_F(P3AConstellationHelperTest, IncludeRefcode) {
EXPECT_EQ(refcode_layers.at(8), "ref|BRV003");

local_state_.SetString(kReferralPromoCode, "ZRK009");
meta_info.Init(&local_state_, "release", "2022-01-01");
meta_info.Init(&local_state_, "release", install_time_);

message_with_refcode = GenerateP3AConstellationMessage(
kTestHistogramName, 0, meta_info, kP3AUploadType,
Expand All @@ -330,7 +332,7 @@ TEST_F(P3AConstellationHelperTest, IncludeRefcode) {

TEST_F(P3AConstellationHelperTest, CustomAttributes) {
MessageMetainfo meta_info;
meta_info.Init(&local_state_, "release", "2022-01-01");
meta_info.Init(&local_state_, "release", install_time_);

// Test with custom attributes list
MetricConfig config{.attributes = MetricAttributes{
Expand Down Expand Up @@ -370,7 +372,7 @@ TEST_F(P3AConstellationHelperTest, CustomAttributes) {

TEST_F(P3AConstellationHelperTest, NebulaMessage) {
MessageMetainfo meta_info;
meta_info.Init(&local_state_, "release", "2022-01-01");
meta_info.Init(&local_state_, "release", install_time_);

std::string message = GenerateP3AConstellationMessage(
kTestHistogramName, 3, meta_info, kP3AUploadType,
Expand All @@ -394,7 +396,7 @@ TEST_F(P3AConstellationHelperTest, NebulaSample) {
task_environment_.RunUntilIdle();

MessageMetainfo meta_info;
meta_info.Init(&local_state_, "release", "2022-01-01");
meta_info.Init(&local_state_, "release", install_time_);

helper_->StartMessagePreparation(
kTestNebulaHistogramName, MetricLogType::kTypical,
Expand Down
4 changes: 2 additions & 2 deletions components/p3a/message_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ MessageManager::MessageManager(PrefService& local_state,
const P3AConfig* config,
Delegate& delegate,
std::string channel,
std::string week_of_install)
base::Time first_run_time)
: local_state_(local_state), config_(config), delegate_(delegate) {
message_meta_.Init(&local_state, channel, week_of_install);
message_meta_.Init(&local_state, channel, first_run_time);

// Init log stores.
for (MetricLogType log_type : kAllMetricLogTypes) {
Expand Down
2 changes: 1 addition & 1 deletion components/p3a/message_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class MessageManager : public MetricLogStore::Delegate {
const P3AConfig* config,
Delegate& delegate,
std::string channel,
std::string week_of_install);
base::Time first_run_time);
~MessageManager() override;

MessageManager(const MessageManager&) = delete;
Expand Down
4 changes: 3 additions & 1 deletion components/p3a/message_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ class P3AMessageManagerTest : public testing::Test,
url_loader_factory_.AddResponse(request.url.spec(), response);
}));

base::Time install_time;
ASSERT_TRUE(base::Time::FromString("2099-01-01", &install_time));
message_manager_ = std::make_unique<MessageManager>(
*local_state_, &p3a_config_, *this, "release", "2099-01-01");
*local_state_, &p3a_config_, *this, "release", install_time);

message_manager_->Start(shared_url_loader_factory_);

Expand Down
3 changes: 2 additions & 1 deletion components/p3a/metric_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ enum class MetricAttribute {
kRegion,
kSubregion,
kRef,
kMaxValue = kRef,
kDateOfInstall,
kMaxValue = kDateOfInstall,
};

inline constexpr MetricAttribute kDefaultMetricAttributes[] = {
Expand Down
9 changes: 2 additions & 7 deletions components/p3a/metric_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,10 @@ inline constexpr auto kCollectedExpressHistograms =
{"Brave.AIChat.UsageDaily.2", MetricConfig{.ephemeral = true}},
{"Brave.AIChat.UsageDaily.SidebarEnabledA", MetricConfig{.ephemeral = true}},
{"Brave.Core.UsageDaily", {}},
{"Brave.DayZero.A.InstallTime", MetricConfig{
{"Brave.DayZero.Variant", MetricConfig{
.ephemeral = true,
.constellation_only = true,
.append_attributes = MetricAttributesToAppend{MetricAttribute::kRef}
}},
{"Brave.DayZero.B.InstallTime", MetricConfig{
.ephemeral = true,
.constellation_only = true,
.append_attributes = MetricAttributesToAppend{MetricAttribute::kRef}
.attributes = MetricAttributes{MetricAttribute::kAnswerIndex, MetricAttribute::kDateOfInstall, MetricAttribute::kVersion, MetricAttribute::kChannel, MetricAttribute::kPlatform, MetricAttribute::kCountryCode, MetricAttribute::kRef}
}},
{"Brave.Rewards.EnabledInstallationTime", MetricConfig{.ephemeral = true}},
{"Brave.Search.BraveDaily", MetricConfig{.ephemeral = true}},
Expand Down
18 changes: 11 additions & 7 deletions components/p3a/p3a_message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "brave/components/brave_stats/browser/brave_stats_updater_util.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/p3a/metric_config.h"
Expand Down Expand Up @@ -44,6 +45,7 @@ constexpr char kWosAttributeName[] = "wos";
constexpr char kMosAttributeName[] = "mos";
constexpr char kWoiAttributeName[] = "woi";
constexpr char kYoiAttributeName[] = "yoi";
constexpr char kDateOfInstallAttributeName[] = "dtoi";
constexpr char kCountryCodeAttributeName[] = "country_code";
constexpr char kVersionAttributeName[] = "version";
constexpr char kRegionAttributeName[] = "region";
Expand Down Expand Up @@ -75,7 +77,7 @@ std::vector<std::array<std::string, 2>> PopulateConstellationAttributes(
const std::vector<MetricAttribute>& attributes_to_load,
bool is_creative) {
base::Time::Exploded exploded;
meta.date_of_install().LocalExplode(&exploded);
meta.date_of_install().UTCExplode(&exploded);
DCHECK_GE(exploded.year, 999);

std::vector<std::array<std::string, 2>> attributes;
Expand All @@ -88,6 +90,7 @@ std::vector<std::array<std::string, 2>> PopulateConstellationAttributes(
attributes = {{kMetricNameAttributeName, std::string(metric_name)}};
}
std::string country_code;
std::string dtoi;
for (const auto& attribute : attributes_to_load) {
switch (attribute) {
case MetricAttribute::kAnswerIndex:
Expand Down Expand Up @@ -132,6 +135,11 @@ std::vector<std::array<std::string, 2>> PopulateConstellationAttributes(
attributes.push_back(
{kWoiAttributeName, base::NumberToString(meta.woi())});
break;
case MetricAttribute::kDateOfInstall:
dtoi = base::StringPrintf("%d-%02d-%02d", exploded.year, exploded.month,
exploded.day_of_month);
attributes.push_back({kDateOfInstallAttributeName, dtoi});
break;
case MetricAttribute::kGeneralPlatform:
attributes.push_back(
{kGeneralPlatformAttributeName, meta.general_platform()});
Expand Down Expand Up @@ -273,19 +281,15 @@ std::string GenerateP3AConstellationMessage(

void MessageMetainfo::Init(PrefService* local_state,
std::string brave_channel,
std::string week_of_install) {
base::Time first_run_time) {
local_state_ = local_state;
platform_ = brave_stats::GetPlatformIdentifier();
general_platform_ = brave_stats::GetGeneralPlatformIdentifier();
channel_ = brave_channel;
InitVersion();
InitRef();

if (!week_of_install.empty()) {
date_of_install_ = brave_stats::GetYMDAsDate(week_of_install);
} else {
date_of_install_ = base::Time::Now();
}
date_of_install_ = first_run_time;
woi_ = brave_stats::GetIsoWeekNumber(date_of_install_);

country_code_from_timezone_raw_ =
Expand Down
2 changes: 1 addition & 1 deletion components/p3a/p3a_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MessageMetainfo {

void Init(PrefService* local_state,
std::string brave_channel,
std::string week_of_install);
base::Time first_run_time);

void Update();

Expand Down
Loading
Loading