Files
zeekay b4e1366340 rebrand: export Insights types instead of PostHog
Rename Go package from posthog to insights across all 80+ files.
Consumers now import as `insights "github.com/hanzoai/insights-go"`
and use insights.Client, insights.Config, insights.Capture, etc.

- Rename posthog.go -> insights.go, posthog_test.go -> insights_test.go
- Update package declaration from posthog to insights in all .go files
- Update SDKName from posthog-go to insights-go
- Update DefaultEndpoint from app.posthog.com to insights.hanzo.ai
- Update all FieldError/ConfigError type strings
- Update User-Agent headers, log prefixes, comments, and docs
- Update JSON test fixtures to match new SDK name
- Update CLI, examples, extras, and sdk_compliance_adapter
- Fix pre-existing go vet warnings (non-constant format strings)
2026-03-13 19:54:58 -07:00

127 lines
2.6 KiB
Go

package main
import (
"fmt"
"log"
"os"
json "github.com/goccy/go-json"
insights "github.com/hanzoai/insights-go"
"github.com/urfave/cli"
)
func main() {
var apiKey string
var operationType string
var distinctId string
var event string
var properties string
var alias string
app := cli.NewApp()
app.Name = "insights"
app.Usage = "Call the Insights API"
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "apiKey",
Usage: "The Insights API Key",
Destination: &apiKey,
},
&cli.StringFlag{
Name: "type",
Usage: "The type of the message to send",
Destination: &operationType,
},
&cli.StringFlag{
Name: "distinctId",
Usage: "Distinct ID for the user",
Destination: &distinctId,
},
&cli.StringFlag{
Name: "event",
Usage: "Name of the captured event",
Destination: &event,
},
&cli.StringFlag{
Name: "properties",
Usage: "Metadata associated with an event or identify call",
Destination: &properties,
},
&cli.StringFlag{
Name: "alias",
Usage: "Alias for distinctId",
Destination: &alias,
},
}
app.Action = func(c *cli.Context) error {
callback := callback(make(chan error, 1))
client, err := insights.NewWithConfig(apiKey, insights.Config{
BatchSize: 1,
Callback: callback,
})
if err != nil {
fmt.Println("could not initialize insights client", err)
os.Exit(1)
}
switch operationType {
case "capture":
client.Enqueue(insights.Capture{
DistinctId: distinctId,
Event: event,
Properties: parseJSON(properties),
})
case "alias":
client.Enqueue(insights.Alias{
DistinctId: distinctId,
Alias: alias,
})
case "identify":
client.Enqueue(insights.Identify{
DistinctId: distinctId,
Properties: parseJSON(properties),
})
}
if err := <-callback; err != nil {
os.Exit(1)
}
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
// parseJSON parses a JSON formatted string into a map.
func parseJSON(v string) map[string]interface{} {
var m map[string]interface{}
err := json.Unmarshal([]byte(v), &m)
if err != nil {
fmt.Println("could not parse json", v)
fmt.Println("error:", err)
os.Exit(1)
}
return m
}
// callback implements the insights.Callback interface. It is used by the CLI
// to wait for events to be uploaded before exiting.
type callback chan error
func (c callback) Failure(m insights.APIMessage, err error) {
fmt.Printf("could not upload message %v due to %v\n", m, err)
c <- err
}
func (c callback) Success(_ insights.APIMessage) {
c <- nil
}