-
feat(core): add analytics (#34144)
发布于
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 whenanalyticsis not yet configured innx.json:- Only appears when
analyticsis undefined innx.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
analyticsfield ofnx.json - Defaults to
falseif 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 addandnx 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:nxCloudId(ornxCloudAccessToken) fromnx.json— used
directly, most stable- Git remote URL (
git remote get-url origin) — SHA-256 hashed for
privacy - 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_KEYSlist) - Analytics is strictly opt-in (must be
trueinnx.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 generationpackages/nx/src/analytics/analytics.ts— Analytics collector, event
tracking, argument sanitizationpackages/nx/src/native/telemetry/— Rust telemetry service
(constants, service, mod)packages/nx/src/utils/machine-id-cache.ts— Machine ID for user
identificationpackages/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下载附件
- Only appears when