diff --git a/actions/telemetry_actions.jsx b/actions/telemetry_actions.jsx index 8ffb921fd038..d4cbf43df32b 100644 --- a/actions/telemetry_actions.jsx +++ b/actions/telemetry_actions.jsx @@ -2,6 +2,9 @@ // See LICENSE.txt for license information. import {Client4} from 'mattermost-redux/client'; +import {getConfig} from 'mattermost-redux/selectors/entities/general'; + +import store from 'stores/redux_store.jsx'; import {isDevMode} from 'utils/utils'; @@ -14,6 +17,15 @@ const SUPPORTS_MEASURE_METHODS = isSupported([ performance.clearMeasures, ]); +export function isTelemetryEnabled(state) { + const config = getConfig(state); + return config.DiagnosticsEnabled === 'true'; +} + +export function shouldTrackPerformance(state = store.getState()) { + return isDevMode(state) || isTelemetryEnabled(state); +} + export function trackEvent(category, event, props) { Client4.trackEvent(category, event, props); if (isDevMode() && category === 'performance' && props) { @@ -33,14 +45,14 @@ export function pageVisited(category, name) { * */ export function clearMarks(names) { - if (!isDevMode() || !SUPPORTS_CLEAR_MARKS) { + if (!shouldTrackPerformance() || !SUPPORTS_CLEAR_MARKS) { return; } names.forEach((name) => performance.clearMarks(name)); } export function mark(name) { - if (!isDevMode() || !SUPPORTS_MARK) { + if (!shouldTrackPerformance() || !SUPPORTS_MARK) { return; } performance.mark(name); @@ -60,7 +72,7 @@ export function mark(name) { * */ export function measure(name1, name2) { - if (!isDevMode() || !SUPPORTS_MEASURE_METHODS) { + if (!shouldTrackPerformance() || !SUPPORTS_MEASURE_METHODS) { return [-1, '']; } diff --git a/actions/telemetry_actions.test.js b/actions/telemetry_actions.test.js new file mode 100644 index 000000000000..35a1087a1923 --- /dev/null +++ b/actions/telemetry_actions.test.js @@ -0,0 +1,48 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +import {isTelemetryEnabled, shouldTrackPerformance} from 'actions/telemetry_actions'; + +describe('Actions.Telemetry', () => { + test('isTelemetryEnabled', async () => { + const state = { + entities: { + general: { + config: { + DiagnosticsEnabled: 'false', + }, + }, + }, + }; + + expect(isTelemetryEnabled(state)).toBeFalsy(); + + state.entities.general.config.DiagnosticsEnabled = 'true'; + + expect(isTelemetryEnabled(state)).toBeTruthy(); + }); + + test('shouldTrackPerformance', async () => { + const state = { + entities: { + general: { + config: { + DiagnosticsEnabled: 'false', + EnableDeveloper: 'false', + }, + }, + }, + }; + + expect(shouldTrackPerformance(state)).toBeFalsy(); + + state.entities.general.config.DiagnosticsEnabled = 'true'; + + expect(shouldTrackPerformance(state)).toBeTruthy(); + + state.entities.general.config.DiagnosticsEnabled = 'false'; + state.entities.general.config.EnableDeveloper = 'true'; + + expect(shouldTrackPerformance(state)).toBeTruthy(); + }); +}); diff --git a/utils/utils.jsx b/utils/utils.jsx index 39af920b47f0..d0aa4d54a792 100644 --- a/utils/utils.jsx +++ b/utils/utils.jsx @@ -1669,8 +1669,8 @@ export function setCSRFFromCookie() { /** * Returns true if in dev mode, false otherwise. */ -export function isDevMode() { - const config = getConfig(store.getState()); +export function isDevMode(state = store.getState()) { + const config = getConfig(state); return config.EnableDeveloper === 'true'; }