1 // Copyright (c) 2011 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.
5 #include "chrome/browser/profiles/profile_metrics.h"
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "base/metrics/histogram.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/profiles/profile_info_cache.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/common/chrome_constants.h"
15 #include "chrome/installer/util/google_update_settings.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/user_metrics.h"
21 const int kMaximumReportedProfileCount
= 5;
22 const int kMaximumDaysOfDisuse
= 4 * 7; // Should be integral number of weeks.
24 struct ProfileCounts
{
30 ProfileCounts() : total(0), signedin(0), managed(0), unused(0) {}
33 ProfileMetrics::ProfileType
GetProfileType(
34 const base::FilePath
& profile_path
) {
35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
36 ProfileMetrics::ProfileType metric
= ProfileMetrics::SECONDARY
;
37 ProfileManager
* manager
= g_browser_process
->profile_manager();
38 base::FilePath user_data_dir
;
39 // In unittests, we do not always have a profile_manager so check.
41 user_data_dir
= manager
->user_data_dir();
43 if (profile_path
== user_data_dir
.AppendASCII(chrome::kInitialProfile
)) {
44 metric
= ProfileMetrics::ORIGINAL
;
49 void UpdateReportedOSProfileStatistics(int active
, int signedin
) {
51 GoogleUpdateSettings::UpdateProfileCounts(active
, signedin
);
55 bool CountProfileInformation(ProfileManager
* manager
, ProfileCounts
* counts
) {
56 const ProfileInfoCache
& info_cache
= manager
->GetProfileInfoCache();
57 size_t number_of_profiles
= info_cache
.GetNumberOfProfiles();
58 counts
->total
= number_of_profiles
;
60 // Ignore other metrics if we have no profiles, e.g. in Chrome Frame tests.
61 if (!number_of_profiles
)
64 // Maximum age for "active" profile is 4 weeks.
65 base::Time oldest
= base::Time::Now() -
66 base::TimeDelta::FromDays(kMaximumDaysOfDisuse
);
68 for (size_t i
= 0; i
< number_of_profiles
; ++i
) {
69 if (info_cache
.GetProfileActiveTimeAtIndex(i
) < oldest
) {
72 if (info_cache
.ProfileIsManagedAtIndex(i
))
74 if (!info_cache
.GetUserNameOfProfileAtIndex(i
).empty())
84 AVATAR_GENERIC
= 0, // The names for avatar icons
88 AVATAR_GENERIC_ORANGE
,
89 AVATAR_GENERIC_PURPLE
,
91 AVATAR_GENERIC_YELLOW
,
94 AVATAR_VOLLEYBALL
, // 10
104 AVATAR_CUPCAKE
, // 20
111 AVATAR_UNKNOWN
, // 27
113 NUM_PROFILE_AVATAR_METRICS
116 void ProfileMetrics::UpdateReportedProfilesStatistics(ProfileManager
* manager
) {
117 ProfileCounts counts
;
118 if (CountProfileInformation(manager
, &counts
)) {
119 int limited_total
= counts
.total
;
120 int limited_signedin
= counts
.signedin
;
121 if (limited_total
> kMaximumReportedProfileCount
) {
122 limited_total
= kMaximumReportedProfileCount
+ 1;
124 (int)((float)(counts
.signedin
* limited_total
)
125 / counts
.total
+ 0.5);
127 UpdateReportedOSProfileStatistics(limited_total
, limited_signedin
);
131 void ProfileMetrics::LogNumberOfProfiles(ProfileManager
* manager
) {
132 ProfileCounts counts
;
133 bool success
= CountProfileInformation(manager
, &counts
);
134 UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfProfiles", counts
.total
);
136 // Ignore other metrics if we have no profiles, e.g. in Chrome Frame tests.
138 UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfManagedProfiles",
140 UMA_HISTOGRAM_COUNTS_100("Profile.PercentageOfManagedProfiles",
141 100 * counts
.managed
/ counts
.total
);
142 UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfSignedInProfiles",
144 UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfUnusedProfiles",
147 UpdateReportedOSProfileStatistics(counts
.total
, counts
.signedin
);
151 void ProfileMetrics::LogProfileAddNewUser(ProfileAdd metric
) {
152 DCHECK(metric
< NUM_PROFILE_ADD_METRICS
);
153 UMA_HISTOGRAM_ENUMERATION("Profile.AddNewUser", metric
,
154 NUM_PROFILE_ADD_METRICS
);
155 UMA_HISTOGRAM_ENUMERATION("Profile.NetUserCount", ADD_NEW_USER
,
156 NUM_PROFILE_NET_METRICS
);
159 void ProfileMetrics::LogProfileAvatarSelection(size_t icon_index
) {
160 DCHECK(icon_index
< NUM_PROFILE_AVATAR_METRICS
);
161 ProfileAvatar icon_name
= AVATAR_UNKNOWN
;
162 switch (icon_index
) {
164 icon_name
= AVATAR_GENERIC
;
167 icon_name
= AVATAR_GENERIC_AQUA
;
170 icon_name
= AVATAR_GENERIC_BLUE
;
173 icon_name
= AVATAR_GENERIC_GREEN
;
176 icon_name
= AVATAR_GENERIC_ORANGE
;
179 icon_name
= AVATAR_GENERIC_PURPLE
;
182 icon_name
= AVATAR_GENERIC_RED
;
185 icon_name
= AVATAR_GENERIC_YELLOW
;
188 icon_name
= AVATAR_SECRET_AGENT
;
191 icon_name
= AVATAR_SUPERHERO
;
194 icon_name
= AVATAR_VOLLEYBALL
;
197 icon_name
= AVATAR_BUSINESSMAN
;
200 icon_name
= AVATAR_NINJA
;
203 icon_name
= AVATAR_ALIEN
;
206 icon_name
= AVATAR_AWESOME
;
209 icon_name
= AVATAR_FLOWER
;
212 icon_name
= AVATAR_PIZZA
;
215 icon_name
= AVATAR_SOCCER
;
218 icon_name
= AVATAR_BURGER
;
221 icon_name
= AVATAR_CAT
;
224 icon_name
= AVATAR_CUPCAKE
;
227 icon_name
= AVATAR_DOG
;
230 icon_name
= AVATAR_HORSE
;
233 icon_name
= AVATAR_MARGARITA
;
236 icon_name
= AVATAR_NOTE
;
239 icon_name
= AVATAR_SUN_CLOUD
;
242 icon_name
= AVATAR_PLACEHOLDER
;
245 icon_name
= AVATAR_GAIA
;
247 default: // We should never actually get here.
251 UMA_HISTOGRAM_ENUMERATION("Profile.Avatar", icon_name
,
252 NUM_PROFILE_AVATAR_METRICS
);
255 void ProfileMetrics::LogProfileDeleteUser(ProfileNetUserCounts metric
) {
256 DCHECK(metric
< NUM_PROFILE_NET_METRICS
);
257 UMA_HISTOGRAM_ENUMERATION("Profile.NetUserCount", metric
,
258 NUM_PROFILE_NET_METRICS
);
261 void ProfileMetrics::LogProfileOpenMethod(ProfileOpen metric
) {
262 DCHECK(metric
< NUM_PROFILE_OPEN_METRICS
);
263 UMA_HISTOGRAM_ENUMERATION("Profile.OpenMethod", metric
,
264 NUM_PROFILE_OPEN_METRICS
);
267 void ProfileMetrics::LogProfileSwitchGaia(ProfileGaia metric
) {
268 if (metric
== GAIA_OPT_IN
)
269 LogProfileAvatarSelection(AVATAR_GAIA
);
270 UMA_HISTOGRAM_ENUMERATION("Profile.SwitchGaiaPhotoSettings",
272 NUM_PROFILE_GAIA_METRICS
);
275 void ProfileMetrics::LogProfileSwitchUser(ProfileOpen metric
) {
276 DCHECK(metric
< NUM_PROFILE_OPEN_METRICS
);
277 UMA_HISTOGRAM_ENUMERATION("Profile.OpenMethod", metric
,
278 NUM_PROFILE_OPEN_METRICS
);
281 void ProfileMetrics::LogProfileSyncInfo(ProfileSync metric
) {
282 DCHECK(metric
< NUM_PROFILE_SYNC_METRICS
);
283 UMA_HISTOGRAM_ENUMERATION("Profile.SyncCustomize", metric
,
284 NUM_PROFILE_SYNC_METRICS
);
287 void ProfileMetrics::LogProfileAuthResult(ProfileAuth metric
) {
288 UMA_HISTOGRAM_ENUMERATION("Profile.AuthResult", metric
,
289 NUM_PROFILE_AUTH_METRICS
);
292 void ProfileMetrics::LogProfileUpgradeEnrollment(
293 ProfileUpgradeEnrollment metric
) {
294 UMA_HISTOGRAM_ENUMERATION("Profile.UpgradeEnrollment", metric
,
295 NUM_PROFILE_ENROLLMENT_METRICS
);
298 void ProfileMetrics::LogProfileLaunch(Profile
* profile
) {
299 base::FilePath profile_path
= profile
->GetPath();
300 UMA_HISTOGRAM_ENUMERATION("Profile.LaunchBrowser",
301 GetProfileType(profile_path
),
302 NUM_PROFILE_TYPE_METRICS
);
304 if (profile
->IsManaged()) {
305 content::RecordAction(
306 base::UserMetricsAction("ManagedMode_NewManagedUserWindow"));
310 void ProfileMetrics::LogProfileSyncSignIn(const base::FilePath
& profile_path
) {
311 UMA_HISTOGRAM_ENUMERATION("Profile.SyncSignIn",
312 GetProfileType(profile_path
),
313 NUM_PROFILE_TYPE_METRICS
);
316 void ProfileMetrics::LogProfileUpdate(const base::FilePath
& profile_path
) {
317 UMA_HISTOGRAM_ENUMERATION("Profile.Update",
318 GetProfileType(profile_path
),
319 NUM_PROFILE_TYPE_METRICS
);