Commit d4e2df5e authored by Donn Denman's avatar Donn Denman Committed by Commit Bot

[EoC] Integrate the Metrics Reporter into the CCSS.

Updates the ContextualSuggestionsService to call the new
ContextualSuggestionsMetricsReporter to do basic metric
reporting.

BUG=824185

Change-Id: I6db197cd0390abcda1337569e30b2f42961cac0f
Reviewed-on: https://chromium-review.googlesource.com/994709Reviewed-by: default avatarFilip Gorski <fgorski@chromium.org>
Commit-Queue: Donn Denman <donnd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#548618}
parent a509a21b
...@@ -94,9 +94,9 @@ class ContextualSuggestionsBridge { ...@@ -94,9 +94,9 @@ class ContextualSuggestionsBridge {
* Reports an event happening in the context of the current URL. * Reports an event happening in the context of the current URL.
* *
* @param webContents Web contents with the document for which event is reported. * @param webContents Web contents with the document for which event is reported.
* @param eventId Id of the reported event. * @param eventId The Id of the reported event as a {@link ContextualSuggestionsEvent} integer.
*/ */
void reportEvent(WebContents webContents, int eventId) { void reportEvent(WebContents webContents, @ContextualSuggestionsEvent int eventId) {
assert mNativeContextualSuggestionsBridge != 0; assert mNativeContextualSuggestionsBridge != 0;
assert webContents != null && !webContents.isDestroyed(); assert webContents != null && !webContents.isDestroyed();
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "chrome/browser/profiles/profile_android.h" #include "chrome/browser/profiles/profile_android.h"
#include "components/ntp_snippets/category.h" #include "components/ntp_snippets/category.h"
#include "components/ntp_snippets/content_suggestions_service.h" #include "components/ntp_snippets/content_suggestions_service.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_metrics_reporter.h"
#include "components/ukm/content/source_url_recorder.h" #include "components/ukm/content/source_url_recorder.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "jni/ContextualSuggestionsBridge_jni.h" #include "jni/ContextualSuggestionsBridge_jni.h"
...@@ -138,8 +139,10 @@ void ContextualSuggestionsBridge::ReportEvent( ...@@ -138,8 +139,10 @@ void ContextualSuggestionsBridge::ReportEvent(
ukm::SourceId ukm_source_id = ukm::SourceId ukm_source_id =
ukm::GetSourceIdForWebContentsDocument(web_contents); ukm::GetSourceIdForWebContentsDocument(web_contents);
contextual_content_suggestions_service_->ReportEvent(ukm_source_id, contextual_suggestions::ContextualSuggestionsEvent event =
j_event_id); static_cast<contextual_suggestions::ContextualSuggestionsEvent>(
j_event_id);
contextual_content_suggestions_service_->ReportEvent(ukm_source_id, event);
} }
void ContextualSuggestionsBridge::OnSuggestionsAvailable( void ContextualSuggestionsBridge::OnSuggestionsAvailable(
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/ntp_snippets/contextual/contextual_content_suggestions_service.h" #include "components/ntp_snippets/contextual/contextual_content_suggestions_service.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_fetcher_impl.h" #include "components/ntp_snippets/contextual/contextual_suggestions_fetcher_impl.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_metrics_reporter.h"
#include "components/ntp_snippets/remote/cached_image_fetcher.h" #include "components/ntp_snippets/remote/cached_image_fetcher.h"
#include "components/ntp_snippets/remote/remote_suggestions_database.h" #include "components/ntp_snippets/remote/remote_suggestions_database.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
...@@ -107,10 +108,12 @@ ContextualContentSuggestionsServiceFactory::BuildServiceInstanceFor( ...@@ -107,10 +108,12 @@ ContextualContentSuggestionsServiceFactory::BuildServiceInstanceFor(
std::make_unique<suggestions::ImageDecoderImpl>(), std::make_unique<suggestions::ImageDecoderImpl>(),
request_context.get()), request_context.get()),
pref_service, contextual_suggestions_database.get()); pref_service, contextual_suggestions_database.get());
auto metrics_reporter = std::make_unique<
contextual_suggestions::ContextualSuggestionsMetricsReporter>();
auto* service = new ContextualContentSuggestionsService( auto* service = new ContextualContentSuggestionsService(
std::move(contextual_suggestions_fetcher), std::move(contextual_suggestions_fetcher),
std::move(cached_image_fetcher), std::move(cached_image_fetcher),
std::move(contextual_suggestions_database)); std::move(contextual_suggestions_database), std::move(metrics_reporter));
return service; return service;
} }
...@@ -22,6 +22,8 @@ static const char kSamplePeekText[] = "Peek text"; ...@@ -22,6 +22,8 @@ static const char kSamplePeekText[] = "Peek text";
static const char kSampleClusterTitle[] = "Cluster title filler"; static const char kSampleClusterTitle[] = "Cluster title filler";
} // namespace } // namespace
using contextual_suggestions::ContextualSuggestionsMetricsReporter;
ContextualContentSuggestionsService::Cluster::Cluster() = default; ContextualContentSuggestionsService::Cluster::Cluster() = default;
ContextualContentSuggestionsService::Cluster::Cluster(Cluster&& other) = ContextualContentSuggestionsService::Cluster::Cluster(Cluster&& other) =
...@@ -33,12 +35,15 @@ ContextualContentSuggestionsService::ContextualContentSuggestionsService( ...@@ -33,12 +35,15 @@ ContextualContentSuggestionsService::ContextualContentSuggestionsService(
std::unique_ptr<ContextualSuggestionsFetcher> std::unique_ptr<ContextualSuggestionsFetcher>
contextual_suggestions_fetcher, contextual_suggestions_fetcher,
std::unique_ptr<CachedImageFetcher> image_fetcher, std::unique_ptr<CachedImageFetcher> image_fetcher,
std::unique_ptr<RemoteSuggestionsDatabase> contextual_suggestions_database) std::unique_ptr<RemoteSuggestionsDatabase> contextual_suggestions_database,
std::unique_ptr<ContextualSuggestionsMetricsReporter> metrics_reporter)
: contextual_suggestions_database_( : contextual_suggestions_database_(
std::move(contextual_suggestions_database)), std::move(contextual_suggestions_database)),
contextual_suggestions_fetcher_( contextual_suggestions_fetcher_(
std::move(contextual_suggestions_fetcher)), std::move(contextual_suggestions_fetcher)),
image_fetcher_(std::move(image_fetcher)) {} image_fetcher_(std::move(image_fetcher)),
metrics_reporter_(std::move(metrics_reporter)),
last_ukm_source_id_(ukm::kInvalidSourceId) {}
ContextualContentSuggestionsService::~ContextualContentSuggestionsService() = ContextualContentSuggestionsService::~ContextualContentSuggestionsService() =
default; default;
...@@ -82,7 +87,19 @@ void ContextualContentSuggestionsService::FetchContextualSuggestionImage( ...@@ -82,7 +87,19 @@ void ContextualContentSuggestionsService::FetchContextualSuggestionImage(
void ContextualContentSuggestionsService::ReportEvent( void ContextualContentSuggestionsService::ReportEvent(
ukm::SourceId ukm_source_id, ukm::SourceId ukm_source_id,
int event_id) {} contextual_suggestions::ContextualSuggestionsEvent event) {
DCHECK(ukm_source_id != ukm::kInvalidSourceId);
// Flush the previous page (if any) and setup the new page.
if (ukm_source_id != last_ukm_source_id_) {
if (last_ukm_source_id_ != ukm::kInvalidSourceId)
metrics_reporter_->Flush();
last_ukm_source_id_ = ukm_source_id;
metrics_reporter_->SetupForPage(ukm_source_id);
}
metrics_reporter_->RecordEvent(event);
}
// TODO(gaschler): Cache contextual suggestions at run-time. // TODO(gaschler): Cache contextual suggestions at run-time.
void ContextualContentSuggestionsService::DidFetchContextualSuggestions( void ContextualContentSuggestionsService::DidFetchContextualSuggestions(
...@@ -118,4 +135,10 @@ void ContextualContentSuggestionsService:: ...@@ -118,4 +135,10 @@ void ContextualContentSuggestionsService::
std::move(callback).Run(kSamplePeekText, std::move(clusters)); std::move(callback).Run(kSamplePeekText, std::move(clusters));
} }
void ContextualContentSuggestionsService::Shutdown() {
if (last_ukm_source_id_ != ukm::kInvalidSourceId)
metrics_reporter_->Flush();
last_ukm_source_id_ = ukm::kInvalidSourceId;
}
} // namespace ntp_snippets } // namespace ntp_snippets
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "components/ntp_snippets/callbacks.h" #include "components/ntp_snippets/callbacks.h"
#include "components/ntp_snippets/content_suggestion.h" #include "components/ntp_snippets/content_suggestion.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_fetcher.h" #include "components/ntp_snippets/contextual/contextual_suggestions_fetcher.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_metrics_reporter.h"
#include "services/metrics/public/cpp/ukm_source_id.h" #include "services/metrics/public/cpp/ukm_source_id.h"
namespace ntp_snippets { namespace ntp_snippets {
...@@ -47,7 +48,10 @@ class ContextualContentSuggestionsService : public KeyedService { ...@@ -47,7 +48,10 @@ class ContextualContentSuggestionsService : public KeyedService {
contextual_suggestions_fetcher, contextual_suggestions_fetcher,
std::unique_ptr<CachedImageFetcher> image_fetcher, std::unique_ptr<CachedImageFetcher> image_fetcher,
std::unique_ptr<RemoteSuggestionsDatabase> std::unique_ptr<RemoteSuggestionsDatabase>
contextual_suggestions_database); contextual_suggestions_database,
std::unique_ptr<
contextual_suggestions::ContextualSuggestionsMetricsReporter>
metrics_reporter);
~ContextualContentSuggestionsService() override; ~ContextualContentSuggestionsService() override;
using FetchContextualSuggestionsCallback = using FetchContextualSuggestionsCallback =
...@@ -75,8 +79,11 @@ class ContextualContentSuggestionsService : public KeyedService { ...@@ -75,8 +79,11 @@ class ContextualContentSuggestionsService : public KeyedService {
ImageFetchedCallback callback); ImageFetchedCallback callback);
// Used to report events using various metrics (e.g. UMA, UKM). // Used to report events using various metrics (e.g. UMA, UKM).
// TODO(donnd): Change type of event ID, implement. void ReportEvent(ukm::SourceId sourceId,
void ReportEvent(ukm::SourceId sourceId, int event_id); contextual_suggestions::ContextualSuggestionsEvent event);
// KeyedService overrides.
void Shutdown() override;
private: private:
void DidFetchContextualSuggestions( void DidFetchContextualSuggestions(
...@@ -99,6 +106,13 @@ class ContextualContentSuggestionsService : public KeyedService { ...@@ -99,6 +106,13 @@ class ContextualContentSuggestionsService : public KeyedService {
std::unique_ptr<CachedImageFetcher> image_fetcher_; std::unique_ptr<CachedImageFetcher> image_fetcher_;
std::unique_ptr<contextual_suggestions::ContextualSuggestionsMetricsReporter>
metrics_reporter_;
// The most recent SourceId in use by metrics_reporter_, or
// ukm::kInvalidSourceId.
ukm::SourceId last_ukm_source_id_;
// Look up by ContentSuggestion::ID::id_within_category() aka std::string to // Look up by ContentSuggestion::ID::id_within_category() aka std::string to
// get image URL. // get image URL.
std::map<std::string, GURL> image_url_by_id_; std::map<std::string, GURL> image_url_by_id_;
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "components/ntp_snippets/content_suggestion.h" #include "components/ntp_snippets/content_suggestion.h"
#include "components/ntp_snippets/contextual/contextual_suggestion.h" #include "components/ntp_snippets/contextual/contextual_suggestion.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_fetcher.h" #include "components/ntp_snippets/contextual/contextual_suggestions_fetcher.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_metrics_reporter.h"
#include "components/ntp_snippets/remote/cached_image_fetcher.h" #include "components/ntp_snippets/remote/cached_image_fetcher.h"
#include "components/ntp_snippets/remote/json_to_categories.h" #include "components/ntp_snippets/remote/json_to_categories.h"
#include "components/ntp_snippets/remote/remote_suggestions_database.h" #include "components/ntp_snippets/remote/remote_suggestions_database.h"
...@@ -125,10 +126,13 @@ class ContextualContentSuggestionsServiceTest : public testing::Test { ...@@ -125,10 +126,13 @@ class ContextualContentSuggestionsServiceTest : public testing::Test {
std::unique_ptr<FakeContextualSuggestionsFetcher> fetcher = std::unique_ptr<FakeContextualSuggestionsFetcher> fetcher =
std::make_unique<FakeContextualSuggestionsFetcher>(); std::make_unique<FakeContextualSuggestionsFetcher>();
fetcher_ = fetcher.get(); fetcher_ = fetcher.get();
auto metrics_reporter = std::make_unique<
contextual_suggestions::ContextualSuggestionsMetricsReporter>();
source_ = std::make_unique<ContextualContentSuggestionsService>( source_ = std::make_unique<ContextualContentSuggestionsService>(
std::move(fetcher), std::move(fetcher),
std::make_unique<FakeCachedImageFetcher>(&pref_service_), std::make_unique<FakeCachedImageFetcher>(&pref_service_),
std::unique_ptr<RemoteSuggestionsDatabase>()); std::unique_ptr<RemoteSuggestionsDatabase>(),
std::move(metrics_reporter));
} }
FakeContextualSuggestionsFetcher* fetcher() { return fetcher_; } FakeContextualSuggestionsFetcher* fetcher() { return fetcher_; }
......
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