Coder Social home page Coder Social logo

amplitude-go's Introduction

Amplitude Golang SDK

Go Reference GitHub release (latest by date) GitHub go.mod Go version

Amplitude unofficial client for Go, inspired in their official SDK for Node.

For reference, visit HTTP API v2 documentation.

Installation

$ go get github.com/renatoaf/amplitude-go

Usage

// startup
client := amplitude.NewDefaultClient("<your-api-key")
client.Start()

// logging events
client.LogEvent(&data.Event{
    UserID: "[email protected]",
    EventType: "test-event",
    EventProperties: map[string]interface{}{
        "source": "notification",
    },
    UserProperties: map[string]interface{}{
        "age": 25,
        "gender": "female",
    },
})

// gracefully shutdown, waiting pending events to be sent
client.Shutdown()

The Event (doc) structure is based on API V2 request properties.

Events will not be sent synchronously, the client keeps a goroutine responsible for batching and issuing uploads of events. This routine will upload events:

  • after the upload interval (every 10ms by default).
  • as soon as we accumulate enough events to batch (256 events by default).
  • when Flush is explicitly invoked.
  • during shutdown process.

LogEvent calls, therefore, should never block. It will return an error in case the event was not queued (which means the event will be dropped without even being sent). This should not happen unless the uploads are not getting through for some reason (e.g. a misconfiguration).

Check advanced parameters to learn how to tweak the default behaviour.

Advanced parameters

The default client behaviour can be configured through a set of custom Options (doc).

client := amplitude.NewClient("<your-api-key", amplitude.Options{ ... })

Examples

  1. If you want to configure your client to issue uploads every second:
client := amplitude.NewClient("<your-api-key", amplitude.Options{
    UploadInterval: time.Second,
})
  1. If you want to disable retries:
client := amplitude.NewClient("<your-api-key", amplitude.Options{
    MaxUploadAttempts: 1,
})
  1. If you want to hook your own Datadog metrics for amplitude events:
client := amplitude.NewClient("<your-api-key", amplitude.Options{
    UploadDelegate: func(_ *amplitude.Uploader, events []*data.Event, err error) {
        count := len(events)
        if err != nil {
            statsd.Incr("amplitude.events", []string{"status:failure"}, count)
        } else {
            statsd.Incr("amplitude.events", []string{"status:success"}, count)
        }
    },
})
  1. If you want to allow more upload batches in parallel and a larger queue, in case you anticipate a higher throughput of events:
client := amplitude.NewClient("<your-api-key", amplitude.Options{
    MaxParallelUploads: 16, 
    MaxCachedEvents: 32000,
})

amplitude-go's People

Contributors

renatoaf avatar stefanos1019 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.