analytics v0.6.0 Analytics.Mixpanel

Link to this section Summary

Functions

Associates one identity with another, usually an anonymous user with an identified user once they sign up

Appends each to a list associated with the corresponding property name. Appending to a property that doesn’t exist will result in assigning a list with one element to that property

When processed, the property values are added to the existing values of the properties on the profile. If the property is not present on the profile, the value will be added to 0. It is possible to decrement by calling “$add” with negative values. This is useful for maintaining the values of properties like “Number of Logins” or “Files Uploaded”

Updates the profile attribute. If the profile does not exist, it creates it with these properties. If it does exist, it sets the properties to these values, overwriting existing values

Works just like set/2, except it will not overwrite existing property values. This is useful for properties like “First login date”

Adds a transactions to the individual user profile, which will also be reflected in the Mixpanel Revenue report

The list values in the request are merged with the existing list on the user profile, ignoring duplicates

Link to this section Functions

Link to this function alias_identity(distinct_id, new_distinct_id_or_user_id)

Associates one identity with another, usually an anonymous user with an identified user once they sign up.

This function MUST be called exactly once for each user when the used is signed up, otherwise user would be duplicated on Mixpanel side. You MUST not submit any People data before creating an alias, unless you work with legacy users that were signed up before Mixpanel was integrated.

By default, DistinctID was automatically set by Mixpanel and you would want to alias it with a User email in your database. (Email is preferred over User ID because your code won’t need to know the ID, which is useful for third-party integrations that don’t have access to the production data.)

Calling alias doesn’t actually change Mixpanel distinct_id; instead, what it does do is add the ID to a lookup table on Mixpanel’s end and map it to the original Mixpanel distinct_id.

You can receive a disting_id from your front-end or by reading a Mixpanel cookie set for your domain:

def client_distinct_id(conn) do
  # Usually it's format is: "mp_#{mixpanel_token}_mixpanel"
  cookie_name = "mp_sldasjdlasjiu39d8ds9hfn3l_mixpanel"

  with value when is_binary(value) <- get_mixpanel_cookie(conn, cookie_name),
       value = URI.decode(value),
       {:ok, %{"distinct_id" => distinct_id}} <- Jason.decode(value) do
    distinct_id
  else
    _ -> nil
  end
end

defp get_mixpanel_cookie(conn, cookie_name) do
  maybe_fetch_cookies(conn).cookies[cookie_name]
end

defp maybe_fetch_cookies(%{cookies: %Plug.Conn.Unfetched{}} = conn), do: Plug.Conn.fetch_cookies(conn)
defp maybe_fetch_cookies(%{} = conn), do: conn

Do not batch updates to people attributes with creating an alias, alias would not be available for other events and you would get duplicated in People.

Link to this function append(distinct_id, key, value)

Appends each to a list associated with the corresponding property name. Appending to a property that doesn’t exist will result in assigning a list with one element to that property.

Link to this function increment(distinct_id, key, value \\ 1)

When processed, the property values are added to the existing values of the properties on the profile. If the property is not present on the profile, the value will be added to 0. It is possible to decrement by calling “$add” with negative values. This is useful for maintaining the values of properties like “Number of Logins” or “Files Uploaded”.

Link to this function set(distinct_id, key, value)

Updates the profile attribute. If the profile does not exist, it creates it with these properties. If it does exist, it sets the properties to these values, overwriting existing values.

Link to this function set_once(distinct_id, key, value)

Works just like set/2, except it will not overwrite existing property values. This is useful for properties like “First login date”.

Link to this function track(distinct_id, event, properties \\ %{})

Tracks an event.

Link to this function track_charge(distinct_id, amount, metadata \\ %{})

Adds a transactions to the individual user profile, which will also be reflected in the Mixpanel Revenue report.

Link to this function union(distinct_id, key, list)

The list values in the request are merged with the existing list on the user profile, ignoring duplicates.