\Mixpanel

This is the main class for the Mixpanel PHP Library which provides all of the methods you need to track events and create/update profiles.

Architecture

This library is built such that all messages are buffered in an in-memory "queue" The queue will be automatically flushed at the end of every request. Alternatively, you can call "flush()" manually at any time. Flushed messages will be passed to a Consumer's "persist" method. The library comes with a handful of Consumers. The "CurlConsumer" is used by default which will send the messages to Mixpanel using forked cURL processes. You can implement your own custom Consumer to customize how a message is sent to Mixpanel. This can be useful when you want to put messages onto a distributed queue (such as ActiveMQ or Kestrel) instead of writing to Mixpanel in the user thread.

Options

Option Description Default
max_queue_size The maximum number of items to buffer in memory before flushing 1000
debug Enable/disable debug mode false
consumer The consumer to use for writing messages curl
consumers An array of custom consumers in the format array(consumer_key => class_name) null
host The host name for api calls (used by some consumers) api.mixpanel.com
events_endpoint The endpoint for tracking events (relative to the host) /events
people_endpoint The endpoint for making people updates (relative to the host) /engage
use_ssl Tell the consumer whether or not to use ssl (when available) true
error_callback The name of a function to be called on consumption failures null
connect_timeout In both the SocketConsumer and CurlConsumer, this is used for the connection timeout (i.e. How long it has take to actually make a connection). 5
timeout In the CurlConsumer (non-forked), it is used to determine how long the cURL call has to execute. 30

Example: Tracking an Event

$mp = Mixpanel::getInstance("MY_TOKEN");

$mp->track("My Event");

Example: Setting Profile Properties

$mp = Mixpanel::getInstance("MY_TOKEN", array("use_ssl" => false));

$mp->people->set(12345, array( '$first_name' => "John", '$last_name' => "Doe", '$email' => "john.doe@example.com", '$phone' => "5555555555", 'Favorite Color' => "red" ));

Summary

Methods
Properties
Constants
__construct()
getInstance()
enqueue()
enqueueAll()
flush()
reset()
identify()
track()
register()
registerAll()
registerOnce()
registerAllOnce()
unregister()
unregisterAll()
getProperty()
createAlias()
$people
No constants found
_log()
_debug()
$_options
N/A
No private methods found
$_defaults
$_events
$_instances
N/A

Properties

$people

$people : \Producers_MixpanelPeople

An instance of the MixpanelPeople class (used to create/update profiles)

Type

\Producers_MixpanelPeople

$_options

$_options : array

An array of options to be used by the Mixpanel library.

Type

array

$_defaults

$_defaults : array

Default options that can be overridden via the $options constructor arg

Type

array

$_instances

$_instances : array<mixed,\Mixpanel>

Instances' list of the Mixpanel class (for singleton use, splitted by token)

Type

array<mixed,\Mixpanel>

Methods

__construct()

__construct(  $token, array  $options = array()) 

Instantiates a new Mixpanel instance.

Parameters

$token
array $options

getInstance()

getInstance(  $token, array  $options = array()) : \Mixpanel

Returns a singleton instance of Mixpanel

Parameters

$token
array $options

Returns

\Mixpanel

enqueue()

enqueue(array  $message = array()) 

Add an array representing a message to be sent to Mixpanel to the in-memory queue.

Parameters

array $message

enqueueAll()

enqueueAll(array  $messages = array()) 

Add an array representing a list of messages to be sent to Mixpanel to a queue.

Parameters

array $messages

flush()

flush(integer  $desired_batch_size = 50) 

Flush the events queue

Parameters

integer $desired_batch_size

reset()

reset() 

Empty the events queue

identify()

identify(string|integer  $user_id, [optional] string|integer $anon_id) 

Identify the user you want to associate to tracked events. The $anon_id must be UUID v4 format and not already merged to an $identified_id. All identify calls with a new and valid $anon_id will trigger a track $identify event, and merge to the $identified_id.

Parameters

string|integer $user_id
string|integer $anon_id

track()

track(string  $event, array  $properties = array()) 

Track an event defined by $event associated with metadata defined by $properties

Parameters

string $event
array $properties

register()

register(string  $property, mixed  $value) 

Register a property to be sent with every event.

If the property has already been registered, it will be overwritten. NOTE: Registered properties are only persisted for the life of the Mixpanel class instance.

Parameters

string $property
mixed $value

registerAll()

registerAll(array  $props_and_vals = array()) 

Register multiple properties to be sent with every event.

If any of the properties have already been registered, they will be overwritten. NOTE: Registered properties are only persisted for the life of the Mixpanel class instance.

Parameters

array $props_and_vals

registerOnce()

registerOnce(  $property,   $value) 

Register a property to be sent with every event.

If the property has already been registered, it will NOT be overwritten. NOTE: Registered properties are only persisted for the life of the Mixpanel class instance.

Parameters

$property
$value

registerAllOnce()

registerAllOnce(array  $props_and_vals = array()) 

Register multiple properties to be sent with every event.

If any of the properties have already been registered, they will NOT be overwritten. NOTE: Registered properties are only persisted for the life of the Mixpanel class instance.

Parameters

array $props_and_vals

unregister()

unregister(string  $property) 

Un-register an property to be sent with every event.

Parameters

string $property

unregisterAll()

unregisterAll(array  $properties) 

Un-register a list of properties to be sent with every event.

Parameters

array $properties

getProperty()

getProperty(string  $property) : mixed

Get a property that is set to be sent with every event

Parameters

string $property

Returns

mixed

createAlias()

createAlias(string|integer  $distinct_id, string|integer  $alias) 

An alias to be merged with the distinct_id. Each alias can only map to one distinct_id. This is helpful when you want to associate a generated id (such as a session id) to a user id or username.

Parameters

string|integer $distinct_id
string|integer $alias

_log()

_log(  $msg) 

Log a message to PHP's error log

Parameters

$msg

_debug()

_debug() : boolean

Returns true if in debug mode, false if in production mode

Returns

boolean