Code Monkey home page Code Monkey logo

analytics-go's Introduction

analytics-go

Segment.io analytics client for Go. For additional documentation visit https://segment.io/docs/libraries/go or view the godocs.

Installation

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

Examples

Basic

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("your-write-key-here")

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

Client options

Example with customized client:

package main

import "github.com/segmentio/analytics-go"
import "time"

func main() {
  client := analytics.New("your-write-key-here")
  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)
  }
}

Context

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

Flushing on shutdown

The following example illustrates how .Stop() may be used to flush and wait for pending calls to be sent to Segment.

package main

import "github.com/visionmedia/go-gracefully"
import "github.com/segmentio/analytics-go"
import "time"
import "log"

type Worker struct {
  analytics *analytics.Client
  exit      chan struct{}
}

func (w *Worker) Start() {
  println("starting")

  go func() {
    for {
      select {
      case <-w.exit:
        return
      case <-time.Tick(50 * time.Millisecond):
        log.Println("send track")

        w.analytics.Track(map[string]interface{}{
          "event":  "Download",
          "userId": "123456",
          "properties": map[string]interface{}{
            "application": "Segment Desktop",
            "version":     "1.1.0",
            "platform":    "osx",
          },
        })
      }
    }
  }()
}

func (w *Worker) Stop() {
  println("stopping")
  close(w.exit)
  println("flushing analytics")
  w.analytics.Close()
  println("bye :)")
}

func NewWorker(client *analytics.Client) *Worker {
  return &Worker{
    analytics: client,
    exit:      make(chan struct{}),
  }
}

// run with DEBUG=analytics to view
// analytics-specific debug output
func main() {
  client := analytics.New("your-write-key-here")
  client.FlushAfter = 5 * time.Second
  client.FlushAt = 25

  w := NewWorker(client)
  w.Start()
  gracefully.Shutdown()
  w.Stop()
}

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

analytics-go's People

Contributors

tj avatar

Watchers

 avatar  avatar  avatar

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.