Segment.io analytics client for Go. For additional documentation visit https://segment.io/docs/tracking-api/.
$ go get github.com/segmentio/analytics-go
Full example void of client.Track
error-handling for brevity:
package main
import "github.com/segmentio/analytics-go"
import "time"
func main() {
client := analytics.New("h97jamjw3h")
for {
client.Track(map[string]interface{}{
"event": "Download",
"userId": "123456",
"properties": map[string]interface{}{
"application": "Segment Desktop",
"version": "1.1.0",
"platform": "osx",
},
})
time.Sleep(50 * time.Millisecond)
}
}
With customized client:
package main
import "github.com/segmentio/analytics-go"
import "time"
func main() {
client := analytics.New("h97jamjw3h")
client.FlushAfter = 30 * time.Second
client.FlushAt = 100
for {
client.Track(map[string]interface{}{
"event": "Download",
"userId": "123456",
"properties": map[string]interface{}{
"application": "Segment Desktop",
"version": "1.1.0",
"platform": "osx",
},
})
time.Sleep(50 * time.Millisecond)
}
}
For each call a context
map may be passed, which
is merged with the original values.
client.Track(map[string]interface{}{
"event": "Download",
"userId": "123456",
"properties": map[string]interface{}{
"application": "Segment Desktop",
"version": "1.1.0",
"platform": "osx",
},
"context": map[string]interface{}{
"appVersion": "2.0.0",
"appHostname": "some-host"
}
})
const Version = "0.0.1"
By default messages are flushed in batches of 20 or after the default flush interval of 5 seconds.
type Client struct {
FlushAt int
FlushAfter time.Duration
Endpoint string
Key string
}
func New(key string) (c *Client)
func (c *Client) Alias(msg Message) error
func (c *Client) Group(msg Message) error
func (c *Client) Identify(msg Message) error
func (c *Client) Page(msg Message) error
func (c *Client) Screen(msg Message) error
func (c *Client) Track(msg Message) error
type Message map[string]interface{}
Enable debug output via the DEBUG environment variable, for example DEBUG=analytics
:
2014/04/23 18:56:57 buffer (110/1000) &{Track Download {segmentio 1.0.0 osx} 2014-04-23T18:56:57-0700}
2014/04/23 18:56:58 buffer (111/1000) &{Track Download {segmentio 1.0.0 osx} 2014-04-23T18:56:58-0700}
2014/04/23 18:56:58 buffer (112/1000) &{Track Download {segmentio 1.0.0 osx} 2014-04-23T18:56:58-0700}
2014/04/23 18:56:58 buffer (113/1000) &{Track Download {segmentio 1.0.0 osx} 2014-04-23T18:56:58-0700}
2014/04/23 18:56:58 buffer (114/1000) &{Track Download {segmentio 1.0.0 osx} 2014-04-23T18:56:58-0700}
2014/04/23 18:56:58 buffer (115/1000) &{Track Download {segmentio 1.0.0 osx} 2014-04-23T18:56:58-0700}
2014/04/23 18:56:58 buffer (116/1000) &{Track Download {segmentio 1.0.0 osx} 2014-04-23T18:56:58-0700}
2014/04/23 18:56:58 buffer (117/1000) &{Track Download {segmentio 1.0.0 osx} 2014-04-23T18:56:58-0700}
2014/04/23 18:56:58 buffer (118/1000) &{Track Download {segmentio 1.0.0 osx} 2014-04-23T18:56:58-0700}
MIT