mixpanel

package module
v0.0.0-...-3b2cfc0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2019 License: MIT Imports: 5 Imported by: 0

README

Go Mixpanel client

GoDoc

An MIT-licensed Go client library for Mixpanel

Warning

This project is under heavy development and not officially released yet.

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

This library doesn't use 3rd party library for its core. But 3rd party library is being used for unit test. If you really want to run unit tests in the project.

go get -u github.com/spf13/viper
go get -u github.com/stretchr/testify/assert
Installing

Use go get to get this library.

go get -u github.com/antonyho/mixpanel

Running the tests

To thoroughly run all tests including Track and Engage, you have to set Mixpanel token to your environment variable. The test will make RESTful calls to your Mixpanel account.

*Nix environment:
export MIXPANEL_TOKEN="<token string from Mixpanel>"
Starting the test:
go test -v ./...

Usage

import github.com/antonyho/mixpanel

Tracking an event

token := "<Mixpanel token from Mixpanel setting page>"
mp := mixpanel.NewClient(token)
event := NewEvent("go-test", props)
event.DistinctID = "2"
event.Time = uint(time.Now().Unix())
event.IP = "8.8.8.8"
event.GroupKey = "MPGO"
event.GroupID = "MPGOTEST"
result, err := mp.Track(event)

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

TODO

  • GoDoc compatible Documentation
  • More test cases
  • Better instruction on README.md
  • License file
  • Version 1.0.0

Documentation

Overview

Package mixpanel provides the API client for making Mixpanel API calls.

Index

Examples

Constants

View Source
const (
	// Protocol is the protocol being used for Mixpanel API request.
	Protocol = "https"
	// Host is the hostname of Mixpanel API.
	Host = "api.mixpanel.com"
	// TrackingPath is the API URL path for Mixpanel event tracking.
	// https://developer.mixpanel.com/docs/http#section-events
	TrackingPath = "track/"
	// UpdatePath is the API URL path for Mixpanel customer profile updates.
	// https://developer.mixpanel.com/docs/http#section-profile-updates
	UpdatePath = "engage/"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AddOperation

type AddOperation struct {
	BasicUpdateOperation
	AddProperties map[string]interface{} `json:"$add"`
}

func (AddOperation) JSON

func (u AddOperation) JSON() string

type AppendOperation

type AppendOperation struct {
	BasicUpdateOperation
	AppendProperties map[string]interface{} `json:"$append"`
}

func (AppendOperation) JSON

func (u AppendOperation) JSON() string

type BasicUpdateOperation

type BasicUpdateOperation struct {
	Token       string `json:"$token"`
	DistinctID  string `json:"$distinct_id"`
	IP          string `json:"$ip,omitempty"`
	Time        uint   `json:"$time,omitempty"`
	IgnoreTime  bool   `json:"$ignore_time,omitempty"`
	IgnoreAlias bool   `json:"$ignore_alias,omitempty"`
}

BasicUpdateOperation is the basic structure of a update operation. It has attributes which every kind of update operation consists.

func (BasicUpdateOperation) JSON

func (u BasicUpdateOperation) JSON() string

JSON returns the JSON format string of this update operation struct.

func (*BasicUpdateOperation) SetToken

func (u *BasicUpdateOperation) SetToken(token string)

SetToken is use by Mixpanel client to set the Mixpanel API token before making API request.

type Client

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

Client is the client for making Mixpanel API.

func NewClient

func NewClient(token string) *Client

NewClient is the constructor for Mixpanel API client. It takes the Mixpanel API token string.

func (Client) Track

func (c Client) Track(e *Event) (success bool, err error)

Track makes event tracking API call to Mixpanel. It returns the simple response from Mixpanel with a boolean to indicate the API call was successful or not. It returns error when the RESTful request to Mixpanel has error.

Example
viper.SetEnvPrefix("mixpanel")
viper.AutomaticEnv()
viper.AllowEmptyEnv(true)
token := viper.Get("token")
if token == nil {
	log.Fatalf("Mixpanel Token is not provided for the test. You can add MIXPANEL_TOKEN to your environment variable for the test.\n")
}
client := NewClient(token.(string))

props := map[string]interface{}{"test": "testing"}
event := NewEvent("go-test", props)
event.DistinctID = "2"
event.Time = uint(time.Now().Unix())
event.IP = "8.8.8.8"
event.GroupKey = "MPGO"
event.GroupID = "MPGOTEST"
success, err := client.Track(event)
if err != nil {
	log.Printf("Error occur when tracking event. Error: %+v\n", err)
}
if !success {
	log.Printf("Unsuccessful %s event tracking with distinct ID: %s\n", event.Title, event.DistinctID)
}
Output:

func (Client) Update

func (c Client) Update(u UpdateOperation) (success bool, err error)

Update makes profile update API call to Mixpanel. It returns the simple response from Mixpanel with a boolean to indicate the API call was successful or not. It returns error when the RESTful request to Mixpanel has error.

Example
viper.SetEnvPrefix("mixpanel")
viper.AutomaticEnv()
viper.AllowEmptyEnv(true)
token := viper.Get("token")
if token == nil {
	log.Fatalf("Mixpanel Token is not provided for the test. You can add MIXPANEL_TOKEN to your environment variable for the test.\n")
}
client := NewClient(token.(string))

distinctID := "1"
props := map[string]interface{}{"test": "testing"}
update := NewSetOperation(distinctID, props)
success, err := client.Update(update)
if err != nil {
	log.Printf("Error occur when updating user profile. Error: %+v\n", err)
}
if !success {
	log.Printf("Unsuccessful updating user %s profile\n", distinctID)
}
Output:

type DeleteOperation

type DeleteOperation struct {
	BasicUpdateOperation
	Delete string `json:"$delete"`
}

func (DeleteOperation) JSON

func (u DeleteOperation) JSON() string

type Event

type Event struct {
	Title      string                 `json:"event"`
	Token      string                 `json:"-"`
	Properties map[string]interface{} `json:"properties"`
	DistinctID string                 `json:"-"`
	Time       uint                   `json:"-"`
	IP         string                 `json:"-"`
	InsertID   string                 `json:"-"`
	GroupKey   string                 `json:"-"`
	GroupID    string                 `json:"-"`
}

Event is the structure of Mixpanel event. https://developer.mixpanel.com/docs/http#section-events

func NewEvent

func NewEvent(event string, props map[string]interface{}) *Event

func (Event) JSON

func (e Event) JSON() string

JSON returns the JSON format string of this event struct.

func (*Event) SetToken

func (e *Event) SetToken(token string)

SetToken is use by Mixpanel client to set the Mixpanel API token before making API request.

type RemoveOperation

type RemoveOperation struct {
	BasicUpdateOperation
	RemoveProperties map[string]interface{} `json:"$remove"`
}

func (RemoveOperation) JSON

func (u RemoveOperation) JSON() string

type SetOnceOperation

type SetOnceOperation struct {
	BasicUpdateOperation
	SetOnceProperties map[string]interface{} `json:"$set_once"`
}

func (SetOnceOperation) JSON

func (u SetOnceOperation) JSON() string

type SetOperation

type SetOperation struct {
	BasicUpdateOperation
	SetProperties map[string]interface{} `json:"$set"`
}

func (SetOperation) JSON

func (u SetOperation) JSON() string

type UnionOperation

type UnionOperation struct {
	BasicUpdateOperation
	UnionProperties map[string][]interface{} `json:"$union"`
}

func (UnionOperation) JSON

func (u UnionOperation) JSON() string

type UnsetOperation

type UnsetOperation struct {
	BasicUpdateOperation
	UnsetProperties []string `json:"$unset"`
}

func (UnsetOperation) JSON

func (u UnsetOperation) JSON() string

type UpdateOperation

type UpdateOperation interface {
	JSON() string
	SetToken(token string)
}

func NewAddOperation

func NewAddOperation(distinctID string, properties map[string]interface{}) UpdateOperation

func NewAppendOperation

func NewAppendOperation(distinctID string, properties map[string]interface{}) UpdateOperation

func NewDeleteOperation

func NewDeleteOperation(distinctID string) UpdateOperation

func NewRemovalOperation

func NewRemovalOperation(distinctID string, properties map[string]interface{}) UpdateOperation

func NewSetOnceOperation

func NewSetOnceOperation(distinctID string, properties map[string]interface{}) UpdateOperation

func NewSetOperation

func NewSetOperation(distinctID string, properties map[string]interface{}) UpdateOperation

func NewUnionOperation

func NewUnionOperation(distinctID string, properties map[string][]interface{}) UpdateOperation

func NewUnsetOperation

func NewUnsetOperation(distinctID string, propertyNames []string) UpdateOperation

Jump to

Keyboard shortcuts

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