Skip to content

Commit 216db93

Browse files
committed
minimal tracking only that can be disabled
1 parent db13cfe commit 216db93

File tree

3 files changed

+13
-66
lines changed

3 files changed

+13
-66
lines changed

app.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func main() {
6868
cwd := flag.String("cwd", ".", "base directory of project")
6969
port := flag.String("port", ":8048", "port to start the server on")
7070
protected := flag.Bool("protected", false, "enable protected mode")
71+
tracking := flag.Bool("tracking", true, "enable usage tracking")
7172

7273
flag.Parse()
7374

@@ -174,28 +175,26 @@ func main() {
174175

175176
router.PathPrefix("/").Handler(getSpaHandler())
176177

177-
analytics.TrackEvent("server_started", map[string]interface{}{"source": "web"})
178+
// Track server start and stop events if tracking is enabled
179+
// Note that this helps me understand if the users are actually using Zasper
180+
// and keeps me motivated to maintain and improve the product
181+
182+
if *tracking {
183+
analytics.TrackServerStartStopEvent("server_started", map[string]interface{}{"source": "web"})
184+
}
178185

179186
// Channel for graceful shutdown
180187
stop := make(chan os.Signal, 1)
181188
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
182189

183-
printBanner(*port, core.ServerAccessToken, version, *protected)
190+
printBanner(*port, core.ServerAccessToken, version, *protected, *tracking)
184191

185192
go func() {
186193
if err := http.ListenAndServe(*port, corsOpts.Handler(router)); err != nil && err != http.ErrServerClosed {
187194
fmt.Printf("ListenAndServe(): %s\n", err)
188195
}
189196
}()
190197

191-
go func() {
192-
ticker := time.NewTicker(10 * time.Minute)
193-
defer ticker.Stop()
194-
for range ticker.C {
195-
analytics.SendStatsToPostHog()
196-
}
197-
}()
198-
199198
<-stop
200199
fmt.Println("Shutting down server...")
201200

@@ -208,7 +207,7 @@ func main() {
208207
fmt.Println("Server exiting")
209208
}
210209

211-
func printBanner(port string, accessToken string, version string, protected bool) {
210+
func printBanner(port string, accessToken string, version string, protected bool, tracking bool) {
212211
fmt.Println("==========================================================")
213212
fmt.Println(" ███████╗ █████╗ ███████╗██████╗ ███████╗██████╗ ")
214213
fmt.Println(" ╚══███╔╝██╔══██╗██╔════╝██╔══██╗██╔════╝██╔══██╗")
@@ -229,6 +228,7 @@ func printBanner(port string, accessToken string, version string, protected bool
229228
} else {
230229
fmt.Println(" 🔒 Protected Mode: disabled")
231230
}
231+
fmt.Println(" 🔄 Server start/shutdown tracking enabled:", tracking)
232232
fmt.Println("==========================================================")
233233
}
234234

internal/analytics/tracking.go

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package analytics
22

33
import (
44
"strings"
5-
"time"
65

76
"github.com/google/uuid"
87
"github.com/posthog/posthog-go"
@@ -54,51 +53,6 @@ func IncrementUsageStat(eventType EventType) {
5453
}
5554
}
5655

57-
func SendStatsToPostHog() {
58-
if client == nil {
59-
log.Info().Msg("PostHog client not initialized.")
60-
return
61-
}
62-
63-
props := posthog.NewProperties().
64-
Set("notebooks_opened", stats.NotebooksOpened).
65-
Set("terminals_opened", stats.TerminalsOpened).
66-
Set("code_cells_executed", stats.CodeCellsExecuted).
67-
Set("files_opened", stats.FilesOpened).
68-
Set("timestamp", time.Now().Format(time.RFC3339)).
69-
Set("OS", core.Zasper.OSName)
70-
71-
config, _ := core.ReadConfig()
72-
73-
err := client.Enqueue(posthog.Capture{
74-
DistinctId: config.TrackingID,
75-
Event: "session usage summary",
76-
Properties: props,
77-
})
78-
79-
if err != nil {
80-
log.Debug().Msgf("PostHog enqueue failed: %v. Retrying once...", err)
81-
82-
// Retry once after a short delay
83-
time.Sleep(2 * time.Second)
84-
err = client.Enqueue(posthog.Capture{
85-
DistinctId: config.TrackingID,
86-
Event: "session usage summary",
87-
Properties: props,
88-
})
89-
90-
if err != nil {
91-
log.Printf("PostHog enqueue retry also failed: %v", err)
92-
} else {
93-
log.Debug().Msg("PostHog retry succeeded.")
94-
stats = UsageStats{}
95-
}
96-
} else {
97-
log.Debug().Msg("PostHog session usage summary sent successfully.")
98-
stats = UsageStats{}
99-
}
100-
}
101-
10256
// Function to generate or retrieve the tracking ID from the config
10357
func GetAnonymousTrackingId() (string, error) {
10458
config, err := core.ReadConfig()
@@ -143,7 +97,7 @@ func SetUpPostHogClient() error {
14397
return nil
14498
}
14599

146-
func TrackEvent(eventName string, properties map[string]interface{}) {
100+
func TrackServerStartStopEvent(eventName string, properties map[string]interface{}) {
147101
if phAPIKey == "" {
148102
log.Warn().Msg("API key is missing. Event tracking skipped.")
149103
return
@@ -167,8 +121,7 @@ func TrackEvent(eventName string, properties map[string]interface{}) {
167121
}
168122

169123
func CloseClient() {
170-
SendStatsToPostHog()
171-
TrackEvent("server_shutdown", map[string]interface{}{"source": "web"})
124+
TrackServerStartStopEvent("server_shutdown", map[string]interface{}{"source": "web"})
172125
// Flush analytics queue
173126
if client != nil {
174127
client.Close() // This will block until the queue is flushed

internal/content/content_manager.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,6 @@ func GetKernelPath(path string) int {
370370
return 1
371371
}
372372

373-
func dirExists(path string) bool {
374-
path = filepath.Clean(path)
375-
os_path := GetSafePath(path)
376-
return IsDir(os_path)
377-
}
378-
379373
func IsDir(path string) bool {
380374
info, err := os.Lstat(path)
381375

0 commit comments

Comments
 (0)