Git Product home page Git Product logo

go-cli-analytics's Introduction

go-cli-analytics

Disk-buffered analytics for CLI tools powered by Segment. Analytics for CLI tools can help determine which aspects of your tool are most valued by users, and where improvements could be made. Track command or flag usage, errors, latencies, or others, just don't be sketchy!

How

Pass your Segemnt write key, and specify a directory name which will reside in HOME.

a := analytics.New(&analytics.Config{
  WriteKey: "<write key>",
  Dir:      ".myprogram",
})

This package will create three files:

  • ~/DIR/id – pseudo user id
  • ~/DIR/events – buffered events
  • ~/DIR/last_flush – state for previous flush

Track events like this:

a.Track("More Something", map[string]interface{}{
  "other":    "stuff",
  "whatever": "else here",
})

Flush events at random, based on the previous duration time, or based on size. Note that flushing on every command will introduce ~500ms of latency, so don't do this.

lastFlush, _ := a.LastFlush()
n, _ := a.Size()

switch {
case n >= 15:
  fmt.Printf("  flush due to size\n")
  a.Flush()
case time.Now().Sub(lastFlush) >= time.Minute:
  fmt.Printf("  flush due to duration\n")
  a.Flush()
default:
  fmt.Printf("  close\n")
  a.Close()
}

Notes

Note that there is no file-level locking at the moment for concurrent executions of your program. This may be added in the future if necessary.

Badges

GoDoc


tjholowaychuk.com  ·  GitHub @tj  ·  Twitter @tjholowaychuk

go-cli-analytics's People

Contributors

tj 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.