tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

event-telemetry.js (4963B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const {
      8  TOGGLE_REQUEST_FILTER_TYPE,
      9  ENABLE_REQUEST_FILTER_TYPE_ONLY,
     10  SET_REQUEST_FILTER_TEXT,
     11  SELECT_DETAILS_PANEL_TAB,
     12  SEND_CUSTOM_REQUEST,
     13  ENABLE_PERSISTENT_LOGS,
     14  MSG_SELECT,
     15 } = require("resource://devtools/client/netmonitor/src/constants.js");
     16 
     17 const {
     18  CHANGE_NETWORK_THROTTLING,
     19 } = require("resource://devtools/client/shared/components/throttling/actions.js");
     20 
     21 /**
     22 * Event telemetry middleware is responsible for logging
     23 * various events to telemetry. This helps to track Network
     24 * panel usage.
     25 */
     26 function eventTelemetryMiddleware(connector, telemetry) {
     27  return store => next => action => {
     28    const oldState = store.getState();
     29    const res = next(action);
     30    const toolbox = connector.getToolbox();
     31    if (!toolbox) {
     32      return res;
     33    }
     34 
     35    if (action.skipTelemetry) {
     36      return res;
     37    }
     38 
     39    const state = store.getState();
     40 
     41    const filterChangeActions = [
     42      TOGGLE_REQUEST_FILTER_TYPE,
     43      ENABLE_REQUEST_FILTER_TYPE_ONLY,
     44      SET_REQUEST_FILTER_TEXT,
     45    ];
     46 
     47    // Record telemetry event when filter changes.
     48    if (filterChangeActions.includes(action.type)) {
     49      filterChange({
     50        action,
     51        state,
     52        oldState,
     53        telemetry,
     54      });
     55    }
     56 
     57    // Record telemetry event when side panel is selected.
     58    if (action.type == SELECT_DETAILS_PANEL_TAB) {
     59      sidePanelChange({
     60        state,
     61        oldState,
     62        telemetry,
     63      });
     64    }
     65 
     66    // Record telemetry event when a request is resent.
     67    if (action.type == SEND_CUSTOM_REQUEST) {
     68      sendCustomRequest({
     69        telemetry,
     70      });
     71    }
     72 
     73    // Record telemetry event when throttling is changed.
     74    if (action.type == CHANGE_NETWORK_THROTTLING) {
     75      throttlingChange({
     76        action,
     77        telemetry,
     78      });
     79    }
     80 
     81    // Record telemetry event when log persistence changes.
     82    if (action.type == ENABLE_PERSISTENT_LOGS) {
     83      persistenceChange({
     84        telemetry,
     85        state,
     86      });
     87    }
     88 
     89    // Record telemetry event when message is selected.
     90    if (action.type == MSG_SELECT) {
     91      selectMessage({
     92        telemetry,
     93      });
     94    }
     95 
     96    return res;
     97  };
     98 }
     99 
    100 /**
    101 * This helper function is executed when filter related action is fired.
    102 * It's responsible for recording "filters_changed" telemetry event.
    103 */
    104 function filterChange({ action, state, oldState, telemetry }) {
    105  const oldFilterState = oldState.filters;
    106  const filterState = state.filters;
    107  const activeFilters = [];
    108  const inactiveFilters = [];
    109 
    110  for (const [key, value] of Object.entries(filterState.requestFilterTypes)) {
    111    if (value) {
    112      activeFilters.push(key);
    113    } else {
    114      inactiveFilters.push(key);
    115    }
    116  }
    117 
    118  let trigger;
    119  if (
    120    action.type === TOGGLE_REQUEST_FILTER_TYPE ||
    121    action.type === ENABLE_REQUEST_FILTER_TYPE_ONLY
    122  ) {
    123    trigger = action.filter;
    124  } else if (action.type === SET_REQUEST_FILTER_TEXT) {
    125    if (
    126      oldFilterState.requestFilterText !== "" &&
    127      filterState.requestFilterText !== ""
    128    ) {
    129      return;
    130    }
    131 
    132    trigger = "text";
    133  }
    134 
    135  telemetry.recordEvent("filters_changed", "netmonitor", null, {
    136    trigger,
    137    active: activeFilters.join(","),
    138    inactive: inactiveFilters.join(","),
    139  });
    140 }
    141 
    142 /**
    143 * This helper function is executed when side panel is selected.
    144 * It's responsible for recording "sidepanel_tool_changed"
    145 * telemetry event.
    146 */
    147 function sidePanelChange({ state, oldState, telemetry }) {
    148  telemetry.recordEvent("sidepanel_changed", "netmonitor", null, {
    149    oldpanel: oldState.ui.detailsPanelSelectedTab,
    150    newpanel: state.ui.detailsPanelSelectedTab,
    151  });
    152 }
    153 
    154 /**
    155 * This helper function is executed when a request is resent.
    156 * It's responsible for recording "edit_resend" telemetry event.
    157 */
    158 function sendCustomRequest({ telemetry }) {
    159  telemetry.recordEvent("edit_resend", "netmonitor");
    160 }
    161 
    162 /**
    163 * This helper function is executed when network throttling is changed.
    164 * It's responsible for recording "throttle_changed" telemetry event.
    165 */
    166 function throttlingChange({ action, telemetry }) {
    167  telemetry.recordEvent("throttle_changed", "netmonitor", null, {
    168    mode: action.profile,
    169  });
    170 }
    171 
    172 /**
    173 * This helper function is executed when log persistence is changed.
    174 * It's responsible for recording "persist_changed" telemetry event.
    175 */
    176 function persistenceChange({ telemetry, state }) {
    177  telemetry.recordEvent(
    178    "persist_changed",
    179    "netmonitor",
    180    String(state.ui.persistentLogsEnabled)
    181  );
    182 }
    183 
    184 /**
    185 * This helper function is executed when a WS frame is selected.
    186 * It's responsible for recording "select_ws_frame" telemetry event.
    187 */
    188 function selectMessage({ telemetry }) {
    189  telemetry.recordEvent("select_ws_frame", "netmonitor");
    190 }
    191 
    192 module.exports = eventTelemetryMiddleware;