发布

  • feat(core): add analytics (#34144)

    frostbyte_neo 发布于 2026-03-09 15:33:52 +00:00

    Current Behavior

    Nx CLI has no mechanism for collecting usage analytics, and users are
    not prompted about their analytics preferences. There is no way for the
    Nx team to understand which commands, generators, and features are most
    used.

    Expected Behavior

    This PR adds opt-in analytics collection to the Nx CLI with two main
    components:

    1. Analytics Prompt

    Users are prompted for their analytics preference on first interactive
    CLI run when analytics is not yet configured in nx.json:

    • Only appears when analytics is undefined in nx.json
    • Skipped in CI environments
    • Skipped in non-interactive terminals (piped input/output)
    • Stores the user's choice as a boolean (true/false) in the
      analytics field of nx.json
    • Defaults to false if the user cancels (Ctrl+C)
    • Includes a migration (update-22-6-0/enable-analytics-prompt) for
      existing workspaces

    2. Analytics Collector

    When analytics is enabled, the CLI collects usage data via a Rust-based
    telemetry service and sends it to GA4. Data collected includes:

    • Commands run (e.g. build, test, generate, add) — tracked
      as page views
    • Command arguments — with aggressive sanitization of sensitive
      values (project names, file paths, URLs, credentials, free-form text are
      all redacted; only boolean flags and safe enum values are preserved)
    • Generator and package names for nx add and nx generate (as
      custom dimensions)
    • Project graph creation duration
    • Environment metadata: Nx version, Node version, package manager,
      OS, architecture, CI detection

    Workspace Identification

    Each workspace is identified by a deterministic ID (used as the GA4
    client ID) with the following priority:

    1. nxCloudId (or nxCloudAccessToken) from nx.json — used
      directly, most stable
    2. Git remote URL (git remote get-url origin) — SHA-256 hashed for
      privacy
    3. First commit SHA (git rev-list --max-parents=0 HEAD) — used
      directly as a fallback

    Each user/machine is identified separately via node-machine-id.

    Privacy & Safety

    • No project names, file paths, or other PII is collected
    • Sensitive CLI arguments are redacted (see SENSITIVE_ARGS_KEYS list)
    • Analytics is strictly opt-in (must be true in nx.json)
    • Telemetry failures are silently ignored — never blocks or crashes the
      CLI
    • WASM builds are excluded (no native telemetry module available)
    • The native telemetry functions are loaded with optional chaining to
      prevent crashes when running against published Nx binaries that don't
      include them yet

    Key Files

    • packages/nx/src/utils/analytics-prompt.ts — Prompt logic and
      workspace ID generation
    • packages/nx/src/analytics/analytics.ts — Analytics collector, event
      tracking, argument sanitization
    • packages/nx/src/native/telemetry/ — Rust telemetry service
      (constants, service, mod)
    • packages/nx/src/utils/machine-id-cache.ts — Machine ID for user
      identification
    • packages/nx/src/migrations/update-22-6-0/enable-analytics-prompt.ts
      — Migration for existing workspaces

    Related Issue(s)

    Closes NXC-3731
    Closes NXC-3732
    Closes NXC-3733
    Closes NXC-3734


    Co-authored-by: nx-cloud[bot] <71083854+nx-cloud[bot]@users.noreply.github.com>
    Co-authored-by: Coly010 Coly010@users.noreply.github.com
    Co-authored-by: Jason Jean jasonjean1993@gmail.com

    下载附件