amplitude

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package amplitude provides a http client for working with various Amplitude APIs.

Index

Constants

View Source
const (
	// MaxEvents is the max number of events to track before dropping occurs
	MaxEvents = 1024

	// MaxErrors is the max number of errors to track before dropping occurs
	MaxErrors = 1024
)

Variables

This section is empty.

Functions

func AsError

func AsError(resp *http.Response) error

AsError reads the response for errors and returns them if present

Types

type BatchEventsSuccessSummary

type BatchEventsSuccessSummary struct {
	Code           int   `json:"code"`
	EventsIngested int   `json:"events_ingested"`
	PayloadSize    int   `json:"payload_size_bytes"`
	UploadTime     int64 `json:"server_upload_time"`
}

BatchEventsSuccessSummary is expected to be returned for all successful requests.

func (*BatchEventsSuccessSummary) String

func (s *BatchEventsSuccessSummary) String() string

String converts the summary response to a pretty string format.

type Client

type Client struct {
	// BaseURL for all API requests. Exposed services should
	// use relative paths for making requests.
	BaseURL   *url.URL
	UserAgent string
	APIKey    string

	Events *EventsService
	// contains filtered or unexported fields
}

Client allows interaction with amplitude services

func New

func New(apiKey string) *Client

New creates a new Amplitude client

func (*Client) Do

func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error)

Do a http request to Amplitude and handles the response it receives

func (*Client) NewRequest

func (c *Client) NewRequest(ctx context.Context, method, endpoint string, body RequestBody) (*http.Request, error)

NewRequest provides a http request to be sent to Amplitude

func (*Client) NewRequestBody

func (c *Client) NewRequestBody() RequestBody

NewRequestBody creates a request body with authentication

func (*Client) SendMiddleware added in v0.2.2

func (c *Client) SendMiddleware() *SendMiddleware

SendMiddleware creates a new http middleware instance from the client

func (*Client) WithHttpClient

func (c *Client) WithHttpClient(client *http.Client) *Client

WithHttpClient sets the underlining http client if default is not enough

type Error

type Error struct {
	Response *http.Response
	Context  map[string]interface{}
}

Error houses any potential http error responses from the API The body is not well-defined so more information can be obtained by access the context

func (*Error) Code

func (e *Error) Code() int

Code is an integer denoting what error has been received

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface

func (*Error) Message

func (e *Error) Message() string

Message is a human-readable error denoting (hopefully) what has gone wrong

type Event

type Event struct {
	UserId              string                 `json:"user_id,omitempty"`
	DeviceId            string                 `json:"device_id,omitempty"`
	Name                string                 `json:"event_type"`
	Time                int64                  `json:"time,omitempty"`
	Properties          map[string]interface{} `json:"event_properties,omitempty"`
	UserProperties      map[string]interface{} `json:"user_properties,omitempty"`
	Groups              map[string]interface{} `json:"groups,omitempty"`
	GroupProperties     map[string]interface{} `json:"group_properties,omitempty"`
	AppVersion          string                 `json:"app_version,omitempty"`
	Platform            string                 `json:"platform,omitempty"`
	OSName              string                 `json:"os_name,omitempty"`
	OSVersion           string                 `json:"os_version,omitempty"`
	DeviceBrand         string                 `json:"device_brand,omitempty"`
	DeviceManufacturer  string                 `json:"device_manufacturer,omitempty"`
	DeviceModel         string                 `json:"device_model,omitempty"`
	Carrier             string                 `json:"carrier,omitempty"`
	Country             string                 `json:"country,omitempty"`
	Region              string                 `json:"region,omitempty"`
	City                string                 `json:"city,omitempty"`
	DMA                 string                 `json:"dma,omitempty"`
	Language            string                 `json:"language,omitempty"`
	Price               float64                `json:"price,omitempty"`
	Quantity            int                    `json:"quantity,omitempty"`
	Revenue             float64                `json:"revenue,omitempty"`
	ProductId           string                 `json:"productId,omitempty"`
	RevenueType         string                 `json:"revenueType,omitempty"`
	Latitude            float64                `json:"location_lat,omitempty"`
	Longitude           float64                `json:"location_lng,omitempty"`
	IP                  string                 `json:"ip,omitempty"`
	IOSAdvertiserId     string                 `json:"idfa,omitempty"`
	IOSVendorId         string                 `json:"idfv,omitempty"`
	AndroidAdvertiserId string                 `json:"adid,omitempty"`
	AndroidId           string                 `json:"android_id,omitempty"`
	Id                  int                    `json:"event_id,omitempty"`
	SessionId           int                    `json:"session_id,omitempty"`
	InsertId            string                 `json:"insert_id,omitempty"`
}

Event is the base analytic structure for capturing user activity

func NewEventFromRequest added in v0.2.2

func NewEventFromRequest(r *http.Request, userId, deviceId string) *Event

NewEventFromRequest creates a new amplitude event from a http request

func (*Event) Environment added in v0.2.2

func (e *Event) Environment(env string) *Event

Environment sets the environment of the event

func (*Event) Latency added in v0.2.2

func (e *Event) Latency(latency int64) *Event

Latency sets the latency of the event

func (*Event) Version added in v0.2.2

func (e *Event) Version(v string) *Event

Version sets the app version of the event

type EventsService

type EventsService service

EventsService provides access to events related functions

func (*EventsService) Send added in v0.2.2

func (s *EventsService) Send(ctx context.Context, events ...*Event) (*BatchEventsSuccessSummary, error)

Send batches of events via the Batch Event Upload API This endpoint is recommended for Customers that want to send large batches of data at a time, for example through scheduled jobs, rather than in a continuous realtime stream. Due to the higher rate of data that is permitted to this endpoint, data sent to this endpoint may be delayed based on load.

type RequestBody

type RequestBody map[string]interface{}

RequestBody is sent as the body of all requests

func (RequestBody) WithValue

func (b RequestBody) WithValue(key string, v interface{}) RequestBody

WithValue adds a value to the request body

type SendMiddleware added in v0.2.2

type SendMiddleware struct {
	// contains filtered or unexported fields
}

SendMiddleware is a http middleware for sending request metrics to amplitude

func NewSendMiddleware added in v0.2.2

func NewSendMiddleware(c *Client) *SendMiddleware

NewSendMiddleware create a new instance of the amplitude middleware

func (*SendMiddleware) DeviceHeader added in v0.2.2

func (m *SendMiddleware) DeviceHeader(h string) *SendMiddleware

DeviceHeader sets the header to retrieve device id from

func (*SendMiddleware) Environment added in v0.2.2

func (m *SendMiddleware) Environment(env string) *SendMiddleware

Environment sets the environment the app is running

func (*SendMiddleware) Error added in v0.2.2

func (m *SendMiddleware) Error() error

Error gets any errors that have occurred

func (*SendMiddleware) Event added in v0.2.2

func (m *SendMiddleware) Event() *Event

Event gets any events that have occurred

func (*SendMiddleware) Flush added in v0.2.2

func (m *SendMiddleware) Flush(ctx context.Context)

Flush will send all batched events to the amplitude batch upload API

func (*SendMiddleware) Handle added in v0.2.2

func (m *SendMiddleware) Handle(next http.Handler) http.Handler

func (*SendMiddleware) Send added in v0.2.2

func (m *SendMiddleware) Send(event *Event) *SendMiddleware

Send buffers an event

func (*SendMiddleware) SendError added in v0.2.2

func (m *SendMiddleware) SendError(err error) *SendMiddleware

SendError buffers an error

func (*SendMiddleware) UserHeader added in v0.2.2

func (m *SendMiddleware) UserHeader(h string) *SendMiddleware

UserHeader sets the header to retrieve user id from

func (*SendMiddleware) Version added in v0.2.2

func (m *SendMiddleware) Version(v string) *SendMiddleware

Version sets the version of your app

Jump to

Keyboard shortcuts

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