Vincent Boisselle | 275215f | 2019-06-27 19:33:51 | [diff] [blame] | 1 | // Copyright 2019 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 | #include "components/metrics/demographic_metrics_provider.h" |
Vincent Boisselle | 743efdb | 2019-07-13 16:59:03 | [diff] [blame] | 6 | |
Vincent Boisselle | 275215f | 2019-06-27 19:33:51 | [diff] [blame] | 7 | #include "base/feature_list.h" |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 8 | #include "base/metrics/histogram_functions.h" |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 9 | #include "base/optional.h" |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 10 | #include "components/sync/base/sync_prefs.h" |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 11 | #include "components/sync/driver/sync_service.h" |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 12 | #include "third_party/metrics_proto/ukm/report.pb.h" |
Vincent Boisselle | 275215f | 2019-06-27 19:33:51 | [diff] [blame] | 13 | |
| 14 | namespace metrics { |
| 15 | |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 16 | // static |
| 17 | const base::Feature DemographicMetricsProvider::kDemographicMetricsReporting = { |
Caitlin Fischer | 53c61a4e | 2020-04-17 19:18:31 | [diff] [blame^] | 18 | "DemographicMetricsReporting", base::FEATURE_ENABLED_BY_DEFAULT}; |
Vincent Boisselle | 743efdb | 2019-07-13 16:59:03 | [diff] [blame] | 19 | |
| 20 | DemographicMetricsProvider::DemographicMetricsProvider( |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 21 | std::unique_ptr<ProfileClient> profile_client, |
| 22 | MetricsLogUploader::MetricServiceType metrics_service_type) |
| 23 | : profile_client_(std::move(profile_client)), |
| 24 | metrics_service_type_(metrics_service_type) { |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 25 | DCHECK(profile_client_); |
Vincent Boisselle | 743efdb | 2019-07-13 16:59:03 | [diff] [blame] | 26 | } |
| 27 | |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 28 | DemographicMetricsProvider::~DemographicMetricsProvider() {} |
| 29 | |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 30 | base::Optional<syncer::UserDemographics> |
| 31 | DemographicMetricsProvider::ProvideSyncedUserNoisedBirthYearAndGender() { |
Vincent Boisselle | 743efdb | 2019-07-13 16:59:03 | [diff] [blame] | 32 | // Skip if feature disabled. |
| 33 | if (!base::FeatureList::IsEnabled(kDemographicMetricsReporting)) |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 34 | return base::nullopt; |
Vincent Boisselle | 743efdb | 2019-07-13 16:59:03 | [diff] [blame] | 35 | |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 36 | // Skip if not exactly one Profile on disk. Having more than one Profile that |
| 37 | // is using the browser can make demographics less relevant. This approach |
| 38 | // cannot determine if there is more than 1 distinct user using the Profile. |
Vincent Boisselle | 7577df0 | 2019-08-01 19:07:04 | [diff] [blame] | 39 | if (profile_client_->GetNumberOfProfilesOnDisk() != 1) { |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 40 | LogUserDemographicsStatusInHistogram( |
Vincent Boisselle | 7577df0 | 2019-08-01 19:07:04 | [diff] [blame] | 41 | syncer::UserDemographicsStatus::kMoreThanOneProfile); |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 42 | return base::nullopt; |
Vincent Boisselle | 7577df0 | 2019-08-01 19:07:04 | [diff] [blame] | 43 | } |
Vincent Boisselle | 743efdb | 2019-07-13 16:59:03 | [diff] [blame] | 44 | |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 45 | syncer::SyncService* sync_service = profile_client_->GetSyncService(); |
| 46 | // Skip if no sync service. |
Vincent Boisselle | 7577df0 | 2019-08-01 19:07:04 | [diff] [blame] | 47 | if (!sync_service) { |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 48 | LogUserDemographicsStatusInHistogram( |
Vincent Boisselle | 7577df0 | 2019-08-01 19:07:04 | [diff] [blame] | 49 | syncer::UserDemographicsStatus::kNoSyncService); |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 50 | return base::nullopt; |
Vincent Boisselle | 7577df0 | 2019-08-01 19:07:04 | [diff] [blame] | 51 | } |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 52 | |
Vincent Boisselle | 7577df0 | 2019-08-01 19:07:04 | [diff] [blame] | 53 | syncer::UserDemographicsResult demographics_result = |
Vincent Boisselle | 892e9b7e | 2019-08-21 17:27:32 | [diff] [blame] | 54 | sync_service->GetUserNoisedBirthYearAndGender( |
| 55 | profile_client_->GetNetworkTime()); |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 56 | LogUserDemographicsStatusInHistogram(demographics_result.status()); |
| 57 | |
| 58 | if (demographics_result.IsSuccess()) |
| 59 | return demographics_result.value(); |
| 60 | |
| 61 | return base::nullopt; |
| 62 | } |
| 63 | |
| 64 | void DemographicMetricsProvider::ProvideCurrentSessionData( |
| 65 | ChromeUserMetricsExtension* uma_proto) { |
| 66 | ProvideSyncedUserNoisedBirthYearAndGender(uma_proto); |
| 67 | } |
| 68 | |
| 69 | void DemographicMetricsProvider:: |
| 70 | ProvideSyncedUserNoisedBirthYearAndGenderToReport(ukm::Report* report) { |
| 71 | ProvideSyncedUserNoisedBirthYearAndGender(report); |
| 72 | } |
| 73 | |
| 74 | void DemographicMetricsProvider::LogUserDemographicsStatusInHistogram( |
| 75 | syncer::UserDemographicsStatus status) { |
| 76 | switch (metrics_service_type_) { |
| 77 | case MetricsLogUploader::MetricServiceType::UMA: |
| 78 | base::UmaHistogramEnumeration("UMA.UserDemographics.Status", status); |
| 79 | return; |
| 80 | case MetricsLogUploader::MetricServiceType::UKM: |
| 81 | base::UmaHistogramEnumeration("UKM.UserDemographics.Status", status); |
| 82 | return; |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 83 | } |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 84 | NOTREACHED(); |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 85 | } |
Vincent Boisselle | 275215f | 2019-06-27 19:33:51 | [diff] [blame] | 86 | |
| 87 | } // namespace metrics |