Version: v1.0.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2021 License: MIT Imports: 10 Imported by: 0



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


func main() {

 const ApiKey = "<your-api-key>"
	client := amplitude.NewDefaultClient(ApiKey)

	err := client.LogEvent(&data.Event{
		UserID:    "",
		EventType: "test-event",
		EventProperties: map[string]interface{}{
			"source": "notification",
		UserProperties: map[string]interface{}{
			"age":    25,
			"gender": "female",

	if err != nil {
		log.Printf("failed to queue event: %v", err)





View Source
const (
	DefaultServerUrl = ""

	// 256 events batch size for individual uploads
	DefaultBatchSize = 256

	// Based on nodejs client (but not estimated for this SDK): 2kb is a safe estimate for a medium size event object. This keeps the SDK's memory footprint roughly under 32 MB.
	DefaultMaxCachedEvents = 16000

	// 12 goroutines to execute parallel uploads.
	DefaultMaxParallelUploads = 12

	// 5 retries with backoff.
	DefaultMaxUploadAttempts = 6

	// 10 ms pace of upload
	DefaultUploadInterval = 10 * time.Millisecond

	// Default of 10s was took from NodeJS sdk
	DefaultRequestTimeout = 10 * time.Second


This section is empty.


This section is empty.


type Client

type Client struct {
	// contains filtered or unexported fields

Client wraps Amplitude V2 api client.

func NewClient

func NewClient(apiKey string, options Options) *Client

NewClient creates a client with a set of options.

func NewDefaultClient

func NewDefaultClient(apiKey string) *Client

NewDefaultClient creates a client with default options.

func (*Client) Flush

func (c *Client) Flush() error

Flush uploads all queued events immediately.

func (*Client) Identify added in v0.0.2

func (c *Client) Identify(identify *data.Identify) error

Identify enqueues an identify event to be uploaded in background.

func (*Client) LogEvent

func (c *Client) LogEvent(event *data.Event) error

LogEvent enqueues an event to be uploaded in background.

func (*Client) Shutdown

func (c *Client) Shutdown() error

Shutdown stops the client and flushes remaining events.

func (*Client) Start

func (c *Client) Start() error

Start starts the client event transport routine.

func (*Client) State

func (c *Client) State() ClientState

State returns client current state.

type ClientState

type ClientState uint32

ClientState defines the client FSM.

const (
	Idle ClientState = iota

type Options

type Options struct {
	// Base context
	Context context.Context

	// BatchSize is the amount of events sent in a single upload batch; defaults to DefaultBatchSize.
	BatchSize int

	// MaxCachedEvents is the limit of events we can queue before start dropping logged events; defaults to DefaultMaxCachedEvents.
	MaxCachedEvents int

	// MaxParallelUploads is the number of goroutines we spawn to handle uploads in batch; defaults to DefaultMaxParallelUploads.
	MaxParallelUploads int

	// MaxUploadAttempts is the number of upload attempts we execute before dropping a batch (MaxUploadAttempts - 1 retries); defaults to DefaultMaxUploadAttempts.
	MaxUploadAttempts int

	// OptOut should be set to true if you want a dry-run experience; defaults to false.
	OptOut bool

	// Transport allows you to configure your own transport layer; defaults to transport.HttpTransport.
	Transport transport.Transport

	// ServerUrl allows you to configure your own API server url; defaults to DefaultServerUrl.
	ServerUrl string

	// RequestTimeout is the timeout for each individual upload attempt; defaults to DefaultRequestTimeout.
	RequestTimeout time.Duration

	// UploadInterval is the interval between individual uploads; defaults to DefaultUploadInterval.
	UploadInterval time.Duration

	// UploadDelegate allows you to hook your own code when an upload happens (for example to send metrics); defaults to nil.
	UploadDelegate UploadBatchDelegate // defaults to nil

Options defines API Client optional parameters.

func (*Options) BaseContext added in v0.0.3

func (o *Options) BaseContext() context.Context

type UploadBatchDelegate

type UploadBatchDelegate func(*Uploader, []*data.Event, error)

UploadBatchDelegate function executes when an upload batch happens (succeeded or not).

type UploadCallback

type UploadCallback func(*Uploader)

UploadCallback function executes deferred code during the upload.

type Uploader

type Uploader struct {
	// contains filtered or unexported fields

Uploader represents an upload worker.

func (*Uploader) Start

func (u *Uploader) Start() (context.Context, context.CancelFunc)


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL