Skip to content

Commit 3f6ed79

Browse files
authored
feat: re-add env config properties (#296)
1 parent c3c86a5 commit 3f6ed79

File tree

3 files changed

+200
-13
lines changed

3 files changed

+200
-13
lines changed

docs/telemetry.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,24 @@ Events are still sent to the analytics endpoint in debug mode, but you can see e
139139

140140
## Custom Tracking Endpoint (for Organizations)
141141

142-
Organizations can point telemetry to their own analytics endpoint:
142+
Organizations can point telemetry to their own analytics endpoint using environment variables. These environment variables have the **highest priority** and will override any configuration from project-level config or global config files:
143143

144144
```bash
145-
# Set custom endpoint
145+
# Set custom endpoint (highest priority - overrides all other configs)
146146
export CODEGEN_TELEMETRY_ENDPOINT=https://analytics.mycompany.com/telemetry
147147
export CODEGEN_TELEMETRY_ID=custom-tracking-id
148148
export CODEGEN_TELEMETRY_API_SECRET=your-api-secret
149149
```
150150

151+
**Configuration Priority Order (highest to lowest):**
152+
1. **Environment variables** (highest priority):
153+
- `CODEGEN_TELEMETRY_DISABLED` / `DO_NOT_TRACK` - disable telemetry
154+
- `CODEGEN_TELEMETRY_ENDPOINT` - custom analytics endpoint
155+
- `CODEGEN_TELEMETRY_ID` - custom tracking ID
156+
- `CODEGEN_TELEMETRY_API_SECRET` - custom API secret
157+
2. **Project-level config** (from `codegen.config.js`)
158+
3. **Global config file** (`~/.the-codegen-project/config.json`)
159+
151160
Expected endpoint format (GA4 Measurement Protocol compatible):
152161

153162
```

src/telemetry/config.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ import {ProjectTelemetryConfig} from '../codegen/types';
1313
* This function never throws - returns disabled config on any error.
1414
*
1515
* Priority order (highest to lowest):
16-
* 1. Project-level config (from codegen.config.js)
17-
* 2. Global config file (~/.the-codegen-project/config.json)
18-
* 3. Environment variable overrides (CODEGEN_TELEMETRY_DISABLED, DO_NOT_TRACK)
16+
* 1. Environment variable overrides (highest priority):
17+
* - CODEGEN_TELEMETRY_DISABLED / DO_NOT_TRACK: disable telemetry
18+
* - CODEGEN_TELEMETRY_ENDPOINT: custom analytics endpoint
19+
* - CODEGEN_TELEMETRY_ID: custom tracking ID
20+
* - CODEGEN_TELEMETRY_API_SECRET: custom API secret
21+
* 2. Project-level config (from codegen.config.js)
22+
* 3. Global config file (~/.the-codegen-project/config.json)
1923
*
2024
* @param projectConfig - Optional project-level telemetry config from codegen.config.js
2125
* @returns Promise resolving to telemetry configuration
@@ -30,13 +34,26 @@ export async function getTelemetryConfig(
3034
...(projectConfig ?? {})
3135
};
3236

33-
// Apply environment variable overrides
37+
// Apply environment variable overrides (highest priority)
3438
if (
3539
process.env.CODEGEN_TELEMETRY_DISABLED === '1' ||
3640
process.env.DO_NOT_TRACK
3741
) {
3842
telemetryConfig.enabled = false;
3943
}
44+
45+
if (process.env.CODEGEN_TELEMETRY_ENDPOINT) {
46+
telemetryConfig.endpoint = process.env.CODEGEN_TELEMETRY_ENDPOINT;
47+
}
48+
49+
if (process.env.CODEGEN_TELEMETRY_ID) {
50+
telemetryConfig.trackingId = process.env.CODEGEN_TELEMETRY_ID;
51+
}
52+
53+
if (process.env.CODEGEN_TELEMETRY_API_SECRET) {
54+
telemetryConfig.apiSecret = process.env.CODEGEN_TELEMETRY_API_SECRET;
55+
}
56+
4057
return telemetryConfig;
4158
} catch (error) {
4259
// On any error, return disabled config

test/telemetry/config.spec.ts

Lines changed: 168 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ describe('Telemetry Config', () => {
3232
enabled: true,
3333
anonymousId: 'test-uuid',
3434
endpoint: 'https://example.com',
35-
trackingId: 'G-TEST123'
35+
trackingId: 'G-TEST123',
36+
apiSecret: ''
3637
},
3738
hasShownTelemetryNotice: true
3839
};
@@ -51,7 +52,8 @@ describe('Telemetry Config', () => {
5152
enabled: true,
5253
anonymousId: 'test-uuid',
5354
endpoint: 'https://example.com',
54-
trackingId: 'G-TEST123'
55+
trackingId: 'G-TEST123',
56+
apiSecret: ''
5557
},
5658
hasShownTelemetryNotice: true
5759
};
@@ -92,7 +94,8 @@ describe('Telemetry Config', () => {
9294
enabled: true,
9395
anonymousId: 'test-uuid',
9496
endpoint: 'https://example.com',
95-
trackingId: 'G-TEST123'
97+
trackingId: 'G-TEST123',
98+
apiSecret: ''
9699
},
97100
hasShownTelemetryNotice: true
98101
};
@@ -120,7 +123,8 @@ describe('Telemetry Config', () => {
120123
enabled: true,
121124
anonymousId: 'test-uuid',
122125
endpoint: 'https://example.com',
123-
trackingId: 'G-TEST123'
126+
trackingId: 'G-TEST123',
127+
apiSecret: ''
124128
},
125129
hasShownTelemetryNotice: true
126130
};
@@ -138,6 +142,160 @@ describe('Telemetry Config', () => {
138142
expect(config.endpoint).toBe('https://example.com'); // From global
139143
expect(config.trackingId).toBe('G-TEST123'); // From global
140144
});
145+
146+
it('should apply CODEGEN_TELEMETRY_ENDPOINT environment variable override', async () => {
147+
process.env.CODEGEN_TELEMETRY_ENDPOINT = 'https://custom-endpoint.com';
148+
149+
const mockConfig = {
150+
version: '1.0.0',
151+
telemetry: {
152+
enabled: true,
153+
anonymousId: 'test-uuid',
154+
endpoint: 'https://example.com',
155+
trackingId: 'G-TEST123',
156+
apiSecret: ''
157+
},
158+
hasShownTelemetryNotice: true
159+
};
160+
161+
mockGetGlobalConfig.mockResolvedValue(mockConfig);
162+
163+
const config = await getTelemetryConfig();
164+
165+
expect(config.endpoint).toBe('https://custom-endpoint.com');
166+
expect(config.enabled).toBe(true); // Should not affect other properties
167+
expect(config.trackingId).toBe('G-TEST123');
168+
});
169+
170+
it('should apply CODEGEN_TELEMETRY_ID environment variable override', async () => {
171+
process.env.CODEGEN_TELEMETRY_ID = 'G-CUSTOM123';
172+
173+
const mockConfig = {
174+
version: '1.0.0',
175+
telemetry: {
176+
enabled: true,
177+
anonymousId: 'test-uuid',
178+
endpoint: 'https://example.com',
179+
trackingId: 'G-TEST123',
180+
apiSecret: ''
181+
},
182+
hasShownTelemetryNotice: true
183+
};
184+
185+
mockGetGlobalConfig.mockResolvedValue(mockConfig);
186+
187+
const config = await getTelemetryConfig();
188+
189+
expect(config.trackingId).toBe('G-CUSTOM123');
190+
expect(config.enabled).toBe(true); // Should not affect other properties
191+
expect(config.endpoint).toBe('https://example.com');
192+
});
193+
194+
it('should apply CODEGEN_TELEMETRY_API_SECRET environment variable override', async () => {
195+
process.env.CODEGEN_TELEMETRY_API_SECRET = 'custom-secret-123';
196+
197+
const mockConfig = {
198+
version: '1.0.0',
199+
telemetry: {
200+
enabled: true,
201+
anonymousId: 'test-uuid',
202+
endpoint: 'https://example.com',
203+
trackingId: 'G-TEST123',
204+
apiSecret: 'original-secret'
205+
},
206+
hasShownTelemetryNotice: true
207+
};
208+
209+
mockGetGlobalConfig.mockResolvedValue(mockConfig);
210+
211+
const config = await getTelemetryConfig();
212+
213+
expect(config.apiSecret).toBe('custom-secret-123');
214+
expect(config.enabled).toBe(true); // Should not affect other properties
215+
expect(config.endpoint).toBe('https://example.com');
216+
});
217+
218+
it('should apply multiple environment variable overrides together', async () => {
219+
process.env.CODEGEN_TELEMETRY_ENDPOINT = 'https://org-analytics.com';
220+
process.env.CODEGEN_TELEMETRY_ID = 'G-ORG123';
221+
process.env.CODEGEN_TELEMETRY_API_SECRET = 'org-secret';
222+
223+
const mockConfig = {
224+
version: '1.0.0',
225+
telemetry: {
226+
enabled: true,
227+
anonymousId: 'test-uuid',
228+
endpoint: 'https://example.com',
229+
trackingId: 'G-TEST123',
230+
apiSecret: 'original-secret'
231+
},
232+
hasShownTelemetryNotice: true
233+
};
234+
235+
mockGetGlobalConfig.mockResolvedValue(mockConfig);
236+
237+
const config = await getTelemetryConfig();
238+
239+
expect(config.endpoint).toBe('https://org-analytics.com');
240+
expect(config.trackingId).toBe('G-ORG123');
241+
expect(config.apiSecret).toBe('org-secret');
242+
expect(config.enabled).toBe(true);
243+
expect(config.anonymousId).toBe('test-uuid'); // Should keep from global
244+
});
245+
246+
it('should prioritize environment variables over project config', async () => {
247+
process.env.CODEGEN_TELEMETRY_ENDPOINT = 'https://env-endpoint.com';
248+
249+
const mockConfig = {
250+
version: '1.0.0',
251+
telemetry: {
252+
enabled: true,
253+
anonymousId: 'test-uuid',
254+
endpoint: 'https://global-endpoint.com',
255+
trackingId: 'G-TEST123',
256+
apiSecret: ''
257+
},
258+
hasShownTelemetryNotice: true
259+
};
260+
261+
mockGetGlobalConfig.mockResolvedValue(mockConfig);
262+
263+
const projectConfig = {
264+
endpoint: 'https://project-endpoint.com'
265+
};
266+
267+
const config = await getTelemetryConfig(projectConfig);
268+
269+
// Environment variable should override both project and global config
270+
expect(config.endpoint).toBe('https://env-endpoint.com');
271+
});
272+
273+
it('should disable telemetry via environment variable even if project config enables it', async () => {
274+
process.env.CODEGEN_TELEMETRY_DISABLED = '1';
275+
276+
const mockConfig = {
277+
version: '1.0.0',
278+
telemetry: {
279+
enabled: false,
280+
anonymousId: 'test-uuid',
281+
endpoint: 'https://example.com',
282+
trackingId: 'G-TEST123',
283+
apiSecret: ''
284+
},
285+
hasShownTelemetryNotice: true
286+
};
287+
288+
mockGetGlobalConfig.mockResolvedValue(mockConfig);
289+
290+
const projectConfig = {
291+
enabled: true // Try to enable in project config
292+
};
293+
294+
const config = await getTelemetryConfig(projectConfig);
295+
296+
// Environment variable should have highest priority
297+
expect(config.enabled).toBe(false);
298+
});
141299
});
142300

143301
describe('setTelemetryEnabled', () => {
@@ -148,7 +306,8 @@ describe('Telemetry Config', () => {
148306
enabled: false,
149307
anonymousId: 'test-uuid',
150308
endpoint: 'https://example.com',
151-
trackingId: 'G-TEST123'
309+
trackingId: 'G-TEST123',
310+
apiSecret: ''
152311
},
153312
hasShownTelemetryNotice: true
154313
};
@@ -174,7 +333,8 @@ describe('Telemetry Config', () => {
174333
enabled: true,
175334
anonymousId: 'test-uuid',
176335
endpoint: 'https://example.com',
177-
trackingId: 'G-TEST123'
336+
trackingId: 'G-TEST123',
337+
apiSecret: ''
178338
},
179339
hasShownTelemetryNotice: true
180340
};
@@ -216,7 +376,8 @@ describe('Telemetry Config', () => {
216376
enabled: true,
217377
anonymousId: 'test-uuid',
218378
endpoint: 'https://example.com',
219-
trackingId: 'G-TEST123'
379+
trackingId: 'G-TEST123',
380+
apiSecret: ''
220381
},
221382
hasShownTelemetryNotice: true
222383
};

0 commit comments

Comments
 (0)