segment_elixir
segment_elixir is a non-supported third-party client for Segment
Installation
In mix.exs, add the segment_elixir dependency:
def deps do
# Get the latest from hex.pm.
[
{:segment_elixir, "~> 2.0"},
]
end
And then run mix deps.get to install it.
Add the following configuration to your config.ex:
config :segment, write_key: "2iFFnRsCfi"In tests, you can set the configuration to use the sandbox:
# config/test.exs
config :segment, api: Segment.SandboxMake sure you reset the sandbox state between tests. If you're using Phoenix this is as easy as adding:
# test/support/conn_case.ex
setup tags
:ok = Segment.Sandbox.checkout()
...
endUsage
Configure Segment with your write_key
config :segment, write_key: System.get_env("SEGMENT_WRITE_KEY")
You can call the different methods on the API (like track, identify, etc.) either by:
- Passing in arguments, or:
- Passing a full struct (which allows you to set Context and Integrations)
track
Segment.track(user_id, event, %{property1: "", property2: ""})or the full way using a struct with all the possible options for the track call
%Segment.Track{
userId: "foo",
vent: "eventname",
properties: %{property1: "", property2: ""}
}
> Segment.trackidentify
Segment.identify(user_id, %{trait1: "", trait2: ""})or the full way using a struct with all the possible options for the identify call
%Segment.Identify{
userId: "foo",
%{
trait1: "",
trait2: ""
}
}
|> Segment.identifyscreen
Segment.screen(user_id, name)or the full way using a struct with all the possible options for the screen call
%Segment.Screen{
userId: "foo",
name: "bar"
}
|> Segment.screenalias
Note that we need to use
alias_userhere instead of Segment'salias, due toaliasbeing reserved in Elixir.
Segment.alias_user(user_id, previous_id)or the full way using a struct with all the possible options for the alias call:
%Segment.Alias{
userId: "foo",
previousId: "bar"
}
|> Segment.alias_usergroup
Segment.group(user_id, group_id)or the full way using a struct with all the possible options for the group call
%Segment.Group{
userId: "foo",
groupId: "bar"
}
|> Segment.grouppage
Segment.page(user_id, name)or the full way using a struct with all the possible options for the page call
%Segment.Page{
userId: "foo",
name: "bar"
}
|> Segment.pageUsage in tests
Sandbox keeps track of events that occured since the last Segment.Sandbox.checkout() call.
They can be accessed by following functions from Segment.Sandbox module:
get_track_calls/0get_identify_calls/0get_screen_calls/0get_alias_calls/0get_group_calls/0get_group_calls/0
Example usage:
tracked_events = Segment.Sandbox.get_track_calls() |> Enum.map(& &1.event)
assert "Trial Started" in tracked_events
assert length(tracked_events) == 1Keep in mind that the returned events will not be in order they arrived in. For this you can simply reverse the list:
get_track_calls() |> Enum.reverse()Running tests
There are not many tests at the moment. But you can run a live test on your segment account by running:
SEGMENT_KEY=yourkey mix testAcknowledgements
This is a fork from stueccles' analytics-elixir incorporating work done by lswith in his fork.