-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add PostHog event tracking for CLI installs and upgrades (ENG-2277) #293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devin-ai-integration
wants to merge
5
commits into
main
Choose a base branch
from
thomas-devin/track-cli-sdk-installs-posthog
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b0d7825
feat: add PostHog event tracking for CLI installs and upgrades
devin-ai-integration[bot] ba5b5c0
fix: address review feedback - goreleaser env fallback, brew upgrade …
devin-ai-integration[bot] c2c86b9
fix: use exec.LookPath for brew upgrade version detection
devin-ai-integration[bot] 20c1ef5
fix: prevent spurious subprocess event, use WaitGroup for flush, pres…
devin-ai-integration[bot] 9e1da84
fix: add FlushPosthog to ExitWithError/Exit paths to prevent event lo…
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "crypto/rand" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "path/filepath" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| // PostHog API key injected at build time via ldflags | ||
| var PosthogAPIKey = "" | ||
|
|
||
| // PostHog API endpoint | ||
| var PosthogHost = "https://us.i.posthog.com" | ||
|
|
||
| // telemetryState stores the last reported versions to deduplicate events | ||
| type telemetryState struct { | ||
| DistinctID string `json:"distinct_id"` | ||
| CLI string `json:"cli,omitempty"` | ||
| SDKs map[string]string `json:"sdks,omitempty"` | ||
| } | ||
|
|
||
| var ( | ||
| telemetryOnce sync.Once | ||
| telemetryCache *telemetryState | ||
| telemetryRaw map[string]interface{} // preserves unknown fields from disk | ||
| posthogWg sync.WaitGroup | ||
| ) | ||
|
|
||
| // getTelemetryPath returns the path to the telemetry state file | ||
| func getTelemetryPath() string { | ||
| homeDir, err := os.UserHomeDir() | ||
| if err != nil { | ||
| return "" | ||
| } | ||
| return filepath.Join(homeDir, ".blaxel", "telemetry.json") | ||
| } | ||
|
|
||
| // loadTelemetryState reads the telemetry state from disk | ||
| func loadTelemetryState() *telemetryState { | ||
| telemetryOnce.Do(func() { | ||
| telemetryCache = &telemetryState{ | ||
| SDKs: make(map[string]string), | ||
| } | ||
| telemetryRaw = make(map[string]interface{}) | ||
| path := getTelemetryPath() | ||
| if path == "" { | ||
| return | ||
| } | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return | ||
| } | ||
| // Unmarshal into raw map to preserve unknown fields | ||
| _ = json.Unmarshal(data, &telemetryRaw) | ||
| _ = json.Unmarshal(data, telemetryCache) | ||
| if telemetryCache.SDKs == nil { | ||
| telemetryCache.SDKs = make(map[string]string) | ||
| } | ||
| }) | ||
| return telemetryCache | ||
| } | ||
|
|
||
| // saveTelemetryState writes the telemetry state to disk, preserving unknown fields | ||
| func saveTelemetryState(state *telemetryState) { | ||
| path := getTelemetryPath() | ||
| if path == "" { | ||
| return | ||
| } | ||
| dir := filepath.Dir(path) | ||
| _ = os.MkdirAll(dir, 0755) | ||
|
|
||
| // Merge known fields into raw map to preserve unknown fields from disk | ||
| merged := make(map[string]interface{}) | ||
| for k, v := range telemetryRaw { | ||
| merged[k] = v | ||
| } | ||
| merged["distinct_id"] = state.DistinctID | ||
| if state.CLI != "" { | ||
| merged["cli"] = state.CLI | ||
| } | ||
| merged["sdks"] = state.SDKs | ||
|
|
||
| data, err := json.MarshalIndent(merged, "", " ") | ||
| if err != nil { | ||
| return | ||
| } | ||
| _ = os.WriteFile(path, data, 0600) | ||
| } | ||
|
|
||
| // getDistinctID returns a persistent anonymous UUID for PostHog events. | ||
| // The UUID is generated on first use and stored in ~/.blaxel/telemetry.json. | ||
| func getDistinctID() string { | ||
| state := loadTelemetryState() | ||
| if state.DistinctID != "" { | ||
| return state.DistinctID | ||
| } | ||
| state.DistinctID = generateUUID() | ||
| saveTelemetryState(state) | ||
| return state.DistinctID | ||
| } | ||
|
|
||
| // capturePosthogEvent sends an event to PostHog via HTTP POST. | ||
| // This is fire-and-forget: errors are silently ignored. | ||
| func capturePosthogEvent(event string, properties map[string]string) { | ||
| if PosthogAPIKey == "" { | ||
| return | ||
| } | ||
|
|
||
| distinctID := getDistinctID() | ||
|
|
||
| payload := map[string]interface{}{ | ||
| "api_key": PosthogAPIKey, | ||
| "event": event, | ||
| "distinct_id": distinctID, | ||
| "timestamp": time.Now().UTC().Format(time.RFC3339), | ||
| "properties": properties, | ||
| } | ||
|
|
||
| data, err := json.Marshal(payload) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| posthogWg.Add(1) | ||
| go func() { | ||
| defer posthogWg.Done() | ||
| client := &http.Client{Timeout: 5 * time.Second} | ||
| resp, err := client.Post(PosthogHost+"/capture/", "application/json", bytes.NewReader(data)) | ||
| if err != nil { | ||
| return | ||
| } | ||
| defer resp.Body.Close() | ||
| }() | ||
| } | ||
|
|
||
| // TrackCLIInstalled checks if this CLI version has been reported and sends | ||
| // an "Installed CLI" event if it hasn't. | ||
| func TrackCLIInstalled(cliVersion string) { | ||
| if PosthogAPIKey == "" || cliVersion == "" || cliVersion == "dev" { | ||
| return | ||
| } | ||
| // Skip telemetry in subprocess spawned by detectInstalledVersion() | ||
| if os.Getenv("BL_SKIP_TELEMETRY") == "1" { | ||
| return | ||
| } | ||
|
|
||
| state := loadTelemetryState() | ||
| if state.CLI == cliVersion { | ||
| return | ||
| } | ||
|
|
||
| capturePosthogEvent("Installed CLI", map[string]string{ | ||
| "version": cliVersion, | ||
| }) | ||
|
|
||
| state.CLI = cliVersion | ||
| saveTelemetryState(state) | ||
| } | ||
|
devin-ai-integration[bot] marked this conversation as resolved.
|
||
|
|
||
| // TrackCLIUpgraded sends an "Upgraded CLI" event with old and new versions. | ||
| func TrackCLIUpgraded(oldVersion string, newVersion string) { | ||
| if PosthogAPIKey == "" { | ||
| return | ||
| } | ||
| if oldVersion == "" || newVersion == "" || oldVersion == newVersion { | ||
| return | ||
| } | ||
|
|
||
| capturePosthogEvent("Upgraded CLI", map[string]string{ | ||
| "old_version": oldVersion, | ||
| "new_version": newVersion, | ||
| }) | ||
|
|
||
| // Update the stored CLI version so "Installed CLI" won't re-fire | ||
| state := loadTelemetryState() | ||
| state.CLI = newVersion | ||
| saveTelemetryState(state) | ||
| } | ||
|
|
||
| // generateUUID creates a random UUID v4 string without external dependencies. | ||
| func generateUUID() string { | ||
| b := make([]byte, 16) | ||
| _, err := rand.Read(b) | ||
| if err != nil { | ||
| return "unknown" | ||
| } | ||
| b[6] = (b[6] & 0x0f) | 0x40 // version 4 | ||
| b[8] = (b[8] & 0x3f) | 0x80 // variant 10 | ||
| return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:16]) | ||
| } | ||
|
|
||
| // FlushPosthog waits for all in-flight PostHog requests to complete, | ||
| // with a maximum timeout of 5 seconds to avoid blocking indefinitely. | ||
| func FlushPosthog() { | ||
| if PosthogAPIKey == "" { | ||
| return | ||
| } | ||
| done := make(chan struct{}) | ||
| go func() { | ||
| posthogWg.Wait() | ||
| close(done) | ||
| }() | ||
| select { | ||
| case <-done: | ||
| case <-time.After(5 * time.Second): | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.