mirror of
https://github.com/hanzoai/insights-go.git
synced 2026-09-30 22:46:12 +00:00
The module declares go 1.27.1 and the adapter image follows. go fix applied the modernizers and left ptrString and ptrBool with no callers, so they are gone; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
127 lines
2.6 KiB
Go
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]any {
|
|
var m map[string]any
|
|
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
|
|
}
|