Skip to content

Commit f690a58

Browse files
committed
feat: Add configuration option to disable CLI telemetry
1 parent 55e9cd8 commit f690a58

File tree

6 files changed

+60
-5
lines changed

6 files changed

+60
-5
lines changed

.changeset/rich-rivers-cheer.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@astrojs/telemetry': minor
3+
'astro': minor
4+
---
5+
6+
Adds a configuration option to disable CLI telemetry for the project
7+
8+
Astro collects optional anonymous telemetry to help us understand how people use the CLI. This change adds a new way to disable telemetry on a per-project basis. Currently you can disable telemetry globally per machine by running `astro telemetry disable` or by setting the environment variable `ASTRO_TELEMETRY_DISABLE`. This change adds a new configuration option to disable telemetry for a specific project, which applies to all users of the project.
9+
10+
```js
11+
// astro.config.mjs
12+
import { defineConfig } from 'astro/config';
13+
14+
export default defineConfig({
15+
disableTelemetry: true
16+
});
17+
18+
```

packages/astro/src/core/config/config.ts

+5
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ export async function resolveConfig(
148148

149149
const userConfig = await loadConfig(root, inlineOnlyConfig.configFile, fsMod);
150150
const mergedConfig = mergeConfig(userConfig, inlineUserConfig);
151+
152+
if (mergedConfig.disableTelemetry) {
153+
telemetry.disableForSession();
154+
}
155+
151156
// First-Pass Validation
152157
let astroConfig: AstroConfig;
153158
try {

packages/astro/src/core/config/schema.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export const ASTRO_CONFIG_DEFAULTS = {
8787
security: {
8888
checkOrigin: true,
8989
},
90+
disableTelemetry: false,
9091
env: {
9192
schema: {},
9293
validateSecrets: false,
@@ -509,6 +510,7 @@ export const AstroConfigSchema = z.object({
509510
.strict()
510511
.optional()
511512
.default(ASTRO_CONFIG_DEFAULTS.env),
513+
disableTelemetry: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.disableTelemetry),
512514
experimental: z
513515
.object({
514516
clientPrerender: z
@@ -526,10 +528,7 @@ export const AstroConfigSchema = z.object({
526528
.default({}),
527529
legacy: z
528530
.object({
529-
collections: z
530-
.boolean()
531-
.optional()
532-
.default(ASTRO_CONFIG_DEFAULTS.legacy.collections),
531+
collections: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.legacy.collections),
533532
})
534533
.default({}),
535534
});

packages/astro/src/types/public/config.ts

+16
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,22 @@ export interface AstroUserConfig {
899899
defaultStrategy?: 'tap' | 'hover' | 'viewport' | 'load';
900900
};
901901

902+
/**
903+
* @docs
904+
* @name disableTelemetry
905+
* @type {boolean}
906+
* @version 5.0.0
907+
* @default `false`
908+
* @description
909+
* Disable anonymous telemetry collection for this project.
910+
*
911+
* You can also disable telemetry globally by running `astro telemetry disable`.
912+
*
913+
* Visit https://astro.build/telemetry/ for more information about our approach to anonymous telemetry in Astro.
914+
*/
915+
916+
disableTelemetry?: boolean;
917+
902918
/**
903919
* @docs
904920
* @kind heading

packages/telemetry/src/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class AstroTelemetry {
2525
private debug = debug('astro:telemetry');
2626
private isCI = isCI;
2727
private env = process.env;
28+
private disabledForSession = false;
2829

2930
private get astroVersion() {
3031
return this.opts.astroVersion;
@@ -85,7 +86,7 @@ export class AstroTelemetry {
8586
}
8687

8788
private get isDisabled(): boolean {
88-
if (Boolean(this.ASTRO_TELEMETRY_DISABLED || this.TELEMETRY_DISABLED)) {
89+
if (this.ASTRO_TELEMETRY_DISABLED || this.TELEMETRY_DISABLED || this.disabledForSession) {
8990
return true;
9091
}
9192
return this.enabled === false;
@@ -95,6 +96,11 @@ export class AstroTelemetry {
9596
this.config.set(KEY.TELEMETRY_ENABLED, value);
9697
}
9798

99+
disableForSession() {
100+
this.debug('[notify] telemetry has been disabled for this session');
101+
this.disabledForSession = true;
102+
}
103+
98104
clear() {
99105
return this.config.clear();
100106
}

packages/telemetry/test/index.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,15 @@ describe('AstroTelemetry', () => {
8181
assert.notEqual(log, undefined);
8282
assert.match(logs.join(''), /enabled/);
8383
});
84+
85+
it('respects disabling per session', async () => {
86+
const { telemetry, logs } = setup();
87+
telemetry.disableForSession();
88+
assert.equal(telemetry.isDisabled, true);
89+
const result = await telemetry.record(['TEST']);
90+
assert.equal(result, undefined);
91+
const [log] = logs;
92+
assert.notEqual(log, undefined);
93+
assert.match(logs.join(''), /disabled/);
94+
});
8495
});

0 commit comments

Comments
 (0)