mixpanel

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2022 License: MIT Imports: 7 Imported by: 19

README

Mixpanel Go Client

Usage

import "github.com/dukex/mixpanel"

--

documentation on godoc

Examples

Track

err := client.Track("13793", "Signed Up", map[string]interface{}{
	"Referred By": "Friend",
})

--

Identify and Update Operation

people := client.Identify("13793")

err := people.Track(map[string]interface{}{
	"Buy": "133"
})

err := people.Update("$set", map[string]interface{}{
	"Address":  "1313 Mockingbird Lane",
	"Birthday": "1948-01-01",
})

License

Released under the MIT License.

Author

Duke X (dukex)

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var IgnoreTime *time.Time = &time.Time{}

Functions

This section is empty.

Types

type ErrTrackFailed

type ErrTrackFailed struct {
	Message string
}

func (*ErrTrackFailed) Error

func (err *ErrTrackFailed) Error() string

type Event

type Event struct {
	// IP-address of the user. Leave empty to use autodetect, or set to "0" to
	// not specify an ip-address.
	IP string

	// Timestamp. Set to nil to use the current time.
	Timestamp *time.Time

	// Custom properties. At least one must be specified.
	Properties map[string]interface{}
}

A mixpanel event

type Mixpanel

type Mixpanel interface {
	// Create a mixpanel event using the track api
	Track(distinctId, eventName string, e *Event) error

	// Create a mixpanel event using the import api
	Import(distinctId, eventName string, e *Event) error

	// Set properties for a mixpanel user.
	// Deprecated: Use UpdateUser instead
	Update(distinctId string, u *Update) error

	// Set properties for a mixpanel user.
	UpdateUser(distinctId string, u *Update) error

	// Set properties for a mixpanel group.
	UpdateGroup(groupKey, groupId string, u *Update) error

	// Create an alias for an existing distinct id
	Alias(distinctId, newId string) error
}

The Mixapanel struct store the mixpanel endpoint and the project token

Example
client := New("mytoken", "")

client.Track("1", "Sign Up", &Event{
	Properties: map[string]interface{}{
		"from": "email",
	},
})
Output:

func New

func New(token, apiURL string) Mixpanel

New returns the client instance. If apiURL is blank, the default will be used ("https://api.mixpanel.com").

Example
New("mytoken", "")
Output:

func NewFromClient

func NewFromClient(c *http.Client, token, apiURL string) Mixpanel

NewFromClient creates a client instance using the specified client instance. This is useful when using a proxy.

func NewFromClientWithSecret

func NewFromClientWithSecret(c *http.Client, token, secret, apiURL string) Mixpanel

NewFromClientWithSecret creates a client instance using the specified client instance and secret.

func NewWithSecret

func NewWithSecret(token, secret, apiURL string) Mixpanel

NewWithSecret returns the client instance using a secret.If apiURL is blank, the default will be used ("https://api.mixpanel.com").

Example
NewWithSecret("mytoken", "myapisecret", "")
Output:

type MixpanelError

type MixpanelError struct {
	URL string
	Err error
}

func (*MixpanelError) Cause

func (err *MixpanelError) Cause() error

func (*MixpanelError) Error

func (err *MixpanelError) Error() string

type Mock

type Mock struct {
	// All People identified, mapped by distinctId
	People map[string]*MockPeople
}

Mocked version of Mixpanel which can be used in unit tests.

Example
package main

import (
	"fmt"
	"time"
)

var fullfillsInterface Mixpanel = &Mock{}

func main() {
	client := NewMock()

	t, _ := time.Parse(time.RFC3339, "2016-03-03T15:17:53+01:00")

	client.Update("1", &Update{
		Operation: "$set",
		Timestamp: &t,
		IP:        "127.0.0.1",
		Properties: map[string]interface{}{
			"custom_field": "cool!",
		},
	})

	client.Track("1", "Sign Up", &Event{
		IP: "1.2.3.4",
		Properties: map[string]interface{}{
			"from": "email",
		},
	})

	client.Import("1", "Sign Up", &Event{
		IP:        "1.2.3.4",
		Timestamp: &t,
		Properties: map[string]interface{}{
			"imported": true,
		},
	})

	fmt.Println(client)

}
Output:

1:
  ip: 127.0.0.1
  time: 2016-03-03T15:17:53+01:00
  properties:
    custom_field: cool!
  events:
    Sign Up:
      IP: 1.2.3.4
      Timestamp:
      from: email
    Sign Up:
      IP: 1.2.3.4
      Timestamp: 2016-03-03T15:17:53+01:00
      imported: true

func NewMock

func NewMock() *Mock

func (*Mock) Alias

func (m *Mock) Alias(distinctId, newId string) error

func (*Mock) Import

func (m *Mock) Import(distinctId, eventName string, e *Event) error

func (*Mock) String

func (m *Mock) String() string

func (*Mock) Track

func (m *Mock) Track(distinctId, eventName string, e *Event) error

func (*Mock) Update

func (m *Mock) Update(distinctId string, u *Update) error

func (*Mock) UpdateGroup

func (m *Mock) UpdateGroup(groupKey, groupUser string, u *Update) error

func (*Mock) UpdateUser

func (m *Mock) UpdateUser(distinctId string, u *Update) error

type MockEvent

type MockEvent struct {
	Event
	Name string
}

type MockPeople

type MockPeople struct {
	Properties map[string]interface{}
	Time       *time.Time
	IP         string
	Events     []MockEvent
}

func (*MockPeople) String

func (mp *MockPeople) String() string

type Update

type Update struct {
	// IP-address of the user. Leave empty to use autodetect, or set to "0" to
	// not specify an ip-address at all.
	IP string

	// Timestamp. Set to nil to use the current time, or IgnoreTime to not use a
	// timestamp.
	Timestamp *time.Time

	// Update operation such as "$set", "$update" etc.
	Operation string

	// Custom properties. At least one must be specified.
	Properties map[string]interface{}
}

An update of a user in mixpanel

Jump to

Keyboard shortcuts

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