1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_LENS_LENS_SESSION_METRICS_LOGGER_H_
#define CHROME_BROWSER_UI_LENS_LENS_SESSION_METRICS_LOGGER_H_
#include <optional>
#include <string>
#include <vector>
#include "base/memory/weak_ptr.h"
#include "chrome/browser/lens/core/mojom/text.mojom.h"
#include "components/lens/lens_overlay_dismissal_source.h"
#include "components/lens/lens_overlay_invocation_source.h"
#include "components/lens/lens_overlay_metrics.h"
#include "components/lens/lens_overlay_mime_type.h"
namespace content {
class WebContents;
}
namespace lens {
struct ContextualSearchboxSessionEndMetrics;
// This class is responsible for tracking session-specific metrics for the Lens
// overlay and logging them at appropriate times, primarily at the end of a
// session.
class LensSessionMetricsLogger {
public:
LensSessionMetricsLogger();
~LensSessionMetricsLogger();
LensSessionMetricsLogger(const LensSessionMetricsLogger&) = delete;
LensSessionMetricsLogger& operator=(const LensSessionMetricsLogger&) = delete;
// Call when a new Lens overlay session starts.
void OnSessionStart(LensOverlayInvocationSource invocation_source,
content::WebContents* tab_web_contents);
// Call to update navigation metrics.
void OnPageNavigation();
// Call when a search is performed within this session.
void OnSearchPerformed();
// Stores the type of the page content extracted from the page on invocation.
void OnInitialPageContentRetrieved(lens::MimeType page_content_type);
// Stores the type of the page content extracted from the page on follow up
// page content retrieval.
void OnFollowUpPageContentRetrieved(lens::MimeType page_content_type);
// Called when the contextual searchbox is shown.
void OnContextualSearchboxShown();
// Called when a contextual searchbox query is issued.
// `is_zero_prefix_suggestion` is true if the query was issued from a user
// selecting a zero prefix suggestion. `is_initial_query` is true if the query
// was issued from the initial state of the lens overlay, aka not from the
// side panel.
void OnContextualSearchboxQueryIssued(bool is_zero_prefix_suggestion,
bool is_initial_query);
// Called when the searchbox is focused. Used to log metrics relating to focus
// events of the CSB.
void OnSearchboxFocused();
// Called when the zero prefix suggestions are shown to the user.
void OnZeroSuggestShown(bool is_initial_query);
// Records Lens invocation.
void RecordInvocation();
// Records UMA and UKM metrics for time to first interaction. Not recorded
// when invocation source is an image's content area menu because in this
// case the time to first interaction is essentially zero.
void RecordTimeToFirstInteraction(
lens::LensOverlayFirstInteractionType interaction_type);
// Records UMA and UKM metrics for dismissal and end of session metrics.
// This includes dismissal source, session length, and whether a search was
// recorded in the session.
void RecordEndOfSessionMetrics(
lens::LensOverlayDismissalSource dismissal_source);
// Records the UMA for the first time the contextual searchbox is focused
// after the page has been navigated.
void RecordContextualSearchboxTimeToFocusAfterNavigation();
// Records the UMA for the first time the user interacts with the contextual
// searchbox after the page has been navigated.
void RecordContextualSearchboxTimeToInteractionAfterNavigation();
// Returns the time at which the overlay was invoked.
void GetInvocationTime();
private:
// Invocation source for the lens overlay.
lens::LensOverlayInvocationSource invocation_source_ =
lens::LensOverlayInvocationSource::kAppMenu;
// The time at which the overlay was invoked. Used to compute timing metrics.
// Assumed to be when OnSessionStart is called.
base::TimeTicks invocation_time_;
// The time at which the live page navigated while in the contextual searchbox
// flow. Used to compute timing metrics. Is empty if the user is not in the
// contextual searchbox flow, or this navigation has already been recorded.
std::optional<base::TimeTicks> last_navigation_time_;
// Whether the contextual searchbox has been focused since the last page
// navigation.
bool contextual_searchbox_focused_after_navigation_ = false;
// Indicates whether a search has been performed in the current session. Used
// to record success/abandonment rate, as defined by whether or not a search
// was performed.
bool search_performed_in_session_ = false;
// The UKM source id of the tab Lens was invoked on.
ukm::SourceId ukm_source_id_ = ukm::kInvalidSourceId;
// The type of the page content extracted from the page when the lens overlay
// was initialized. This is used when recording contextual searchbox metrics
// at the end of sessions, since the initialization data can change on page
// contextualization updates and these metrics only want to record the initial
// invocation page content type.
lens::MimeType initial_page_content_type_ = lens::MimeType::kUnknown;
// The type of the document that the lens overlay was initialized on as
// determined by the mime type reported by the tab web contents. This differs
// from initial_page_content_type_ in that the document type is the type of
// the top level document, while the intial_page_content_type_ is the type of
// the content extracted from the page that we are contextualizing to. This is
// used when recording invocation document metrics, since the document type
// can change on page contextualization updates.
lens::MimeType initial_document_type_ = lens::MimeType::kUnknown;
// The type of the page content extracted from the page currently. Is kept up
// to date as the page content changes.
lens::MimeType current_page_content_type_ = lens::MimeType::kUnknown;
// Metrics for the contextual searchbox that will be recorded at the end of a
// session.
ContextualSearchboxSessionEndMetrics csb_session_end_metrics_;
// Must be the last member.
base::WeakPtrFactory<LensSessionMetricsLogger> weak_factory_{this};
};
} // namespace lens
#endif // CHROME_BROWSER_UI_LENS_LENS_SESSION_METRICS_LOGGER_H_
|