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 | |
Mikel Astiz | 417050a | 2020-10-16 06:35:50 | [diff] [blame] | 5 | #include "components/metrics/demographics/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" |
Mikel Astiz | f9d22bc | 2020-10-30 07:59:45 | [diff] [blame^] | 10 | #include "components/sync/driver/sync_service_utils.h" |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 11 | #include "third_party/metrics_proto/ukm/report.pb.h" |
Vincent Boisselle | 275215f | 2019-06-27 19:33:51 | [diff] [blame] | 12 | |
| 13 | namespace metrics { |
| 14 | |
Mikel Astiz | f9d22bc | 2020-10-30 07:59:45 | [diff] [blame^] | 15 | namespace { |
| 16 | |
| 17 | bool CanUploadDemographicsToGoogle(syncer::SyncService* sync_service) { |
| 18 | DCHECK(sync_service); |
| 19 | |
| 20 | // Require that the user has opted into sync the feature, without just relying |
| 21 | // on PRIORITY_PREFERENCES start sync-ing. |
| 22 | if (!sync_service->IsSyncFeatureEnabled()) { |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | switch (GetUploadToGoogleState(sync_service, syncer::PRIORITY_PREFERENCES)) { |
| 27 | case syncer::UploadState::NOT_ACTIVE: |
| 28 | return false; |
| 29 | case syncer::UploadState::INITIALIZING: |
| 30 | // Note that INITIALIZING is considered good enough, because sync is known |
| 31 | // to be enabled, and transient errors don't really matter here. |
| 32 | case syncer::UploadState::ACTIVE: |
| 33 | return true; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | } // namespace |
| 38 | |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 39 | // static |
| 40 | const base::Feature DemographicMetricsProvider::kDemographicMetricsReporting = { |
Caitlin Fischer | 53c61a4e | 2020-04-17 19:18:31 | [diff] [blame] | 41 | "DemographicMetricsReporting", base::FEATURE_ENABLED_BY_DEFAULT}; |
Vincent Boisselle | 743efdb | 2019-07-13 16:59:03 | [diff] [blame] | 42 | |
| 43 | DemographicMetricsProvider::DemographicMetricsProvider( |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 44 | std::unique_ptr<ProfileClient> profile_client, |
| 45 | MetricsLogUploader::MetricServiceType metrics_service_type) |
| 46 | : profile_client_(std::move(profile_client)), |
| 47 | metrics_service_type_(metrics_service_type) { |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 48 | DCHECK(profile_client_); |
Vincent Boisselle | 743efdb | 2019-07-13 16:59:03 | [diff] [blame] | 49 | } |
| 50 | |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 51 | DemographicMetricsProvider::~DemographicMetricsProvider() {} |
| 52 | |
Mikel Astiz | f422691 | 2020-10-19 19:10:37 | [diff] [blame] | 53 | base::Optional<UserDemographics> |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 54 | DemographicMetricsProvider::ProvideSyncedUserNoisedBirthYearAndGender() { |
Vincent Boisselle | 743efdb | 2019-07-13 16:59:03 | [diff] [blame] | 55 | // Skip if feature disabled. |
| 56 | if (!base::FeatureList::IsEnabled(kDemographicMetricsReporting)) |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 57 | return base::nullopt; |
Vincent Boisselle | 743efdb | 2019-07-13 16:59:03 | [diff] [blame] | 58 | |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 59 | // Skip if not exactly one Profile on disk. Having more than one Profile that |
| 60 | // is using the browser can make demographics less relevant. This approach |
| 61 | // cannot determine if there is more than 1 distinct user using the Profile. |
Vincent Boisselle | 7577df0 | 2019-08-01 19:07:04 | [diff] [blame] | 62 | if (profile_client_->GetNumberOfProfilesOnDisk() != 1) { |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 63 | LogUserDemographicsStatusInHistogram( |
Mikel Astiz | f422691 | 2020-10-19 19:10:37 | [diff] [blame] | 64 | UserDemographicsStatus::kMoreThanOneProfile); |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 65 | return base::nullopt; |
Vincent Boisselle | 7577df0 | 2019-08-01 19:07:04 | [diff] [blame] | 66 | } |
Vincent Boisselle | 743efdb | 2019-07-13 16:59:03 | [diff] [blame] | 67 | |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 68 | syncer::SyncService* sync_service = profile_client_->GetSyncService(); |
| 69 | // Skip if no sync service. |
Vincent Boisselle | 7577df0 | 2019-08-01 19:07:04 | [diff] [blame] | 70 | if (!sync_service) { |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 71 | LogUserDemographicsStatusInHistogram( |
Mikel Astiz | f422691 | 2020-10-19 19:10:37 | [diff] [blame] | 72 | UserDemographicsStatus::kNoSyncService); |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 73 | return base::nullopt; |
Vincent Boisselle | 7577df0 | 2019-08-01 19:07:04 | [diff] [blame] | 74 | } |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 75 | |
Mikel Astiz | f9d22bc | 2020-10-30 07:59:45 | [diff] [blame^] | 76 | if (!CanUploadDemographicsToGoogle(sync_service)) { |
Mikel Astiz | f422691 | 2020-10-19 19:10:37 | [diff] [blame] | 77 | LogUserDemographicsStatusInHistogram( |
| 78 | UserDemographicsStatus::kSyncNotEnabled); |
| 79 | return base::nullopt; |
| 80 | } |
| 81 | |
| 82 | UserDemographicsResult demographics_result = |
| 83 | GetUserNoisedBirthYearAndGenderFromPrefs( |
| 84 | profile_client_->GetNetworkTime(), profile_client_->GetPrefService()); |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 85 | LogUserDemographicsStatusInHistogram(demographics_result.status()); |
| 86 | |
| 87 | if (demographics_result.IsSuccess()) |
| 88 | return demographics_result.value(); |
| 89 | |
| 90 | return base::nullopt; |
| 91 | } |
| 92 | |
| 93 | void DemographicMetricsProvider::ProvideCurrentSessionData( |
| 94 | ChromeUserMetricsExtension* uma_proto) { |
| 95 | ProvideSyncedUserNoisedBirthYearAndGender(uma_proto); |
| 96 | } |
| 97 | |
| 98 | void DemographicMetricsProvider:: |
| 99 | ProvideSyncedUserNoisedBirthYearAndGenderToReport(ukm::Report* report) { |
| 100 | ProvideSyncedUserNoisedBirthYearAndGender(report); |
| 101 | } |
| 102 | |
| 103 | void DemographicMetricsProvider::LogUserDemographicsStatusInHistogram( |
Mikel Astiz | f422691 | 2020-10-19 19:10:37 | [diff] [blame] | 104 | UserDemographicsStatus status) { |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 105 | switch (metrics_service_type_) { |
| 106 | case MetricsLogUploader::MetricServiceType::UMA: |
| 107 | base::UmaHistogramEnumeration("UMA.UserDemographics.Status", status); |
| 108 | return; |
| 109 | case MetricsLogUploader::MetricServiceType::UKM: |
| 110 | base::UmaHistogramEnumeration("UKM.UserDemographics.Status", status); |
| 111 | return; |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 112 | } |
Vincent Boisselle | f2acc1a | 2019-10-18 01:43:51 | [diff] [blame] | 113 | NOTREACHED(); |
Vincent Boisselle | 30da3977 | 2019-07-24 18:55:47 | [diff] [blame] | 114 | } |
Vincent Boisselle | 275215f | 2019-06-27 19:33:51 | [diff] [blame] | 115 | |
| 116 | } // namespace metrics |