text: import { AnalyticsTracker, } from "./telemetry";
  import { createFlush } from "./create-flush";
  const instance = new AnalyticsTracker();
  const flush = createFlush(instance.isTrackingEnabled());
  process.on(`exit`, flush);
  // For long running commands we want to occasionally flush the data
  //
  // The data is also sent on exit.
  const intervalDuration = process.env.TELEMETRY_BUFFER_INTERVAL;
  const interval = intervalDuration && Number.isFinite(+intervalDuration)
      ? Math.max(Number(intervalDuration), 1000)
      : 10 * 60 * 1000; // 10 min
  function tick() {
      flush()
          .catch(console.error)
          .then(() => setTimeout(tick, interval));
  }
  export function trackFeatureIsUsed(name) {
      instance.trackFeatureIsUsed(name);
  }
  export function trackCli(input, tags, opts) {
      instance.captureEvent(input, tags, opts);
  }
  export function trackError(input, tags) {
      instance.captureError(input, tags);
  }
  export function trackBuildError(input, tags) {
      instance.captureBuildError(input, tags);
  }
  export function setDefaultTags(tags) {
      instance.decorateAll(tags);
  }
  export function decorateEvent(event, tags) {
      instance.decorateNextEvent(event, tags);
  }
  export function setTelemetryEnabled(enabled) {
      instance.setTelemetryEnabled(enabled);
  }
  export function startBackgroundUpdate() {
      setTimeout(tick, interval);
  }
  export function isTrackingEnabled() {
      return instance.isTrackingEnabled();
  }
  export function aggregateStats(data) {
      return instance.aggregateStats(data);
  }
  export function addSiteMeasurement(event, obj) {
      instance.addSiteMeasurement(event, obj);
  }
  export function expressMiddleware(source) {
      return function (_req, _res, next) {
          try {
              instance.trackActivity(`${source}_ACTIVE`);
          }
          catch (e) {
              // ignore
          }
          next();
      };
  }
  // Internal
  export function setDefaultComponentId(componentId) {
      instance.componentId = componentId;
  }
  export function setGatsbyCliVersion(version) {
      instance.gatsbyCliVersion = version;
  }
  module.exports = {
      trackFeatureIsUsed,
      trackCli,
      trackError,
      trackBuildError,
      setDefaultTags,
      decorateEvent,
      setTelemetryEnabled,
      startBackgroundUpdate,
      isTrackingEnabled,
      aggregateStats,
      addSiteMeasurement,
      expressMiddleware,
  };
  