Mixpanel API


init(options)

Initializes and returns a Mixpanel project instance. This is a static method. Multiple calls to init can be done (to allow multiple mixpanel projects to be used).

  • options - Required. Table with keys and values. Supported keys are:
    • token - Required. String. The token for the Mixpanel project. Can be found under Project Settings in Mixpanel web client.
    • name - Required. String. Arbitrary name for the project instance.

Example

local options = {
    token = "<TOKEN FROM MIXPANEL>",
    name = "test"
}
local mpInstance = mixpanel.init(options)

getInstance(name)

Fetches the Mixpanel project instance. It’s a static method.

  • name - Required. String. The name of the project instance, as specified in init.
local options = {
    token = "<TOKEN FROM MIXPANEL>",
    name = "test"
}
mixpanel.init(options)

local mpInstance = mixpanel.getInstance("test")

track(event, properties, time, callback)

Track an event. Events describe things that happen in your application, usually as the result of user interaction; for example, when a customer reads an article, uploads content, or signs up for your service, you can send an event to record the incident. If the tracking event is submitted successfully an id is returned (otherwise nil is returned). This id is later also returned in the callback function that describes whether the event was successfully accepted by Mixpanel.

  • event - String. Required. The name of the event to track
  • properties - Optional. Properties associated with the event represented by a Mixpanel Properties object.
  • time - Integer. Optional. Time of the event if it occurred in the past. Expressed in seconds since epoch.
  • callback - Optional. Callback function called when event response is received.

Example

local mp_properties = mixpanel.properties.new()

local properties = {
    level = 5,
    difficulty = "hard",
    used_help = true
}
event_properties:insert(properties)

local time = os.time()
mixpanel.getInstance("test"):track("level_completed", event_properties, time, function(event) print("success:  " .. (not event.isError)) end)

register_track_properties(properties)

With register_track_properties a default set of properties will be set on all events.

  • properties - Required. A table with key (string) values (string, number, boolean)

Example


local default_properties = { session_number = 5, target_store = "amazon" }

mixpanel.getInstance("test"):set_track_properties(default_properties)

mixpanel.getInstance("test"):track("New session")
mixpanel.getInstance("test"):track("Level completed")

engage(properties, time, callback)

Engage updates a profile record. It is required to set the user identify before updating a user profile. If the user profile update is submitted successfully an id is returned (otherwise nil is returned). This id is later also returned in the callback function that describes whether the update was successfully accepted by Mixpanel.

People analytics updates describe a fact you’ve learned about one of your customers. For example, when a customer enters their first name or their birthday into your sign-in form, or signs up for a new level of service, you may send a profile update to record what you’ve learned.

  • properties - Required. Mixpanel Properties object
  • time - Integer. Optional. Time of the event if it occurred in the past. Expressed in seconds since epoch.
  • callback - Optional. Callback function called when a response is received.

Example

local function callback(event)
    if not event.isError then
        print("Engage succeeded")
    else
        print("Engage failed")
    end
end

local mp_properties = mixpanel.properties.new()
mp_properties :set_once({signed_up_on_session = 5})

mixpanel.getInstance("test"):engage(mp_properties, os.time(), callback)

identify(id)

To update a user profile, a user identify is required to be set on the project instance. The user is identify is in practice the unique user id.

  • id - Required. String. Unique id of the user.
mixpanel.getInstance("test"):identify("USER_ID")

register_super_properties(properties)

With register_super_properties_once a default set of properties are set on all people updates.

  • properties - Required. A table with key (string) values (string, number, boolean)

Example

local mp_properties = mixpanel.properties.new()

mixpanel.getInstance("Puzzle"):register_super_properties({version = 10, category = "puzzle", platform = "Android"})

props:set({all_levels_completed = true})

mixpanel.getInstance("Puzzle"):engage(mp_properties)

register_super_properties_once(properties)

With register_super_properties_once a default set of properties are set on all people updates. If the property is already set for the user the update will be ignored.

  • properties - Required. A table with key (string) values (string, number, boolean)

Example

local mp_properties = mixpanel.properties.new()

mixpanel.getInstance("test"):register_super_properties_once({platform = "Android"})

mp_properties:set({all_levels_completed = true})

mixpanel.getInstance("test"):engage(mp_properties)

callback(event)

The callback function is called when Mixpanel responds to a tracking event or profile update. It can fail for various reasons or it can succeed.

  • event - A table is passed to the callback function

The event table will have the following parameters set

  • isError - True or false. Indicates if the request was successful or not.
  • name - Response event name, eg. "mixpanelRequest"
  • responseMsg - Response message. Usually "failed" or "ok".
  • responseId - This is the id of the response. Each query returns an id. If something goes wrong, nil is returned.
  • responseType - The type of request. Valid values are: "track",engage"