11 "github.com/charmbracelet/crush/internal/version"
12 "github.com/posthog/posthog-go"
16 endpoint = "https://data.charm.land"
17 key = "phc_4zt4VgDWLqbYnJYEwLRxFoaTL2noNrQij0C6E8k3I0V"
23 baseProps = posthog.NewProperties().
24 Set("GOOS", runtime.GOOS).
25 Set("GOARCH", runtime.GOARCH).
26 Set("TERM", os.Getenv("TERM")).
27 Set("SHELL", filepath.Base(os.Getenv("SHELL"))).
28 Set("Version", version.Version).
29 Set("GoVersion", runtime.Version())
33 c, err := posthog.NewWithConfig(key, posthog.Config{
38 slog.Error("Failed to initialize PostHog client", "error", err)
41 distinctId = getDistinctId()
44// send logs an event to PostHog with the given event name and properties.
45func send(event string, props ...any) {
49 err := client.Enqueue(posthog.Capture{
50 DistinctId: distinctId,
52 Properties: pairsToProps(props...).Merge(baseProps),
55 slog.Error("Failed to enqueue PostHog event", "event", event, "props", props, "error", err)
60// Error logs an error event to PostHog with the error type and message.
61func Error(err any, props ...any) {
65 // The PostHog Go client does not yet support sending exceptions.
66 // We're mimicking the behavior by sending the minimal info required
67 // for PostHog to recognize this as an exception event.
72 {"type": reflect.TypeOf(err).String(), "value": fmt.Sprintf("%v", err)},
77 send("$exception", props...)
84 if err := client.Close(); err != nil {
85 slog.Error("Failed to flush PostHog events", "error", err)
89func pairsToProps(props ...any) posthog.Properties {
90 p := posthog.NewProperties()
92 if !isEven(len(props)) {
93 slog.Error("Event properties must be provided as key-value pairs", "props", props)
97 for i := 0; i < len(props); i += 2 {
98 key := props[i].(string)
100 p = p.Set(key, value)
105func isEven(n int) bool {
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list