Skip to content

Commit 9811005

Browse files
authored
Merge pull request #4 from mnismt/thanh/cer-993-cex-integration
2 parents 0ca7d66 + ca2c26e commit 9811005

3 files changed

Lines changed: 61 additions & 299 deletions

File tree

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,17 @@
4141
}
4242
]
4343
},
44-
"commands": []
44+
"commands": [],
45+
"configuration": {
46+
"title": "Overwrite",
47+
"properties": {
48+
"overwrite.telemetry.enabled": {
49+
"type": "boolean",
50+
"default": true,
51+
"description": "Enable anonymous usage telemetry to help improve the extension. All data is anonymized and no file contents or paths are ever collected."
52+
}
53+
}
54+
}
4555
},
4656
"scripts": {
4757
"vscode:package": "pnpm run package && pnpm vsce package --no-dependencies",

src/services/telemetry.ts

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import crypto from 'node:crypto'
2-
import fs from 'node:fs'
3-
import path from 'node:path'
42
import { PostHog } from 'posthog-node'
53
import * as vscode from 'vscode'
64

@@ -33,16 +31,6 @@ class TelemetryService {
3331
this.context = context
3432
this.startedAt = Date.now()
3533

36-
if (!this.isEnabled()) return
37-
38-
const apiKey = this.getApiKey(context)
39-
if (!apiKey) {
40-
console.warn('[telemetry] POSTHOG_API_KEY not set; telemetry disabled')
41-
return
42-
}
43-
44-
this.client = new PostHog(apiKey, { host: 'https://us.i.posthog.com' })
45-
4634
// Persist anonymous id; never use VS Code machineId directly
4735
this.distinctId = context.globalState.get<string>('telemetryDistinctId')
4836
if (!this.distinctId) {
@@ -52,42 +40,68 @@ class TelemetryService {
5240

5341
this.sessionId = crypto.randomUUID()
5442

43+
// Initialize client if telemetry is enabled
44+
this.initializeClient()
45+
46+
// Listen for configuration changes
47+
context.subscriptions.push(
48+
vscode.workspace.onDidChangeConfiguration((e) => {
49+
if (
50+
e.affectsConfiguration('overwrite.telemetry.enabled') ||
51+
e.affectsConfiguration('telemetry.telemetryLevel')
52+
) {
53+
this.handleConfigurationChange()
54+
}
55+
}),
56+
)
57+
}
58+
59+
private initializeClient() {
60+
if (!this.isEnabled()) return
61+
if (this.client) return // Already initialized
62+
63+
const apiKey = 'phc_LBityQJ7WJhm3iikesHTPvcLGqwUrSoJqofTKJo35rg'
64+
this.client = new PostHog(apiKey, { host: 'https://us.i.posthog.com' })
65+
5566
// Fire activation event with minimal props
5667
this.capture('extension_activated', {
5768
activation_time_ms: Date.now() - this.startedAt,
58-
ext_version: context.extension.packageJSON?.version,
69+
ext_version: this.context?.extension.packageJSON?.version,
5970
vscode_version: vscode.version,
6071
os: process.platform,
6172
node_version: process.versions.node,
6273
session_id: this.sessionId,
6374
})
6475
}
6576

66-
isEnabled(): boolean {
67-
return true
68-
}
69-
70-
private getApiKey(context: vscode.ExtensionContext): string | undefined {
71-
if (process.env.POSTHOG_API_KEY) return process.env.POSTHOG_API_KEY
72-
73-
// Fallback: try to read a .env at the extension root once at startup
74-
try {
75-
const root = context.extensionUri.fsPath
76-
const envPath = path.join(root, '.env')
77-
if (!fs.existsSync(envPath)) return undefined
78-
const content = fs.readFileSync(envPath, 'utf8')
79-
for (const line of content.split(/\r?\n/)) {
80-
const m = line.match(/^\s*POSTHOG_API_KEY\s*=\s*(.*)\s*$/)
81-
if (m) {
82-
// Strip optional quotes
83-
const raw = m[1]?.trim().replace(/^['"]|['"]$/g, '')
84-
if (raw) return raw
85-
}
77+
private handleConfigurationChange() {
78+
if (this.isEnabled()) {
79+
// Telemetry was enabled, initialize if not already done
80+
this.initializeClient()
81+
} else {
82+
// Telemetry was disabled, shutdown client
83+
if (this.client) {
84+
void this.client.shutdown()
85+
this.client = undefined
8686
}
87-
} catch {
88-
// ignore
8987
}
90-
return undefined
88+
}
89+
90+
isEnabled(): boolean {
91+
// Check VS Code global telemetry setting
92+
const globalTelemetryLevel = vscode.workspace
93+
.getConfiguration('telemetry')
94+
.get<string>('telemetryLevel')
95+
if (globalTelemetryLevel === 'off') return false
96+
97+
// Check extension-specific setting
98+
const extensionTelemetryEnabled = vscode.workspace
99+
.getConfiguration('overwrite.telemetry')
100+
.get<boolean>('enabled', true) // default to true
101+
102+
console.log(`Overwrite telemetry enabled: ${extensionTelemetryEnabled}`)
103+
104+
return extensionTelemetryEnabled
91105
}
92106

93107
capture(event: TelemetryEvent, properties?: Record<string, unknown>) {

0 commit comments

Comments
 (0)