Send to a log service

Send editor telemetry to an HTTP log service through your server without exposing its token.

@docx-editor.dev/telemetry is available in version 2.29.0 and later.

The browser posts events to a same-origin route. The route validates them and forwards them with a token that stays on the server.

Send browser events

This client skips successful opens and honors browser privacy signals:

import { browserPrivacySignals, createTelemetry, httpExporter } from '@docx-editor.dev/telemetry';

export const telemetry = createTelemetry({
  exporters: [httpExporter({ url: '/api/telemetry' })],
  filter: (event) => !(event.kind === 'document.open' && event.outcome === 'opened'),
  consent: browserPrivacySignals({ optOutKey: 'my-app:telemetry' }),
});

Register telemetry.module as shown in Set up an editor.

Forward events from your server

This Next.js route forwards to a Better Stack HTTP source:

// app/api/telemetry/route.ts
import { telemetryRoute, toLogRecord } from '@docx-editor.dev/telemetry/ingest';

export const POST = telemetryRoute({
  allowedOrigins: ['https://app.example.com'],
  forward: (events) =>
    fetch(`https://${process.env.BETTERSTACK_INGESTING_HOST}`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Bearer ${process.env.BETTERSTACK_SOURCE_TOKEN}`,
      },
      // Add fixed fields on the server, where the browser cannot change them.
      body: JSON.stringify(events.map((event) => ({ ...toLogRecord(event), service: 'my-app' }))),
    }),
  onError: (error) => console.error('[telemetry]', error),
});

toLogRecord adds dt (timestamp), message, occurrences, and failures. The route returns 204 for every request, so use onError to see failed forwarding.

Count deduplicated events

A refusal repeated within 10 seconds arrives as one event plus one summary. The summary's repeated counts only the extra copies, so sum fields instead of counting records.

FieldValueSum to count
occurrences1, or repeated for a summaryEvents
failures0 for expected refusalsFaults
SELECT code, sum(occurrences) AS events, sum(failures) AS faults
FROM logs WHERE kind = 'edit.rejected' GROUP BY code

Sampled areas send a share of events, so sums are not exact rates. Use metrics for rates.

Count sessions

The following query gives sessions per day, median length, and edits per session from end records:

SELECT
  date_trunc('day', dt) AS day,
  count(*) AS sessions,
  median(durationMs) / 1000 AS median_seconds,
  avg(inputEdits + commandEdits + automationEdits) AS edits_per_session
FROM logs
WHERE kind = 'session' AND phase = 'end'
GROUP BY day
ORDER BY day

To count open sessions, count start records whose editorSessionId has no end record but a recent active record. For counting rules, see Sessions.

Limits

  • Keep provider tokens on the server.
  • Validation checks syntax, not that an event happened. Treat counts as browser reports.
  • Allowed identifier strings can still contain personal information.
  • The route drops events whose editorSessionId or engineVersion does not match the formats that the engine creates.
  • For batching and validation, see HTTP exporter and route reference.

See also