ivolo / analytics-go

Segment.io analytics client for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

analytics-go

Segment.io analytics client for Go. For additional documentation visit https://segment.io/docs/tracking-api/.

Installation

$ go get github.com/segmentio/analytics-go

Example

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"
  }
})

API

const Version = "0.0.1"

type Client

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

func New(key string) (c *Client)

func (*Client) Alias

func (c *Client) Alias(msg Message) error

func (*Client) Group

func (c *Client) Group(msg Message) error

func (*Client) Identify

func (c *Client) Identify(msg Message) error

func (*Client) Page

func (c *Client) Page(msg Message) error

func (*Client) Screen

func (c *Client) Screen(msg Message) error

func (*Client) Track

func (c *Client) Track(msg Message) error

type Message

type Message map[string]interface{}

Debugging

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}

License

MIT

About

Segment.io analytics client for Go


Languages

Language:Go 100.0%