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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
ContextId: "moz-src:///browser/modules/ContextId.sys.mjs",
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
SearchSERPTelemetry:
"moz-src:///browser/components/search/SearchSERPTelemetry.sys.mjs",
SearchUtils: "moz-src:///toolkit/components/search/SearchUtils.sys.mjs",
UrlbarSearchUtils: "resource:///modules/UrlbarSearchUtils.sys.mjs",
});
/**
* This class handles saving search telemetry related to the url bar,
* search bar and other areas as per the sources above.
*/
class BrowserSearchTelemetryHandler {
/**
* A map of known search origins. The values of this map should be used for all
* current telemetry, except for sap.deprecatedCounts.
*
* The keys of this map are used in the calling code to recordSearch, and in
* the sap.deprecatedCounts labelled counter (and the mirrored SEARCH_COUNTS
* histogram).
*
* When legacy telemetry stops being reported, we should remove this map, and
* update the callers to use the values directly. We might still want to keep
* a list of valid sources, to help ensure that telemetry reporting is updated
* correctly if new sources are added.
*/
KNOWN_SEARCH_SOURCES = new Map([
["abouthome", "about_home"],
["contextmenu", "contextmenu"],
["contextmenu_visual", "contextmenu_visual"],
["newtab", "about_newtab"],
["searchbar", "searchbar"],
["system", "system"],
["urlbar", "urlbar"],
["urlbar-handoff", "urlbar_handoff"],
["urlbar-persisted", "urlbar_persisted"],
["urlbar-searchmode", "urlbar_searchmode"],
["webextension", "webextension"],
]);
/**
* Determines if we should record a search for this browser instance.
* Private Browsing mode is normally skipped.
*
* @param {XULBrowserElement} browser
* The browser where the search was loaded.
* @returns {boolean}
* True if the search should be recorded, false otherwise.
*/
shouldRecordSearchCount(browser) {
return (
!lazy.PrivateBrowsingUtils.isWindowPrivate(browser.ownerGlobal) ||
!Services.prefs.getBoolPref("browser.engagement.search_counts.pbm", false)
);
}
/**
* Records the method by which the user selected a result from the searchbar.
*
* @param {Event} event
* The event that triggered the selection.
* @param {number} index
* The index that the user chose in the popup, or -1 if there wasn't a
* selection.
*/
recordSearchSuggestionSelectionMethod(event, index) {
// command events are from the one-off context menu. Treat them as clicks.
// Note that we only care about MouseEvent subclasses here when the
// event type is "click", or else the subclasses are associated with
// non-click interactions.
let isClick =
event &&
(ChromeUtils.getClassName(event) == "MouseEvent" ||
event.type == "click" ||
event.type == "command");
let category;
if (isClick) {
category = "click";
} else if (index >= 0) {
category = "enterSelection";
} else {
category = "enter";
}
Glean.searchbar.selectedResultMethod[category].add(1);
}
/**
* Records entry into the Urlbar's search mode.
*
* Telemetry records only which search mode is entered and how it was entered.
* It does not record anything pertaining to searches made within search mode.
*
* @param {object} searchMode
* A search mode object. See UrlbarInput.setSearchMode documentation for
* details.
*/
recordSearchMode(searchMode) {
// Search mode preview is not search mode. Recording it would just create
// noise.
if (searchMode.isPreview) {
return;
}
let label = lazy.UrlbarSearchUtils.getSearchModeScalarKey(searchMode);
let name = searchMode.entry.replace(/_([a-z])/g, (m, p) => p.toUpperCase());
Glean.urlbarSearchmode[name]?.[label].add(1);
}
/**
* The main entry point for recording search related Telemetry. This includes
* search counts and engagement measurements.
*
* Telemetry records only search counts per engine and action origin, but
* nothing pertaining to the search contents themselves.
*
* @param {XULBrowserElement} browser
* The browser where the search originated.
* @param {nsISearchEngine} engine
* The engine handling the search.
* @param {string} source
* Where the search originated from. See KNOWN_SEARCH_SOURCES for allowed
* values.
* @param {object} [details] Options object.
* @param {boolean} [details.isOneOff=false]
* true if this event was generated by a one-off search.
* @param {boolean} [details.isSuggestion=false]
* true if this event was generated by a suggested search.
* @param {boolean} [details.isFormHistory=false]
* true if this event was generated by a form history result.
* @param {string} [details.alias=null]
* The search engine alias used in the search, if any.
* @param {string} [details.newtabSessionId=undefined]
* The newtab session that prompted this search, if any.
* @param {string} [details.searchUrlType=undefined]
* A `SearchUtils.URL_TYPE` value that indicates the type of search.
* Defaults to `SearchUtils.URL_TYPE.SEARCH`, a plain old search.
* @throws if source is not in the known sources list.
*/
recordSearch(browser, engine, source, details = {}) {
if (engine.clickUrl) {
this.#reportSearchInGlean(engine.clickUrl);
}
try {
if (!this.shouldRecordSearchCount(browser)) {
return;
}
if (!this.KNOWN_SEARCH_SOURCES.has(source)) {
console.error("Unknown source for search: ", source);
return;
}
if (source != "contextmenu_visual") {
const countIdPrefix = `${engine.telemetryId}.`;
const countIdSource = countIdPrefix + source;
// NOTE: When removing the sap.deprecatedCounts telemetry, see the note
// above KNOWN_SEARCH_SOURCES.
if (
details.alias &&
engine.isConfigEngine &&
engine.aliases.includes(details.alias)
) {
// This is a keyword search using a config engine.
// Record the source as "alias", not "urlbar".
Glean.sap.deprecatedCounts[countIdPrefix + "alias"].add();
} else {
Glean.sap.deprecatedCounts[countIdSource].add();
}
}
// When an engine is overridden by a third party, then we report the
// override and skip reporting the partner code, since we don't have
// a requirement to report the partner code in that case.
let isOverridden = !!engine.overriddenById;
let searchUrlType =
details.searchUrlType ?? lazy.SearchUtils.URL_TYPE.SEARCH;
// Strict equality is used because we want to only match against the
// empty string and not other values. We would have `engine.partnerCode`
// return `undefined`, but the XPCOM interfaces force us to return an
// empty string.
let reportPartnerCode =
!isOverridden &&
engine.partnerCode !== "" &&
!engine.wrappedJSObject.getURLOfType(searchUrlType)
?.excludePartnerCodeFromTelemetry;
Glean.sap.counts.record({
source: this.KNOWN_SEARCH_SOURCES.get(source),
provider_id: engine.isConfigEngine ? engine.id : "other",
provider_name: engine.name,
// If no code is reported, we must returned undefined, Glean will then
// not report the field.
partner_code: reportPartnerCode ? engine.partnerCode : undefined,
overridden_by_third_party: isOverridden.toString(),
});
// Dispatch the search signal to other handlers.
switch (source) {
case "urlbar":
case "searchbar":
case "urlbar-searchmode":
case "urlbar-persisted":
case "urlbar-handoff":
this._handleSearchAndUrlbar(browser, engine, source, details);
break;
case "abouthome":
case "newtab":
this._recordSearch(browser, engine, source, "enter");
break;
default:
this._recordSearch(browser, engine, source);
break;
}
if (["urlbar-handoff", "abouthome", "newtab"].includes(source)) {
Glean.newtabSearch.issued.record({
newtab_visit_id: details.newtabSessionId,
search_access_point: this.KNOWN_SEARCH_SOURCES.get(source),
telemetry_id: engine.telemetryId,
});
lazy.SearchSERPTelemetry.recordBrowserNewtabSession(
browser,
details.newtabSessionId
);
}
} catch (ex) {
// Catch any errors here, so that search actions are not broken if
// telemetry is broken for some reason.
console.error(ex);
}
}
/**
* Records visits to a search engine's search form.
*
* @param {nsISearchEngine} engine
* The engine whose search form is being visited.
* @param {string} source
* Where the search form was opened from.
* This can be "urlbar" or "searchbar".
*/
recordSearchForm(engine, source) {
Glean.sap.searchFormCounts.record({
source,
provider_id: engine.isConfigEngine ? engine.id : "other",
});
}
/**
* Records an impression of a search access point.
*
* @param {XULBrowserElement} browser
* The browser associated with the SAP.
* @param {nsISearchEngine|null} engine
* The engine handling the search, or null if this doesn't apply to the SAP
* (e.g., the engine isn't known or selected yet). The counter's label will
* be `engine.id` if `engine` is a non-null, app-provided engine. Otherwise
* the label will be "none".
* @param {string} source
* The name of the SAP. See `KNOWN_SEARCH_SOURCES` for allowed values.
*/
recordSapImpression(browser, engine, source) {
if (!this.shouldRecordSearchCount(browser)) {
return;
}
if (!this.KNOWN_SEARCH_SOURCES.has(source)) {
console.error("Unknown source for SAP impression:", source);
return;
}
let scalarSource = this.KNOWN_SEARCH_SOURCES.get(source);
let name = scalarSource.replace(/_([a-z])/g, (m, p) => p.toUpperCase());
let label = engine?.isConfigEngine ? engine.id : "none";
Glean.sapImpressionCounts[name][label].add(1);
}
/**
* This function handles the "urlbar", "urlbar-oneoff", "searchbar" and
* "searchbar-oneoff" sources.
*
* @param {XULBrowserElement} browser
* The browser where the search originated.
* @param {nsISearchEngine} engine
* The engine handling the search.
* @param {string} source
* Where the search originated from.
* @param {object} details
* See {@link BrowserSearchTelemetryHandler.recordSearch}
*/
_handleSearchAndUrlbar(browser, engine, source, details) {
const isOneOff = !!details.isOneOff;
let action = "enter";
if (isOneOff) {
action = "oneoff";
} else if (details.isFormHistory) {
action = "formhistory";
} else if (details.isSuggestion) {
action = "suggestion";
} else if (details.alias) {
action = "alias";
}
this._recordSearch(browser, engine, source, action);
}
_recordSearch(browser, engine, source, action = null) {
let scalarSource = this.KNOWN_SEARCH_SOURCES.get(source);
lazy.SearchSERPTelemetry.recordBrowserSource(browser, scalarSource);
let label = action ? "search_" + action : "search";
let name = scalarSource.replace(/_([a-z])/g, (m, p) => p.toUpperCase());
Glean.browserEngagementNavigation[name][label].add(1);
}
/**
* Records the search in Glean for contextual services.
*
* @param {string} reportingUrl
* The url to be sent to contextual services.
*/
async #reportSearchInGlean(reportingUrl) {
let defaultValuesByGleanKey = {
contextId: await lazy.ContextId.request(),
};
let sendGleanPing = valuesByGleanKey => {
valuesByGleanKey = { ...defaultValuesByGleanKey, ...valuesByGleanKey };
for (let [gleanKey, value] of Object.entries(valuesByGleanKey)) {
let glean = Glean.searchWith[gleanKey];
if (value !== undefined && value !== "") {
glean.set(value);
}
}
GleanPings.searchWith.submit();
};
sendGleanPing({
reportingUrl,
});
}
}
export var BrowserSearchTelemetry = new BrowserSearchTelemetryHandler();
|