Giter Site home page Giter Site logo

analytics-go's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.