mixpanel

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2021 License: MIT Imports: 10 Imported by: 0

README

mixpanel-go

Mixpanel Go Client

Usage

go get github.com/stryd/mixpanel-go

import (
  "github.com/stryd/mixpanel-go"
)

Examples

Initializing a new client
mixpanelAPI := mixpanel.New(mixpanel.WithToken("api-token"))

There are multiple options that can be provided in any order. If multiple are provided the last one will take effect.

// must be https
mixpanel.WithApiUrl("https://new-api.mixpanel.com")

// required for track calls
mixpanel.WithToken("api_token")

// required for import calls
mixpanel.WithServiceAccount("service_account_username", "service_account_secret")
mixpanel.WithProjectID("1234")
Tracking a single event

This is usually used client-side but can be used from servers as well. The event must have occurred with in the last 5 days if a time is provided.

Token is required.

event := mixpanel.Event{
	DistinctID: "1234", // required
	Name: "your-event", // required
	Properties: map[string]interface{}{
		"run_id": "123456789",
	}
}

if err := mixpanelClient.Track(event); err != nil {
	log.Fatal(err)
}
Importing multiple events

Should only be used server side. Can track up to 2000 events as long as those 2000 events don't exceed the size limit of a single import request (2MB uncompressed). Insert IDs will be calculated if not provided for each event.

Service account credentials are required.

now := time.Now()

events := []mixpanel.Event{{
	DistinctID: "1234",     // required
	Name: "run-completed",  // required
	Time: &now,             // required
	Properties: map[string]interface{}{
		"run_id": "123456789",
	}
},{
	DistinctID: "1234",     // required
	Name: "run-deleted",    // required
	Time: &now,             // required
	Properties: map[string]interface{}{
		"run_id": "123456789",
	}
}}

if err := mixpanelClient.Import(events); err != nil {
	log.Fatal(err)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Headers = struct {
		Accept      string
		ContentType string
	}{
		Accept:      "Accept",
		ContentType: "Content-Type",
	}

	MimeTypes = struct {
		TextPlain          string
		ApplicationJSON    string
		XWWWFormURLEncoded string
	}{
		TextPlain:          "text/plain",
		ApplicationJSON:    "application/json",
		XWWWFormURLEncoded: "application/x-www-form-urlencoded",
	}
)

Functions

This section is empty.

Types

type API

type API interface {
	EventsAPI
}

API is a collection of simple methods to interact with the mixpanel API.

func New

func New(options ...Option) API

New creates a new Mixpanel Client with the given options.

func NewWithClient

func NewWithClient(httpClient *http.Client, options ...Option) API

type APIError

type APIError struct {
	Body string
	Resp *http.Response
}

func (*APIError) Error

func (err *APIError) Error() string

type ApiUrlOption

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

func (ApiUrlOption) Apply

func (o ApiUrlOption) Apply(c *Config)

type Config

type Config struct {
	ApiUrl         string
	Token          string
	ServiceAccount *ServiceAccount
	ProjectID      string
}

type Event

type Event struct {
	// Name of the event being tracked.
	Name string

	// DistinctID of the entity that produced this event. (usually a user)
	DistinctID string

	// InsertID is a unique identifier for the event, used for duplication. Leave blank to have one generated.
	InsertID string

	// IPV4Address of the user when they created the event. Only provide one of IPV4Address or Location.
	IPV4Address *string
	// Location is an optional field to specify where this event occurred. Only provide one of IPV4Address or Location.
	Location *EventLocation

	// Time the event occurred. Set to nil to use the current time.
	Time *time.Time

	// CustomProperties that wished to be tracked as fields of the event
	CustomProperties map[string]interface{}
}

Event A mixpanel event

type EventLocation

type EventLocation struct {
	City string
	// Region usually refers to the state. Can be any string?
	Region string
	// Country upper case 2 character ISO country code
	Country string
}

type EventsAPI

type EventsAPI interface {
	// Import a batch of events to Mixpanel (usually for server side tracking)
	Import(events []Event) error

	// Track events to Mixpanel (usually for client-side tracking)
	Track(e Event) error
}

type MockClient

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

MockClient Mocked version of Mixpanel intended for unit tests only

func NewMockClient

func NewMockClient() *MockClient

func (*MockClient) Import

func (m *MockClient) Import(es []Event) error

func (*MockClient) String

func (m *MockClient) String() string

func (*MockClient) Track

func (m *MockClient) Track(e Event) error

type Option

type Option interface {
	Apply(*Config)
}

func WithApiUrl

func WithApiUrl(value string) Option

func WithProjectID

func WithProjectID(value string) Option

func WithServiceAccount

func WithServiceAccount(username, secret string) Option

func WithToken

func WithToken(value string) Option

type ProjectIDOption

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

func (ProjectIDOption) Apply

func (o ProjectIDOption) Apply(c *Config)

type SecretOption

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

func (SecretOption) Apply

func (o SecretOption) Apply(c *Config)

type ServiceAccount

type ServiceAccount struct {
	Username string
	Secret   string
}

type TokenOption

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

func (TokenOption) Apply

func (o TokenOption) Apply(c *Config)

Jump to

Keyboard shortcuts

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