2023-04-06 15:10:29 +00:00
|
|
|
import {logError} from 'app/client/models/errors';
|
2023-06-06 17:08:50 +00:00
|
|
|
import {Level, TelemetryContracts, TelemetryEvent, TelemetryMetadataByLevel} from 'app/common/Telemetry';
|
|
|
|
import {fetchFromHome, getGristConfig, pageHasHome} from 'app/common/urlUtils';
|
2023-04-06 15:10:29 +00:00
|
|
|
|
2023-06-06 17:08:50 +00:00
|
|
|
export function logTelemetryEvent(event: TelemetryEvent, metadata?: TelemetryMetadataByLevel) {
|
2023-04-06 15:10:29 +00:00
|
|
|
if (!pageHasHome()) { return; }
|
|
|
|
|
2023-06-06 17:08:50 +00:00
|
|
|
const {telemetry} = getGristConfig();
|
|
|
|
if (!telemetry) { return; }
|
|
|
|
|
|
|
|
const {telemetryLevel} = telemetry;
|
|
|
|
if (Level[telemetryLevel] < TelemetryContracts[event].minimumTelemetryLevel) { return; }
|
|
|
|
|
2023-05-18 22:35:39 +00:00
|
|
|
fetchFromHome('/api/telemetry', {
|
2023-04-06 15:10:29 +00:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
2023-06-06 17:08:50 +00:00
|
|
|
event,
|
2023-04-06 15:10:29 +00:00
|
|
|
metadata,
|
|
|
|
}),
|
|
|
|
credentials: 'include',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
|
|
},
|
|
|
|
}).catch((e: Error) => {
|
2023-06-06 17:08:50 +00:00
|
|
|
console.warn(`Failed to log telemetry event ${event}`, e);
|
2023-04-06 15:10:29 +00:00
|
|
|
logError(e);
|
|
|
|
});
|
|
|
|
}
|