gristlabs_grist-core/app/client/lib/telemetry.ts
George Gevoian 10f5f0cb37 (core) Add optional telemetry to grist-core
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
2023-06-07 12:00:51 -04:00

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);
});
}