Skip to content

Commit

Permalink
M110 merge: Modify website usage UKM to use the NAVIGATION_ID and CHR…
Browse files Browse the repository at this point in the history
…OMEOS_WEBSITE_ID source type.

Modify WebsiteMetrics:
1. For the urls saved in the user pref, since we can't get the
web_contents from the previous user session, call
GetSourceIdForChromeOSWebsiteURL to generate the source id.
2. For the realtime url, since we can get web_contents, call GetPageUkmSourceId to get the source id from web_contents with the
NAVIGATION_ID source type.

Remove UrlContent, because we don't report scope and only report the
full url.

Modify test cases to align with the change:
1. Remove UrlContent.
2. Record the full url, not the scope for the website with manifest.

BUG=1401207, b:254508887

(cherry picked from commit e7a16c1)

Change-Id: Ifda002acb93893650f592a8dc361d7dc1aa698fd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4112860
Reviewed-by: Robert Kaplow <rkaplow@chromium.org>
Reviewed-by: Yue Ru Sun <yrsun@chromium.org>
Commit-Queue: Nancy Wang <nancylingwang@chromium.org>
Reviewed-by: Dominick Ng <dominickn@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1091617}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4179390
Cr-Commit-Position: refs/branch-heads/5481@{#474}
Cr-Branched-From: 130f3e4-refs/heads/main@{#1084008}
  • Loading branch information
Nancy Wang authored and Chromium LUCI CQ committed Jan 19, 2023
1 parent 7faa946 commit cd78b3a
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 246 deletions.
107 changes: 32 additions & 75 deletions chrome/browser/apps/app_service/metrics/website_metrics.cc
Expand Up @@ -87,7 +87,6 @@ namespace apps {

constexpr char kWebsiteUsageTime[] = "app_platform_metrics.website_usage_time";
constexpr char kRunningTimeKey[] = "time";
constexpr char kUrlContentKey[] = "url_content";
constexpr char kPromotableKey[] = "promotable";

WebsiteMetrics::ActiveTabWebContentsObserver::ActiveTabWebContentsObserver(
Expand Down Expand Up @@ -139,26 +138,19 @@ WebsiteMetrics::UrlInfo::UrlInfo(const base::Value& value) {
return;
}

auto url_content_value = data_dict->FindInt(kUrlContentKey);
if (!url_content_value.has_value()) {
return;
}

auto promotable_value = data_dict->FindBool(kPromotableKey);
if (!promotable_value.has_value()) {
return;
}

running_time_in_two_hours = running_time_value.value();
url_content = static_cast<UrlContent>(url_content_value.value());
promotable = promotable_value.value();
}

base::Value WebsiteMetrics::UrlInfo::ConvertToValue() const {
base::Value usage_time_dict(base::Value::Type::DICTIONARY);
usage_time_dict.SetPath(kRunningTimeKey,
base::TimeDeltaToValue(running_time_in_two_hours));
usage_time_dict.SetIntKey(kUrlContentKey, static_cast<int>(url_content));
usage_time_dict.SetBoolKey(kPromotableKey, promotable);
return usage_time_dict;
}
Expand Down Expand Up @@ -442,9 +434,9 @@ void WebsiteMetrics::OnWebContentsUpdated(content::WebContents* web_contents) {
bool is_activated = wm::IsActiveWindow(window) &&
it != window_to_web_contents_.end() &&
it->second == web_contents;
AddUrlInfo(web_contents->GetVisibleURL(), base::TimeTicks::Now(),
UrlContent::kFullUrl, is_activated,
/*promotable=*/false);
AddUrlInfo(web_contents->GetVisibleURL(),
web_contents->GetPrimaryMainFrame()->GetPageUkmSourceId(),
base::TimeTicks::Now(), is_activated, /*promotable=*/false);
}

void WebsiteMetrics::OnInstallableWebAppStatusUpdated(
Expand All @@ -465,64 +457,31 @@ void WebsiteMetrics::OnInstallableWebAppStatusUpdated(

// In some test cases, AppBannerManager might be null.
if (!app_banner_manager ||
blink::IsEmptyManifest(app_banner_manager->manifest())) {
return;
}

auto* window =
GetWindowWithBrowser(chrome::FindBrowserWithWebContents(web_contents));
if (!window) {
blink::IsEmptyManifest(app_banner_manager->manifest()) ||
app_banner_manager->manifest().scope.is_empty()) {
return;
}

DCHECK(!app_banner_manager->manifest().scope.is_empty());
auto window_it = window_to_web_contents_.find(window);
bool is_activated = wm::IsActiveWindow(window) &&
window_it != window_to_web_contents_.end() &&
window_it->second == web_contents;
UpdateUrlInfo(it->second, app_banner_manager->manifest().scope,
UrlContent::kScope, is_activated,
/*promotable=*/true);
it->second = app_banner_manager->manifest().scope;
UpdateUrlInfo(it->second, /*promotable=*/true);
}

void WebsiteMetrics::AddUrlInfo(const GURL& url,
ukm::SourceId source_id,
const base::TimeTicks& start_time,
UrlContent url_content,
bool is_activated,
bool promotable) {
auto& url_info = url_infos_[url];
url_info.source_id = source_id;
url_info.start_time = start_time;
url_info.url_content = url_content;
url_info.is_activated = is_activated;
url_info.promotable = promotable;
}

void WebsiteMetrics::UpdateUrlInfo(const GURL& old_url,
const GURL& new_url,
UrlContent url_content,
bool is_activated,
bool promotable) {
base::TimeTicks start_time = base::TimeTicks::Now();
base::TimeDelta running_time_in_five_minutes;
base::TimeDelta running_time_in_two_hours;

auto it = url_infos_.find(old_url);
void WebsiteMetrics::UpdateUrlInfo(const GURL& url, bool promotable) {
auto it = url_infos_.find(url);
if (it != url_infos_.end()) {
running_time_in_five_minutes = it->second.running_time_in_five_minutes;
running_time_in_two_hours = it->second.running_time_in_two_hours;
start_time = it->second.start_time;
url_infos_.erase(old_url);
}

if (new_url.is_empty() || !new_url.SchemeIsHTTPOrHTTPS()) {
return;
it->second.promotable = promotable;
}

AddUrlInfo(new_url, start_time, url_content, is_activated, promotable);
url_infos_[new_url].running_time_in_five_minutes =
running_time_in_five_minutes;
url_infos_[new_url].running_time_in_two_hours = running_time_in_two_hours;
}

void WebsiteMetrics::SetWindowActivated(aura::Window* window) {
Expand Down Expand Up @@ -603,8 +562,9 @@ void WebsiteMetrics::SaveUsageTime() {
void WebsiteMetrics::RecordUsageTime() {
for (auto& it : url_infos_) {
if (!it.second.running_time_in_two_hours.is_zero()) {
EmitUkm(it.first, it.second.running_time_in_two_hours.InMilliseconds(),
it.second.url_content, it.second.promotable,
EmitUkm(it.second.source_id,
it.second.running_time_in_two_hours.InMilliseconds(),
it.second.promotable,
/*is_from_last_login=*/false);
it.second.running_time_in_two_hours = base::TimeDelta();
}
Expand All @@ -629,39 +589,36 @@ void WebsiteMetrics::RecordUsageTimeFromPref() {
}
auto url_info = std::make_unique<UrlInfo>(url_info_value);
if (!url_info->running_time_in_two_hours.is_zero()) {
EmitUkm(url, url_info->running_time_in_two_hours.InMilliseconds(),
url_info->url_content, url_info->promotable,
// For the URL records dump from the user pref, since the web_contents
// doesn't exist due to logout/login, we can't call GetPageUkmSourceId to
// get the source id with the web_contents. So call
// GetSourceIdForChromeOSWebsiteURL to generate the UKM source id to log
// saved URLs from the last login session.
auto source_id = ukm::UkmRecorder::GetSourceIdForChromeOSWebsiteURL(
base::PassKey<WebsiteMetrics>(), url);
EmitUkm(source_id, url_info->running_time_in_two_hours.InMilliseconds(),
url_info->promotable,
/*is_from_last_login=*/true);
}
}
}

void WebsiteMetrics::EmitUkm(const GURL& url,
void WebsiteMetrics::EmitUkm(ukm::SourceId source_id,
int64_t usage_time,
UrlContent url_content,
bool promotable,
bool is_from_last_login) {
auto source_id = ukm::UkmRecorder::GetSourceIdForWebsiteUrl(
base::PassKey<WebsiteMetrics>(), url);
if (url.is_empty() || !url.SchemeIsHTTPOrHTTPS() ||
ukm::SourceIdObj::FromInt64(source_id).GetType() !=
ukm::SourceIdType::DESKTOP_WEB_APP_ID) {
LOG(ERROR) << "WebsiteMetrics::EmitUkm url is " << url.spec()
<< ", source id type is "
<< (int)ukm::SourceIdObj::FromInt64(source_id).GetType();
if (source_id == ukm::kInvalidSourceId) {
DVLOG(1) << "WebsiteMetrics::EmitUkm source id is invalid.";
base::debug::DumpWithoutCrashing();
return;
}

if (source_id != ukm::kInvalidSourceId) {
ukm::builders::ChromeOS_WebsiteUsageTime builder(source_id);
builder.SetDuration(usage_time)
.SetUrlContent(static_cast<int>(url_content))
.SetIsFromLastLogin(is_from_last_login)
.SetPromotable(promotable)
.SetUserDeviceMatrix(user_type_by_device_type_)
.Record(ukm::UkmRecorder::Get());
}
ukm::builders::ChromeOS_WebsiteUsageTime builder(source_id);
builder.SetDuration(usage_time)
.SetIsFromLastLogin(is_from_last_login)
.SetPromotable(promotable)
.SetUserDeviceMatrix(user_type_by_device_type_)
.Record(ukm::UkmRecorder::Get());
}

} // namespace apps
29 changes: 7 additions & 22 deletions chrome/browser/apps/app_service/metrics/website_metrics.h
Expand Up @@ -22,6 +22,7 @@
#include "content/public/browser/page.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
#include "ui/wm/public/activation_change_observer.h"
Expand All @@ -36,17 +37,6 @@ namespace apps {
class WebsiteMetricsBrowserTest;
class TestWebsiteMetrics;

// This is used for logging, so do not remove or reorder existing entries.
enum class UrlContent {
kUnknown = 0,
kFullUrl = 1,
kScope = 2,

// Add any new values above this one, and update kMaxValue to the highest
// enumerator value.
kMaxValue = kScope,
};

extern const char kWebsiteUsageTime[];
extern const char kRunningTimeKey[];
extern const char kUrlContentKey[];
Expand Down Expand Up @@ -134,14 +124,14 @@ class WebsiteMetrics : public BrowserListObserver,
struct UrlInfo {
UrlInfo() = default;
explicit UrlInfo(const base::Value& value);
ukm::SourceId source_id = ukm::kInvalidSourceId;
base::TimeTicks start_time;
// Running time in the past 5 minutes without noise.
base::TimeDelta running_time_in_five_minutes;
// Sum `running_time_in_five_minutes` with noise in the past 2 hours:
// time1 * noise1 + time2 * noise2 + time3 * noise3....
base::TimeDelta running_time_in_two_hours;

UrlContent url_content = UrlContent::kUnknown;
bool is_activated = false;
bool promotable = false;

Expand Down Expand Up @@ -184,18 +174,14 @@ class WebsiteMetrics : public BrowserListObserver,

// Adds the url info to `url_infos_`.
void AddUrlInfo(const GURL& url,
ukm::SourceId source_id,
const base::TimeTicks& start_time,
UrlContent url_content,
bool is_activated,
bool promotable);

// Modifies `old_url` to `new_url` in `url_infos_`, when the website manifest
// is updated.
void UpdateUrlInfo(const GURL& old_url,
const GURL& new_url,
UrlContent url_content,
bool is_activated,
bool promotable);
// Modifies `url_infos_` to set whether the website can be promoted to PWA,
// when the website manifest is updated.
void UpdateUrlInfo(const GURL& old_url, bool promotable);

void SetWindowActivated(aura::Window* window);

Expand All @@ -216,9 +202,8 @@ class WebsiteMetrics : public BrowserListObserver,
// after the user logs in.
void RecordUsageTimeFromPref();

void EmitUkm(const GURL& url,
void EmitUkm(ukm::SourceId source_id,
int64_t usage_time,
UrlContent url_content,
bool promotable,
bool is_from_last_login);

Expand Down

0 comments on commit cd78b3a

Please sign in to comment.