mirror of
https://github.com/gristlabs/grist-core.git
synced 2024-10-27 20:44:07 +00:00
10f5f0cb37
Summary: Adds support for optional telemetry to grist-core. A new environment variable, GRIST_TELEMETRY_LEVEL, controls the level of telemetry collected. Test Plan: Server and unit tests. Reviewers: paulfitz Reviewed By: paulfitz Subscribers: dsagal, anaisconce Differential Revision: https://phab.getgrist.com/D3880
30 lines
941 B
TypeScript
30 lines
941 B
TypeScript
import {logError} from 'app/client/models/errors';
|
|
import {Level, TelemetryContracts, TelemetryEvent, TelemetryMetadataByLevel} from 'app/common/Telemetry';
|
|
import {fetchFromHome, getGristConfig, pageHasHome} from 'app/common/urlUtils';
|
|
|
|
export function logTelemetryEvent(event: TelemetryEvent, metadata?: TelemetryMetadataByLevel) {
|
|
if (!pageHasHome()) { return; }
|
|
|
|
const {telemetry} = getGristConfig();
|
|
if (!telemetry) { return; }
|
|
|
|
const {telemetryLevel} = telemetry;
|
|
if (Level[telemetryLevel] < TelemetryContracts[event].minimumTelemetryLevel) { return; }
|
|
|
|
fetchFromHome('/api/telemetry', {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
event,
|
|
metadata,
|
|
}),
|
|
credentials: 'include',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
},
|
|
}).catch((e: Error) => {
|
|
console.warn(`Failed to log telemetry event ${event}`, e);
|
|
logError(e);
|
|
});
|
|
}
|