forked from asakura/mixpanel_api_ex
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmixpanel.ex
More file actions
58 lines (47 loc) · 1.48 KB
/
mixpanel.ex
File metadata and controls
58 lines (47 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
defmodule Mixpanel do
@moduledoc """
Elixir client for the Mixpanel API.
"""
defmacro __using__(opts) do
quote do
require Logger
use Supervisor
@otp_app unquote(opts)[:otp_app]
def child_spec(opts) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [opts]},
type: :supervisor
}
end
def start_link(opts) do
config = Application.get_env(@otp_app, :mixpanel)
if config do
Supervisor.start_link(__MODULE__, Keyword.merge(config, app: @otp_app))
else
Logger.warning("Mixpanel not configured for application #{@otp_app}")
:ignore
end
end
def init(config) do
children = [
{Mixpanel.Client, Keyword.merge(config, name: get_process_name())}
]
Supervisor.init(children, strategy: :one_for_one)
end
def track(event, properties \\ %{}, opts \\ []) do
opts = Keyword.merge(opts, process: get_process_name())
Mixpanel.Dispatcher.track(event, properties, opts)
end
def engage(distinct_id, operation, value \\ %{}, opts \\ []) do
opts = Keyword.merge(opts, process: get_process_name())
Mixpanel.Dispatcher.engage(distinct_id, operation, value, opts)
end
defp get_process_name() do
:"mixpanel_#{@otp_app}"
end
defoverridable track: 1, track: 2, track: 3
defoverridable engage: 2, engage: 3, engage: 4
end
end
end