-module(plunk@event). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -define(FILEPATH, "src/plunk/event.gleam"). -export([track/2, decode/1]). -export_type([event/0, track_event_response/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -type event() :: {event, binary(), binary(), list({binary(), gleam@json:json()})}. -type track_event_response() :: {track_event_response, boolean(), binary(), binary(), gleam@option:option(binary())}. -file("src/plunk/event.gleam", 72). ?DOC( " Track events in your application and send them to Plunk. This function returns a Gleam `Request` type that can then be used with any client of your choice.\n" "\n" " # Example\n" "\n" " In this example we use the `hackney` HTTP client to send the request we get from `track`:\n" "\n" " ```gleam\n" " import gleam/json\n" " import gleam/hackney\n" " import plunk\n" " import plunk/event\n" "\n" " fn main() {\n" " let instance = plunk.new(key: \"YOUR_API_KEY\") /\n" " let req =\n" " instance\n" " |> event.track(Event(\n" " event: \"your-event\",\n" " email: \"someone@example.com\",\n" " data: [#(\"name\", json.string(\"John\"))],\n" " ))\n" "\n" " // In a real project, you want to pattern match on the result of `track` to handle errors instead of using `assert Ok(..)`.\n" " let assert Ok(resp) = hackney.send(req)\n" " let assert Ok(data) = event.decode(resp)\n" " // do whatever you want with the data\n" " }\n" " ```\n" ). -spec track(plunk@instance:instance(), event()) -> gleam@http@request:request(binary()). track(Instance, Event) -> Body = begin _pipe = gleam@json:object( [{<<"event"/utf8>>, gleam@json:string(erlang:element(2, Event))}, {<<"email"/utf8>>, gleam@json:string(erlang:element(3, Event))}, {<<"data"/utf8>>, gleam@json:object(erlang:element(4, Event))}] ), gleam@json:to_string(_pipe) end, _pipe@1 = Instance, plunk@internal@bridge:make_request(_pipe@1, <<"/track"/utf8>>, post, Body). -file("src/plunk/event.gleam", 86). ?DOC(" Decode the raw response into a `TrackEventResponse` type wrapped in a `Result` type that can be pattern matched on.\n"). -spec decode(gleam@http@response:response(binary())) -> {ok, track_event_response()} | {error, plunk@types:plunk_error()}. decode(Res) -> _pipe = Res, plunk@internal@bridge:decode(_pipe, fun track_event_response_decoder/0). -file("src/plunk/event.gleam", 26). -spec track_event_response_decoder() -> gleam@dynamic@decode:decoder(track_event_response()). track_event_response_decoder() -> gleam@dynamic@decode:field( <<"success"/utf8>>, {decoder, fun gleam@dynamic@decode:decode_bool/1}, fun(Success) -> gleam@dynamic@decode:field( <<"contact"/utf8>>, {decoder, fun gleam@dynamic@decode:decode_string/1}, fun(Contact) -> gleam@dynamic@decode:field( <<"event"/utf8>>, {decoder, fun gleam@dynamic@decode:decode_string/1}, fun(Event) -> gleam@dynamic@decode:optional_field( <<"timestamp"/utf8>>, none, gleam@dynamic@decode:optional( {decoder, fun gleam@dynamic@decode:decode_string/1} ), fun(Timestamp) -> gleam@dynamic@decode:success( {track_event_response, Success, Contact, Event, Timestamp} ) end ) end ) end ) end ).