Commit c32c39f7 authored by Eric Willigers's avatar Eric Willigers Committed by Commit Bot

WebApps: Support WebApp.Engagement histograms

Update WebAppMetrics histograms when the kDesktopPWAsWithoutExtensions
feature is enabled, and a web app launches.

Not yet updating: histograms indicating if an app was installed by the
user.

Bug: 918986
Change-Id: I66b356776b7de5d6c6156517777b92e56dbd92a5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1846625
Commit-Queue: Eric Willigers <ericwilligers@chromium.org>
Reviewed-by: default avatarAlexey Baskakov <loyso@chromium.org>
Cr-Commit-Position: refs/heads/master@{#705783}
parent f366dd85
......@@ -6,6 +6,7 @@
#include <bitset>
#include "base/metrics/statistics_recorder.h"
#include "base/test/bind_test_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/engagement/site_engagement_service.h"
......@@ -119,6 +120,40 @@ void ExpectTotalCounts(const base::HistogramTester& tester,
}
}
void ExpectLaunchCounts(const base::HistogramTester& tester,
base::HistogramBase::Count windowLaunches,
base::HistogramBase::Count tabLaunches) {
tester.ExpectBucketCount("Extensions.HostedAppLaunchContainer",
apps::mojom::LaunchContainer::kLaunchContainerWindow,
windowLaunches);
tester.ExpectBucketCount("Extensions.HostedAppLaunchContainer",
apps::mojom::LaunchContainer::kLaunchContainerTab,
tabLaunches);
tester.ExpectTotalCount("Extensions.HostedAppLaunchContainer",
windowLaunches + tabLaunches);
if (tabLaunches > 0) {
tester.ExpectUniqueSample("Extensions.AppTabLaunchType",
extensions::LAUNCH_TYPE_REGULAR, tabLaunches);
} else {
EXPECT_EQ(nullptr, base::StatisticsRecorder::FindHistogram(
"Extensions.AppTabLaunchType"));
}
tester.ExpectUniqueSample("Extensions.BookmarkAppLaunchSource",
apps::mojom::AppLaunchSource::kSourceTest,
windowLaunches + tabLaunches);
tester.ExpectBucketCount("Extensions.BookmarkAppLaunchContainer",
apps::mojom::LaunchContainer::kLaunchContainerWindow,
windowLaunches);
tester.ExpectBucketCount("Extensions.BookmarkAppLaunchContainer",
apps::mojom::LaunchContainer::kLaunchContainerTab,
tabLaunches);
tester.ExpectTotalCount("Extensions.BookmarkAppLaunchContainer",
windowLaunches + tabLaunches);
}
} // namespace
namespace web_app {
......@@ -192,6 +227,13 @@ class WebAppEngagementBrowserTest : public WebAppControllerBrowserTestBase {
DISALLOW_COPY_AND_ASSIGN(WebAppEngagementBrowserTest);
};
// TODO(crbug.com/1012171): Migrate all to WebAppEngagementBrowserTest.
class HostedAppEngagementBrowserTest : public WebAppEngagementBrowserTest {
public:
HostedAppEngagementBrowserTest() = default;
~HostedAppEngagementBrowserTest() override = default;
};
IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, AppInWindow) {
base::HistogramTester tester;
......@@ -216,9 +258,10 @@ IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, AppInWindow) {
TestEngagementEventWebAppLaunch(tester, histograms);
TestEngagementEventsAfterLaunch(histograms, app_browser);
ExpectLaunchCounts(tester, /*windowLaunches=*/1, /*tabLaunches=*/0);
}
IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, AppInTab) {
IN_PROC_BROWSER_TEST_P(HostedAppEngagementBrowserTest, AppInTab) {
base::HistogramTester tester;
const GURL example_url = GURL("http://example.org/");
......@@ -241,9 +284,10 @@ IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, AppInTab) {
TestEngagementEventWebAppLaunch(tester, histograms);
TestEngagementEventsAfterLaunch(histograms, browser);
ExpectLaunchCounts(tester, /*windowLaunches=*/0, /*tabLaunches=*/1);
}
IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, AppWithoutScope) {
IN_PROC_BROWSER_TEST_P(HostedAppEngagementBrowserTest, AppWithoutScope) {
base::HistogramTester tester;
const GURL example_url = GURL("http://example.org/");
......@@ -270,6 +314,7 @@ IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, AppWithoutScope) {
TestEngagementEventWebAppLaunch(tester, histograms);
TestEngagementEventsAfterLaunch(histograms, browser);
ExpectLaunchCounts(tester, /*windowLaunches=*/1, /*tabLaunches=*/0);
}
IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, TwoApps) {
......@@ -315,6 +360,7 @@ IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, TwoApps) {
SiteEngagementService::ENGAGEMENT_WEBAPP_SHORTCUT_LAUNCH,
3);
ExpectTotalCounts(tester, ~histograms, 0);
ExpectLaunchCounts(tester, /*windowLaunches=*/3, /*tabLaunches=*/0);
}
IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, ManyUserApps) {
......@@ -355,9 +401,10 @@ IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, ManyUserApps) {
SiteEngagementService::ENGAGEMENT_WEBAPP_SHORTCUT_LAUNCH,
4);
ExpectTotalCounts(tester, ~histograms, 0);
ExpectLaunchCounts(tester, /*windowLaunches=*/4, /*tabLaunches=*/0);
}
IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, DefaultApp) {
IN_PROC_BROWSER_TEST_P(HostedAppEngagementBrowserTest, DefaultApp) {
base::HistogramTester tester;
ASSERT_TRUE(embedded_test_server()->Start());
......@@ -380,9 +427,11 @@ IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, DefaultApp) {
TestEngagementEventWebAppLaunch(tester, histograms);
TestEngagementEventsAfterLaunch(histograms, browser);
ExpectLaunchCounts(tester, /*windowLaunches=*/1, /*tabLaunches=*/0);
}
IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, NavigateAwayFromAppTab) {
IN_PROC_BROWSER_TEST_P(HostedAppEngagementBrowserTest, NavigateAwayFromAppTab) {
base::HistogramTester tester;
const GURL app_url = GURL("http://example.org/app/");
const GURL outer_url = GURL("http://example.org/");
......@@ -412,6 +461,7 @@ IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, NavigateAwayFromAppTab) {
histograms[kHistogramUpToThreeUserInstalledApps] = true;
TestEngagementEventsAfterLaunch(histograms, browser);
}
ExpectLaunchCounts(tester, /*windowLaunches=*/0, /*tabLaunches=*/1);
}
IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, RecordedForNonApps) {
......@@ -436,6 +486,13 @@ IN_PROC_BROWSER_TEST_P(WebAppEngagementBrowserTest, RecordedForNonApps) {
INSTANTIATE_TEST_SUITE_P(
/* no prefix */,
WebAppEngagementBrowserTest,
::testing::Values(ControllerType::kHostedAppController,
ControllerType::kUnifiedControllerWithBookmarkApp,
ControllerType::kUnifiedControllerWithWebApp));
INSTANTIATE_TEST_SUITE_P(
/* no prefix */,
HostedAppEngagementBrowserTest,
::testing::Values(ControllerType::kHostedAppController,
ControllerType::kUnifiedControllerWithBookmarkApp));
......
......@@ -4,17 +4,22 @@
#include "chrome/browser/ui/web_applications/web_app_launch_manager.h"
#include "base/metrics/histogram_macros.h"
#include "chrome/browser/app_mode/app_mode_utils.h"
#include "chrome/browser/apps/app_service/app_launch_params.h"
#include "chrome/browser/banners/app_banner_settings_helper.h"
#include "chrome/browser/engagement/site_engagement_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/web_applications/components/web_app_helpers.h"
#include "chrome/browser/web_applications/components/web_app_install_utils.h"
#include "chrome/browser/web_applications/components/web_app_tab_helper.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_launch/web_launch_files_helper.h"
#include "content/public/browser/render_view_host.h"
#include "extensions/common/constants.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"
......@@ -67,9 +72,6 @@ content::WebContents* ShowWebApplicationWindow(
DCHECK(tab_helper);
tab_helper->SetAppId(app_id);
// TODO(https://crbug.com/988928): Update SiteEngagementService
// and AppBannerSettingsHelper.
browser->window()->Show();
web_contents->SetInitialFocus();
......@@ -96,12 +98,37 @@ content::WebContents* WebAppLaunchManager::OpenApplication(
Browser* browser = CreateWebApplicationWindow(profile(), params.app_id);
return ShowWebApplicationWindow(
params, params.app_id,
params.override_url.is_empty()
const GURL url = params.override_url.is_empty()
? provider_->registrar().GetAppLaunchURL(params.app_id)
: params.override_url,
browser, WindowOpenDisposition::NEW_FOREGROUND_TAB);
: params.override_url;
content::WebContents* web_contents =
ShowWebApplicationWindow(params, params.app_id, url, browser,
WindowOpenDisposition::NEW_FOREGROUND_TAB);
// TODO(crbug.com/1014328): Populate WebApp metrics instead of Extensions.
UMA_HISTOGRAM_ENUMERATION("Extensions.HostedAppLaunchContainer",
params.container);
if (params.container == apps::mojom::LaunchContainer::kLaunchContainerTab) {
UMA_HISTOGRAM_ENUMERATION("Extensions.AppTabLaunchType",
extensions::LAUNCH_TYPE_REGULAR, 100);
}
UMA_HISTOGRAM_ENUMERATION("Extensions.BookmarkAppLaunchSource",
params.source);
UMA_HISTOGRAM_ENUMERATION("Extensions.BookmarkAppLaunchContainer",
params.container);
// Record the launch time in the site engagement service. A recent web
// app launch will provide an engagement boost to the origin.
SiteEngagementService::Get(profile())->SetLastShortcutLaunchTime(web_contents,
url);
// Refresh the app banner added to homescreen event. The user may have
// cleared their browsing data since installing the app, which removes the
// event and will potentially permit a banner to be shown for the site.
RecordAppBanner(web_contents, url);
return web_contents;
}
bool WebAppLaunchManager::OpenApplicationWindow(
......
......@@ -41,8 +41,9 @@ bool WebAppRegistrar::WasExternalAppUninstalledByUser(
}
bool WebAppRegistrar::WasInstalledByUser(const AppId& app_id) const {
// TODO(crbug.com/1012171): Implement.
NOTIMPLEMENTED();
return false;
return true;
}
base::Optional<AppId> WebAppRegistrar::FindAppWithUrlInScope(
......@@ -77,7 +78,17 @@ base::Optional<AppId> WebAppRegistrar::FindAppWithUrlInScope(
int WebAppRegistrar::CountUserInstalledApps() const {
NOTIMPLEMENTED();
return 0;
int num_user_installed = 0;
for (const WebApp& app : AllApps()) {
if (!app.is_locally_installed())
continue;
// TODO(crbug.com/1012171): Exclude if not installed by user.
++num_user_installed;
}
return num_user_installed;
}
std::string WebAppRegistrar::GetAppShortName(const AppId& app_id) const {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment