Mxpanel (Mxpanel v0.1.0) View Source

Client for Mixpanel Ingestion API.

Hex.pm Version CI Coverage Status

It provides a sync API that makes HTTP request to the Mixpanel API. And also a async API that buffers and delivers the buffered events to Mixpanel in background.

Checkout the documentation for more information.

Installation

The package can be installed by adding mxpanel to your list of dependencies in mix.exs:

def deps do
  [
    {:mxpanel, "~> 0.1.0"},
    {:jason, "~> 1.2"},
    {:finch, "~> 0.5"}
  ]
end

Usage

Track event

  1. Add to your supervision tree:
{Finch, name: Mxpanel.HTTPClient}
  1. Call track/2:
client = %Mxpanel.Client{token: "mixpanel project token"}
event = Mxpanel.Event.new("signup", "123")

Mxpanel.track(client, event)

Enqueue an event

  1. Add to your supervision tree:
{Finch, name: Mxpanel.HTTPClient},
{Mxpanel.Batcher, name: MyApp.Batcher, token: "mixpanel project token"}
  1. Call track_later/2:
event = Mxpanel.Event.new("signup", "123")

Mxpanel.track_later(MyApp.MxpanelBatcher, event)
  1. The event will be buffered, and later sent in batch to the Mixpanel API.

Telemetry

Mxpanel currently exposes following Telemetry events:

  • [:mxpanel, :batcher, :buffers_info] - Dispatched periodically by each running batcher exposing the size of each running buffer in the pool.

    • Measurement: %{}
    • Metadata: %{batcher_name: atom(), buffer_sizes: [integer()]}

Changelog

See the changelog.

Link to this section Summary

Functions

Returns the configured JSON encoding library for Mxpnale (defaults to Jason).

Send a single event into Mixpanel.

Enqueues the event. The event will be store in a buffer and sent in batches to mixpanel.

Import a batch of events into Mixpanel.

Link to this section Functions

Specs

json_library() :: module()

Returns the configured JSON encoding library for Mxpnale (defaults to Jason).

To customize the JSON library, including the following in your config/config.exs:

config :mxpanel, :json_library, Jason

Specs

track(Mxpanel.Client.t(), Mxpanel.Event.t()) :: :ok | {:error, term()}

Send a single event into Mixpanel.

client = %Mxpanel.Client{token: "mixpanel project token"}
event = Mxpanel.Event.new("signup", "123")
Mxpanel.track(client, event)
Link to this function

track_later(batcher_name, event)

View Source

Specs

track_later(Mxpanel.Batcher.name(), Mxpanel.Event.t()) :: :ok

Enqueues the event. The event will be store in a buffer and sent in batches to mixpanel.

Mxpanel.Batcher.start_link(name: MyApp.MxpanelBatcher, token: "mixpanel project token")
event = Mxpanel.Event.new("signup", "123")

Mxpanel.track_later(MyApp.MxpanelBatcher, event)

Why use it?

HTTP requests to the Mixpanel API often take time and may fail. If you are tracking events during a web request, you probably, don't want to make your users wait the extra time for the mixpanel API call to finish.

Checkout Mxpanel.Batcher for more information.

Link to this function

track_many(client, events)

View Source

Specs

track_many(Mxpanel.Client.t(), [Mxpanel.Event.t()]) :: :ok | {:error, term()}

Import a batch of events into Mixpanel.

client = %Mxpanel.Client{token: "mixpanel project token"}
event_1 = Mxpanel.Event.new("signup", "123")
event_2 = Mxpanel.Event.new("signup", "456")

Mxpanel.track(client, [event_1, event_2])