plunk/event

Types

Event is a type that represents a Plunk event.

pub type Event {
  Event(
    event: String,
    email: String,
    data: List(#(String, Json)),
  )
}

Constructors

  • Event(event: String, email: String, data: List(#(String, Json)))

EventResponse is a type that represents a Plunk event response (track event)

pub type TrackEventResponse {
  TrackEventResponse(
    success: Bool,
    contact: String,
    event: String,
    timestamp: Option(String),
  )
}

Constructors

  • TrackEventResponse(
      success: Bool,
      contact: String,
      event: String,
      timestamp: Option(String),
    )

Functions

pub fn decode(
  res: Response(String),
) -> Result(TrackEventResponse, PlunkError)

Decode the raw response into a TrackEventResponse type wrapped in a Result type that can be pattern matched on.

pub fn track(
  instance: Instance,
  event event: Event,
) -> Request(String)

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.

Example

In this example we use the hackney HTTP client to send the request we get from track:

import gleam/json
import gleam/hackney
import plunk
import plunk/event

fn main() {
  let instance = plunk.new(key: "YOUR_API_KEY") /
  let req =
    instance
    |> event.track(Event(
      event: "your-event",
      email: "someone@example.com",
       data: [#("name", json.string("John"))],
     ))

  // In a real project, you want to pattern match on the result of `track` to handle errors instead of using `assert Ok(..)`.
  let assert Ok(resp) = hackney.send(req)
  let assert Ok(data) = event.decode(resp)
  // do whatever you want with the data
}
pub fn track_event_response_decoder() -> fn(Dynamic) ->
  Result(TrackEventResponse, List(DecodeError))
Search Document