Skip to content

Commit

Permalink
Bug 1880410 - Clean up parts of search telemetry - r=scunnane
Browse files Browse the repository at this point in the history
Largely non-functional changes:
- The pagehide callback can live in the top level pagehide event.
- Moving the submitted check into the callback will make it easier to refactor
  the helper that adds event listeners.
- Changing the callbacks to use target as the name will make it easier to
  remember that the target is potentially overwritable.
- waitForPageWithAction can be used in future tests.

Differential Revision: https://phabricator.services.mozilla.com/D202036
  • Loading branch information
jamesteow committed Feb 23, 2024
1 parent 9afd5fd commit 114ca3d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
36 changes: 13 additions & 23 deletions browser/actors/SearchSERPTelemetryChild.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -834,11 +834,8 @@ class SearchAdImpression {

for (let element of elements) {
let clickCallback = () => {
if (clickAction == "submitted") {
documentToSubmitMap.set(document, true);
}
callback({
type,
target: type,
url,
action: clickAction,
});
Expand All @@ -847,11 +844,8 @@ class SearchAdImpression {

let keydownCallback = event => {
if (event.key == "Enter") {
if (keydownEnterAction == "submitted") {
documentToSubmitMap.set(document, true);
}
callback({
type,
target: type,
url,
action: keydownEnterAction,
});
Expand All @@ -865,20 +859,6 @@ class SearchAdImpression {
});
}

document.ownerGlobal.addEventListener(
"pagehide",
() => {
let callbacks = documentToRemoveEventListenersMap.get(document);
if (callbacks) {
for (let removeEventListenerCallback of callbacks) {
removeEventListenerCallback();
}
documentToRemoveEventListenersMap.delete(document);
}
},
{ once: true }
);

// The map might have entries from previous callers, so we must ensure
// we don't discard existing event listener callbacks.
if (documentToRemoveEventListenersMap.has(document)) {
Expand Down Expand Up @@ -1150,8 +1130,11 @@ export class SearchSERPTelemetryChild extends JSWindowActorChild {
let timerId = Glean.serp.categorizationDuration.start();

let pageActionCallback = info => {
if (info.action == "submitted") {
documentToSubmitMap.set(doc, true);
}
this.sendAsyncMessage("SearchTelemetry:Action", {
type: info.type,
target: info.target,
url: info.url,
action: info.action,
});
Expand Down Expand Up @@ -1288,6 +1271,13 @@ export class SearchSERPTelemetryChild extends JSWindowActorChild {
break;
}
case "pagehide": {
let callbacks = documentToRemoveEventListenersMap.get(this.document);
if (callbacks) {
for (let removeEventListenerCallback of callbacks) {
removeEventListenerCallback();
}
documentToRemoveEventListenersMap.delete(this.document);
}
this.#cancelCheck();
break;
}
Expand Down
14 changes: 8 additions & 6 deletions browser/components/search/SearchSERPTelemetry.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1630,8 +1630,8 @@ class ContentHandler {
*
* @param {object} info
* The search provider infomation for the page.
* @param {string} info.type
* The component type that was clicked on.
* @param {string} info.target
* The target component that was interacted with.
* @param {string} info.action
* The action taken on the page.
* @param {object} browser
Expand All @@ -1644,22 +1644,23 @@ class ContentHandler {
}
let telemetryState = item.browserTelemetryStateMap.get(browser);
let impressionId = telemetryState?.impressionId;
if (info.type && impressionId) {
if (info.target && impressionId) {
lazy.logConsole.debug(`Recorded page action:`, {
impressionId: telemetryState.impressionId,
type: info.type,
target: info.target,
action: info.action,
});
Glean.serp.engagement.record({
impression_id: impressionId,
action: info.action,
target: info.type,
target: info.target,
});
impressionIdsWithoutEngagementsSet.delete(impressionId);
// In-content searches are not be categorized with a type, so they will
// not be picked up in the network processes.
if (
info.type == SearchSERPTelemetryUtils.COMPONENTS.INCONTENT_SEARCHBOX &&
info.target ==
SearchSERPTelemetryUtils.COMPONENTS.INCONTENT_SEARCHBOX &&
info.action == SearchSERPTelemetryUtils.ACTIONS.SUBMITTED
) {
telemetryState.searchBoxSubmitted = true;
Expand All @@ -1668,6 +1669,7 @@ class ContentHandler {
SearchSERPTelemetryUtils.INCONTENT_SOURCES.SEARCHBOX
);
}
Services.obs.notifyObservers(null, "reported-page-with-action");
} else {
lazy.logConsole.warn(
"Expected to report a",
Expand Down
4 changes: 4 additions & 0 deletions browser/components/search/test/browser/telemetry/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ function assertCategorizationValues(expectedResults) {
}
}

function waitForPageWithAction() {
return TestUtils.topicObserved("reported-page-with-action");
}

function waitForPageWithAdImpressions() {
return TestUtils.topicObserved("reported-page-with-ad-impressions");
}
Expand Down

0 comments on commit 114ca3d

Please sign in to comment.